当前位置: 首页 > 知识库问答 >
问题:

JUnit测试在一起运行时失败,但单独通过

尉迟明辉
2023-03-14

我有一堆JUnit测试,它们都单独运行。每一个都是一个真正的独立单元测试--被测试的单个类。不需要上下文。我可以在Eclipse中或通过maven/surefire-plugin单独或一起运行它们。

此后,我添加了一个新的集成测试,它利用了Spring上下文等,并使用了SpringJUnit4ClassRunner。一旦我将这个测试添加到我的套件中,任何测试用例都会在这个类失败后运行。

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = IntegrationTestConfiguration.class)
@DirtiesContext(classMode=ClassMode.AFTER_EACH_TEST_METHOD)
@ActiveProfiles("test")
public class ImportServiceIntegrationTest {
   ...
}

我不确定这是否有巨大的价值,但我也在这里发布了我的配置类:

@EnableAutoConfiguration(exclude = { WebMvcAutoConfiguration.class,
        DispatcherServletAutoConfiguration.class,
        EmbeddedServletContainerAutoConfiguration.class,
        WebSocketAutoConfiguration.class })
@ComponentScan(basePackages = "com.rtc.synchronize",
        excludeFilters = @ComponentScan.Filter(type = FilterType.REGEX, pattern="com\\.rtc\\.synchronize\\.config\\.AppConfig"))
@EnableJpaRepositories("com.util.veracode.rtc.synchronize")
@EntityScan("com.util.veracode.rtc.synchronize")
public class IntegrationTestConfiguration {
}

如果我实际的@configuration类有用,我也可以发布这些类,尽管为了简洁起见,我避免了它们(我不完全确定它们有多有用)。

我怀疑在测试类终止后,JVM中维护了一些东西(一些静态数据)。

@Configuration
@EnableCaching(mode=AdviceMode.ASPECTJ)
public class CacheConfig extends CachingConfigurerSupport{

    /**
     * EhCache configuration.  Used to minimize calls to Veracode
     * 
     * @return
     */
    @Bean(destroyMethod="shutdown")
    public net.sf.ehcache.CacheManager ehCacheManager() {
        ...
        ...
    }
        ...
}
java.lang.IllegalStateException: The workItems Cache is not alive (STATUS_SHUTDOWN)
        at net.sf.ehcache.Cache$CacheStatus.checkAlive(Cache.java:4097)
        at net.sf.ehcache.Cache.checkStatus(Cache.java:2788)
        at net.sf.ehcache.Cache.get(Cache.java:1744)
        at org.springframework.cache.ehcache.EhCacheCache.get(EhCacheCache.java:65)
        at org.springframework.cache.interceptor.AbstractCacheInvoker.doGet(AbstractCacheInvoker.java:68)
        at org.springframework.cache.interceptor.CacheAspectSupport.findInCaches(CacheAspectSupport.java:461)
        at org.springframework.cache.interceptor.CacheAspectSupport.findCachedItem(CacheAspectSupport.java:432)
        at org.springframework.cache.interceptor.CacheAspectSupport.execute(CacheAspectSupport.java:333)
        at org.springframework.cache.interceptor.CacheAspectSupport.execute(CacheAspectSupport.java:299)
        at org.springframework.cache.aspectj.AbstractCacheAspect.ajc$around$org_springframework_cache_aspectj_AbstractCacheAspect$1$2bc714b5(AbstractCacheAspect.aj:74)
        at com.synchronize.repository.rtc.WorkItemRepositoryImpl.findById(WorkItemRepositoryImpl.java:192)
        at com.synchronize.repository.rtc.WorkItemRepositoryImpl.findById(WorkItemRepositoryImpl.java:192)
        at com.synchronize.repository.rtc.WorkItemRepositoryImpl.getState(WorkItemRepositoryImpl.java:179)
        at com.synchronize.repository.rtc.WorkItemRepositoryImplTest.testGetState(WorkItemRepositoryImplTest.java:178)

如果我将 false 添加到我的surefire-plugin定义中,所有测试都会通过,但我对这个解决方案/变通方法不满意。它降低了构建的速度,并且在Eclipse中不受尊重--也就是说,我只是在一个镜头中对整个项目运行一个JUnit测试运行程序,而不会失败。

我是否必须做一些特别的事情来确保Spring在测试用例完成后从JVM中清除出来?为什么我在发布集成测试时会有一些Spring configuraiton?

共有1个答案

鲜于温书
2023-03-14

我在测试套件中注意到,一些基于AspectJ的实现具有引用某个spring bean的静态字段,当概要文件甚至整个spring测试上下文发生更改时,该引用不会更新(我在Spring-Security中观察到了这一点,但@cache可能也存在类似的问题)

