admin管理员组

文章数量:1391995

My setup is as follow.

I have a Kafka Consumer:

@KafkaListener(topics = "${pmp.kafka.import-catalog-processing.topic}", concurrency = "1", groupId = "${pmp.kafka.group-id}",
        containerFactory = "kafkaProductListenerContainerFactory", errorHandler = "kafkaErrorHandler")
public void listen(List<String> values, @Header(KafkaHeaders.RECEIVED_KEY) List<String> keys) { ...

Which then uses a FileDownloadService during the processing.

And i use this as a BaseIntegrationTest

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@ActiveProfiles({"test"})
public abstract class BaseIntegrationTest {

@MockitoBean
protected FileDownloadService fileDownloadService;

and mock the fileDownloadService class. I need to use @MockitoBean because the urls which will be downloaded are not under my control.

Now if i run a single integration test, the fileDownloadService works as expected. But if i run more than the test case which depends on the @KafkaListener does not work as expected.

What i see is that the instance of the fileDownloadService which is used during the processing of the listen method is another one as the one i have injected in the integration test.

My assumption is that the Bean with @KafkaListener is instantiated in the beginning with the "first" fileDownloadService. But the context changes between the tests.

I tried a lot so far. Unfornately, nothing worked yet. Also using @DirtiesContext does not change the fact that there are multiple fileDownloadService.

Any idea what could help here?

Thanks in advance+

My setup is as follow.

I have a Kafka Consumer:

@KafkaListener(topics = "${pmp.kafka.import-catalog-processing.topic}", concurrency = "1", groupId = "${pmp.kafka.group-id}",
        containerFactory = "kafkaProductListenerContainerFactory", errorHandler = "kafkaErrorHandler")
public void listen(List<String> values, @Header(KafkaHeaders.RECEIVED_KEY) List<String> keys) { ...

Which then uses a FileDownloadService during the processing.

And i use this as a BaseIntegrationTest

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@ActiveProfiles({"test"})
public abstract class BaseIntegrationTest {

@MockitoBean
protected FileDownloadService fileDownloadService;

and mock the fileDownloadService class. I need to use @MockitoBean because the urls which will be downloaded are not under my control.

Now if i run a single integration test, the fileDownloadService works as expected. But if i run more than the test case which depends on the @KafkaListener does not work as expected.

What i see is that the instance of the fileDownloadService which is used during the processing of the listen method is another one as the one i have injected in the integration test.

My assumption is that the Bean with @KafkaListener is instantiated in the beginning with the "first" fileDownloadService. But the context changes between the tests.

I tried a lot so far. Unfornately, nothing worked yet. Also using @DirtiesContext does not change the fact that there are multiple fileDownloadService.

Any idea what could help here?

Thanks in advance+

Share Improve this question asked Mar 14 at 6:39 user984200user984200 3031 gold badge7 silver badges20 bronze badges 3
  • Please edit your question to include a minimal reproducible example. – knittl Commented Mar 14 at 9:17
  • Without seeing any code, it's impossible to know what could be wrong, but have you already checked everything in stackoverflow/questions/74027324/…? – knittl Commented Mar 14 at 9:17
  • Please edit your question with an example test class that is having this issue – zn43 Commented Mar 15 at 18:54
Add a comment  | 

1 Answer 1

Reset to default 0

In the end the issue was that i had more than one @ConfigInitializer within my test class hierarchy. This lead to different contexts.

本文标签: springIssue with SpringBootTest with KafkaListener and MockitoSpyBeanStack Overflow