Search code examples
spring-bootspockspring-boot-testembedded-kafka

Application context is reloaded for every Spock specification when using EmbeddedKafka


I have a spring boot application with integration test setup. I have a base groovy class which I extend in every specification. Spring boot version 2.0.6 My problem is that application context is restarted for each specification.

I will appreciate any help with this. Thanks.

People seem to have this issue when they use either DirtiesContext or MockBean. I dont have either.

This is what the base looks like

@SpringBootTest(classes = [Application],
                webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@ActiveProfiles("spring-boot-test")
class SpringBootTestBase extends Specification {
...
}

I expect all my tests to use the same context.


Solution

  • The problem was EmbeddedKafka. It is recreated and reloads the context. Using this https://github.com/spring-projects/spring-kafka/blob/master/src/reference/asciidoc/testing.adoc#using-the-same-brokers-for-multiple-test-classes fixes the issue.