一些JUNIT测试案例在一起运行所有测试课时,但单独通过
一些Junit测试用例在一起运行所有测试课程时会失败,但要单独通过。
我正在运行Maven“ MVN Clean Anstanc”,我可以看到以下内容。
- 一个Junit测试类(test1.java)正在失败,但所有其他测试类都通过。但是,如果我单独运行,则相同的测试类Test1.java正在通过。在Eclipse(IDE)和命令行中,我都注意到了相同的。
- 如果我评论了一个测试类(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.
- 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.
- 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
从您提供的错误中,很明显您在并行中运行测试。
这是共享资源:
system.setproperty(“ spring.kafka.bootstrap-servers”,embeddedkafka.getembeddedkafka(
)
svc_qual_change_topic
创建和配置Embeddedkafka,并将其地址设置为共享属性spring.kafka.kafka.bootstrap-servers
。moi_change_topic
和现在属性spring.kafka.kafka.bootstrap-servers
指向第二个Embeddedkafka。spring.kafka.kafka.bootstrap-servers
并使用“错误” Embeddedkafka。解决方案是将
Spring.kafka.kafka.bootstrap-servers
)排除的测试中排除。并因此运行它们。喜欢这里排除特定的测试,无法在Junit中并行运行。请注意,在每个测试之前设置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
SVC_QUAL_CHANGE_TOPIC
, and sets its address to the shared propertyspring.kafka.bootstrap-servers
.MOI_CHANGE_TOPIC
and now propertyspring.kafka.bootstrap-servers
points to the second embeddedKafka.spring.kafka.bootstrap-servers
and uses "wrong" embeddedKafka.Solution would be to
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 jUnitNote, 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.