我的变通方法是在不同的目录(例如src/test/javasecurity)中按使用的spring配置对测试用例进行分组,并使用命名模式将它们分开。-分开的目录/源文件夹用于eclipse,所以我可以单击目录根文件夹并指示eclipse运行该源文件夹中的所有测试。-命名模式用于用maven执行测试(因为surefire具有通过类命名模式选择执行的测试的特性,而不是通过根文件夹)

  • 目录/模式
    • src/test/java,pattern:*test-包含使用spring配置而不使用Spring-security的测试
    • src/test/javasecurity,pattern:*securitytest需要启用Spring Security性的Spring测试
    <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>build-helper-maven-plugin</artifactId>
        <executions>
            <execution>
                <id>add-security-test-source</id>
                <phase>generate-sources</phase>
                <goals>
                    <goal>add-test-source</goal>
                </goals>
                <configuration>
                    <sources>
                        <source>${basedir}/src/test/javaSecurity</source>
                    </sources>
                </configuration>
            </execution>
        </executions>
    </plugin>
    
    
    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <executions>
            <execution>
                <id>normal-test</id>
                <phase>test</phase>
                <goals>
                    <goal>test</goal>
                </goals>
                <configuration>
                    <excludes>
                        <exclude>**/Abstract*.java</exclude>
                        <exclude>**/*SecurityTest.java</exclude>
                    </excludes>
                    <includes>
                        <include>**/*Test.java</include>
                        <include>**/*Tests.java</include>
                    </includes>
                    <reportsDirectory>${project.build.directory}/surefire-reports/normaltest</reportsDirectory>
                </configuration>
            </execution>            
            <execution>
                <id>security-tests</id>
                <phase>test</phase>
                <goals>
                    <goal>test</goal>
                </goals>
                <configuration>
                    <excludes>
                        <exclude>**/Abstract*.java</exclude>
                    </excludes>
                    <includes>
                        <include>**/*SecurityTest.java</include>
                    </includes>
                    <reportsDirectory>${project.build.directory}/surefire-reports/security-test</reportsDirectory>
                </configuration>
            </execution>
        </executions>
        <configuration>
            <!-- just needed to prevent some bugs -->
            <excludes />
            <includes>
                <include>nothing</include>
            </includes>
            <reportsDirectory>${project.build.directory}/surefire-reports/nothing</reportsDirectory>
        </configuration>
    </plugin>
    

 类似资料:
  • 我目前正在做一个学校的作业,我正在努力与测试部分。出于某种原因,单元测试单独运行时运行良好,但一起运行时就不行了。我知道这与我在他们之间共享对象有关,而我不应该基于我以前的搜索,但我一生都无法找出需要改变什么来解决这个问题。下面是ApplientService类和ApplientServiceTest类的代码。任何帮助都将非常感谢,因为我已经被困在这个问题上一段时间了,现在知道这可能是其他人会立即

  • 我一直遇到一个奇怪的问题。我的测试用例有一个失败的测试,。但是,如果我单独运行相同的程序,它将运行得非常完美。我不熟悉JUnit,不知道为什么会发生这种情况。 如果我注释掉最后一个测试(已经注释掉),我的所有测试都成功运行!然而,如果我不评论它,一个测试失败,但那不是这个测试!它是失败!

  • 不要与之前提出的问题混淆“为什么我的测试在一起运行时失败,但单独通过?” 我有一个任务,我需要修改JUnit测试类来处理多个数据库测试。在实现之前,我需要确保所有测试都在运行,没有失败。令我困惑的是,现在当我一起运行所有的类时,它显示它运行时没有失败。当我运行一个特定的类时,它突然失败了,如果我重复它,结果仍然存在。 这可能是什么原因造成的? 我自己没有写测试,因此我对测试内容的了解是有限的。不过

  • 这是我的整个测试课程: 有3个单元测试,它们在单独运行时都通过了,但当我运行整个测试类时,我的第2个和第3个测试失败,错误如下: 我已经想尽一切办法来解决这个问题: 我将测试实例化下的类移动到@Before函数中 我尝试创建@After函数并调用Mockito。重置我的模拟 我应该提到的是,我正在使用nhaarman。mockitokotlin2库和argumentCaptor。 关于为什么这些测

  • 我为咖啡因CacheLoader实现编写的单元测试(JUnit,Mockito)在单独运行时都成功了,但在一起运行时其中一个失败了。我相信我在所有测试对象设置中使用了的最佳实践。 当与其他人一起运行时,测试testGet_WhenCalledASecondAndThirdTimeBeyondCacheDuration_LoadingMethodCalledASecondTime每次都会失败,并出现

  • 我有几个JUnit测试,都使用运行。我可以从我的SpringSource工具套件(EclipseJuno)IDE中按类单独运行它们,它们通过了。如果我尝试按模块运行它们(“运行所选项目中的所有测试”),则它们将失败,并出现以下初始化错误: 有什么办法解决吗?甚至故障排除。 吉文斯: JUnit 4.11版