I am currently struggling with the server port injection of the SpringBootTest instance. I've written a test configuration class where I would like to access this port.
Test Configuration class:
@Target(AnnotationTarget.CLASS, AnnotationTarget.FILE)
@Retention(AnnotationRetention.RUNTIME)
@Import(value = [TestTemplateConfig::class])
annotation class TestAnnotation
@Configuration
open class TestTemplateConfig {
@Value("\${server.port}")
private var localPort: Int? = null
@Bean
open fun foo() = Foo(localPort)
}
The Test looks like this:
@SpringBootJunit5Test
@TestAnnotation
@EnableTestCouchbase
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
class MyIntegrationTest {
@LocalServerPort
var port: Int = 0
@Autowired
private lateinit var foo: Foo
...
}
The problem now is, that I always receive a value of zero for the port in the configuration class. Because I don't get null this sounds like it is working to get the port but the wrong one (I think zero is defined for a random port in spring). The evaluation of the server port in the MyIntegrationTest class is working properly so far.
Any ideas to fix this?
Thanks