[英]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',我可以看到以下内容。
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我可以假设
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
。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。spring.kafka.bootstrap-servers
and uses "wrong" embeddedKafka.执行 Test1 中的测试方法,读取属性spring.kafka.bootstrap-servers
并使用“错误的”嵌入式 Kafka。Solution would be to解决方案是
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 中并行运行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.