简体   繁体   English

一些 Junit 测试用例在一起运行所有测试类但单独通过时失败

[英]Some Junit Test cases are failing when running all test classes together but passing individually

Some Junit Test cases are failing when running all test classes together but passing individually.一些 Junit 测试用例在一起运行所有测试类但单独通过时失败。

I am running maven 'mvn clean install' and I could see the following things.我正在运行 maven 'mvn clean install',我可以看到以下内容。

  1. One Junit test class (Test1.java) is failing but all other test classes are passing.一个 Junit 测试 class (Test1.java) 失败,但所有其他测试类都通过了。 But same failed test class Test1.java is passing if I run it separately.但是如果我单独运行它,同样失败的测试 class Test1.java 正在通过。 In both Eclipse (IDE) and command line I noticed the same.在 Eclipse (IDE) 和命令行中,我注意到了相同的情况。
  2. If I commented out everything inside one test class (Test2.java) then Test1.java is working when I run all test cases together.如果我在一个测试 class (Test2.java) 中注释掉所有内容,那么当我一起运行所有测试用例时,Test1.java 正在运行。

So Test1.java is failing because of Test2.java.所以 Test1.java 由于 Test2.java 而失败。 But not sure what is the exact cause.但不确定具体原因是什么。 I would appreciate if anybody can suggest the root cause.如果有人能提出根本原因,我将不胜感激。 Thanks.谢谢。

Junit Test1.java: Junit 测试 1.java:

@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class Test1 {

        public static final String REST_API_URI = "/api/someurl/abc";

        @Autowired
        private TestRestTemplate testRestTemplate;
    
        private static final String SVC_QUAL_CHANGE_TOPIC = "tnco-svcqual-change";
    
        @ClassRule
        public static EmbeddedKafkaRule embeddedKafka = new EmbeddedKafkaRule(1,
                true,
                1,
                SVC_QUAL_CHANGE_TOPIC);
        
        @BeforeClass
        public static void setUpBeforeClass() throws IOException {
            System.setProperty("spring.kafka.bootstrap-servers", embeddedKafka.getEmbeddedKafka().getBrokersAsString());
        }
    
        @AfterClass
        public static void tearDownAfterClass() {
            System.clearProperty("spring.kafka.bootstrap-servers");
        }
    
        @Test
        public void testCreateCheckServiceQualification() {
          ...
          //some API test code which send msg to Kafka
        }   
    }

Junit Test2.java: Junit 测试 2.java:

@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, properties = { "alm.ishtar.security.enabled = false" })
public class Test2 {

    public static final String REST_API_URI = "/api/someurl/xyz";

    @Autowired
    private TestRestTemplate testRestTemplate;
    
    private static final String MOI_CHANGE_TOPIC = "tnco-moi-change";
    
    @ClassRule
    public static EmbeddedKafkaRule embeddedKafka = new EmbeddedKafkaRule(1,
            true,
            1,
            MOI_CHANGE_TOPIC);
    
    @BeforeClass
    public static void setUpBeforeClass() throws IOException {
        System.setProperty("spring.kafka.bootstrap-servers", embeddedKafka.getEmbeddedKafka().getBrokersAsString());
    }

    @AfterClass
    public static void tearDownAfterClass() {
        System.clearProperty("spring.kafka.bootstrap-servers");
    }

    @Test
    public void testCreateMoiPut() throws Exception {
      ...
      //some API test code which send msg to Kafka
    }
}

Getting the below error in the console at the time of test failure:测试失败时在控制台中出现以下错误:

org.springframework.kafka.KafkaException: Send failed; nested exception is org.apache.kafka.common.errors.TimeoutException: Topic tnco-svcqual-change not present in metadata after 60000 ms.
org.springframework.kafka.KafkaException: Send failed; nested exception is org.apache.kafka.common.errors.TimeoutException: Topic tnco-svcqual-change not present in metadata after 60000 ms.
        at org.springframework.kafka.core.KafkaTemplate.doSend(KafkaTemplate.java:573)
        at org.springframework.kafka.core.KafkaTemplate.send(KafkaTemplate.java:401)

Note: Test method calls REST API and inside the API implementation it is sending message to Kafka topic.注意:测试方法调用 REST API 并且在 API 实现中它正在向 Kafka 主题发送消息。

From the error you provided, it is obvious that you run tests in parallel .从您提供的错误来看,很明显您并行运行了测试。

And this is a shared resource : System.setProperty("spring.kafka.bootstrap-servers", embeddedKafka.getEmbeddedKafka().getBrokersAsString());这是一个共享资源System.setProperty("spring.kafka.bootstrap-servers", embeddedKafka.getEmbeddedKafka().getBrokersAsString());

I can assume that我可以假设

  1. Test1 gets executed, it creates and configures embeddedKafka with SVC_QUAL_CHANGE_TOPIC , and sets its address to the shared property spring.kafka.bootstrap-servers . Test1 被执行,它使用SVC_QUAL_CHANGE_TOPIC创建和配置 embeddedKafka,并将其地址设置为共享属性spring.kafka.bootstrap-servers
  2. Then Test2 gets executed, configures another embeddedKafka with MOI_CHANGE_TOPIC and now property spring.kafka.bootstrap-servers points to the second embeddedKafka.然后执行 Test2,使用MOI_CHANGE_TOPIC配置另一个嵌入式 Kafka,现在属性spring.kafka.bootstrap-servers指向第二个嵌入式 Kafka。
  3. Test method from Test1 is executed, reads property spring.kafka.bootstrap-servers and uses "wrong" embeddedKafka.执行 Test1 中的测试方法,读取属性spring.kafka.bootstrap-servers并使用“错误的”嵌入式 Kafka。

Solution would be to解决方案是

  1. Exclude tests which rely on the same resource (system property spring.kafka.bootstrap-servers in this case) from the parallel run.从并行运行中排除依赖相同资源(在本例中为系统属性spring.kafka.bootstrap-servers )的测试。 And run them consequentially.并相应地运行它们。 Like here Exclude specific tests from being run in parallel in jUnit像这里一样排除特定测试在 jUnit 中并行运行
  2. Combine both tests in one and configure embeddedKafka with both topics.将两个测试合二为一,并使用两个主题配置 embeddedKafka。

Note, that setting up embeddedKafka before each test (instead of before Class) won't help, as nothing prevents Test1.method1 from executing at the same time Test2.methodX changes the property.请注意,在每次测试之前(而不是在 Class 之前)设置 embeddedKafka 不会有帮助,因为没有什么可以阻止 Test1.method1 同时执行 Test2.methodX 更改属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM