Search code examples
spring-bootkotlinintegration-testingjunit5

How to debug, why Spring Boot integration test context is not reused between tests?


I got another tricky question for the crowd. I got two separate test files reusing the same Context class. I would expect them to reuse the same context, alas Spring ist started two times, prolonging the build time. Do you have any ideas how to figure out / debug, what triggers the context reload?

The test classes look like:

@SpringBootTest(
  classes = [HttpProxyTestContext::class]
)
@AutoConfigureWireMock(port = 8082)
internal class AuthOpenidConnectSpringIT {
...
}

and

@SpringBootTest(
  classes = [HttpProxyTestContext::class]
)
@AutoConfigureWireMock(port = 8082)
internal class AuthOidcWebClientIT {
...
}

The Context class is

@JooqTest
@ComponentScan(basePackages = ["de.denktmit.someproject.springconfig"])
class HttpProxyTestContext {}

Best regards, stay healthy,

Marius Schmmidt


Solution

  • I was finally able to figure out, that the problem somehow arose from using @AutoConfigureWireMock(port = 8082). I found it out by experimenting with the annotations used. Finally I slightly adjusted my test setup and finally my context is reused. Here's how I did it, thats my test only config:

    @SpringBootConfiguration
    @ComponentScan(basePackages = ["de.denktmit.someproject.springconfig"])
    @AutoConfigureJooq
    class HttpProxyTestContext {
    
      @Bean(destroyMethod = "stop")
      fun wiremock(): WireMockServer {
        val wireMockServer = WireMockServer(wireMockConfig().port(8082))
        wireMockServer.start()
        WireMock.configureFor("localhost", 8082);
        return wireMockServer
      }
    
    }
    

    And I just gets picked up as easy as

    @SpringBootTest
    internal class AuthOpenidConnectSpringIT {
      ...
    }
    
    @SpringBootTest
    internal class AuthOidcWebClientIT {
       ...
    }
    

    Wiremock @BeforeEach setup left untouched and is the same as before. May it be helpful.

    Good night and best regards,

    Marius Schmidt