一些JUNIT测试案例在一起运行所有测试课时,但单独通过

发布于 2025-01-21 21:58:33 字数 3255 浏览 0 评论 0原文

一些Junit测试用例在一起运行所有测试课程时会失败,但要单独通过。

我正在运行Maven“ MVN Clean Anstanc”,我可以看到以下内容。

  1. 一个Junit测试类(test1.java)正在失败,但所有其他测试类都通过。但是,如果我单独运行,则相同的测试类Test1.java正在通过。在Eclipse(IDE)和命令行中,我都注意到了相同的。
  2. 如果我评论了一个测试类(test2.java)中的所有内容,那么当我一起运行所有测试用例时,test1.java正在工作。

因此,test1.java由于test2.java而失败。但不确定确切原因是什么。如果有人可以提出根本原因,我将不胜感激。谢谢。

junit test1.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:

@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
    }
}

在测试失败时在控制台中获取以下错误:

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)

注意:测试方法调用REST API和API实现它将消息发送到Kafka主题。

Some Junit Test cases are failing when running all test classes together but passing individually.

I am running maven 'mvn clean install' and I could see the following things.

  1. One Junit test class (Test1.java) is failing but all other test classes are passing. But same failed test class Test1.java is passing if I run it separately. In both Eclipse (IDE) and command line I noticed the same.
  2. If I commented out everything inside one test class (Test2.java) then Test1.java is working when I run all test cases together.

So Test1.java is failing because of Test2.java. But not sure what is the exact cause. I would appreciate if anybody can suggest the root cause. Thanks.

Junit Test1.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 :

@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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

水染的天色ゝ 2025-01-28 21:58:33

从您提供的错误中,很明显您在并行中运行测试

这是共享资源system.setproperty(“ spring.kafka.bootstrap-servers”,embeddedkafka.getembeddedkafka(

  1. Test1被执行,使用svc_qual_change_topic创建和配置Embeddedkafka,并将其地址设置为共享属性spring.kafka.kafka.bootstrap-servers
  2. 然后test2被执行,将另一个embeddedkafka与moi_change_topic和现在属性spring.kafka.kafka.bootstrap-servers指向第二个Embeddedkafka。
  3. 执行了Test1的测试方法,读取属性spring.kafka.kafka.bootstrap-servers并使用“错误” Embeddedkafka。

解决方案是将

  1. 依赖于相同资源(System Property Spring.kafka.kafka.bootstrap-servers)排除的测试中排除。并因此运行它们。喜欢这里排除特定的测试,无法在Junit中并行运行。
  2. 将这两种测试组合在一起,并将Embeddedkafka与两个主题配置。

请注意,在每个测试之前设置Embeddedkafka(而不是上课之前)无济于事,因为没有任何阻止test.method1在同一时间执行test2.Methodx更改属性。

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());

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.
  2. Then Test2 gets executed, configures another embeddedKafka with MOI_CHANGE_TOPIC and now property spring.kafka.bootstrap-servers points to the second embeddedKafka.
  3. Test method from Test1 is executed, reads property spring.kafka.bootstrap-servers and uses "wrong" embeddedKafka.

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. And run them consequentially. Like here Exclude specific tests from being run in parallel in jUnit
  2. Combine both tests in one and configure embeddedKafka with both topics.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文