Stefan S.
Stefan S.

Reputation: 73

How to forward the local port of a SpringBootTest to a test configuration

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

Upvotes: 5

Views: 4605

Answers (3)

Alex
Alex

Reputation: 7926

Here's what we did in this case:

@Configuration
class Config {
    private lateinit var port: java.lang.Integer // declare a var to store the port
    
    @EventListener // subscribe to servlet container initialized event
    fun onServletContainerInitialized(event: EmbeddedServletContainerInitializedEvent) {
        port = event.embeddedServletContainer.port // when event is fired, extract the port for that event
    }
}

Upvotes: 1

Czar
Czar

Reputation: 1703

With Spring Boot 2.1.6 this works for me:

import com.example.MyApplication
import org.junit.jupiter.api.Assertions.assertTrue
import org.junit.jupiter.api.Test
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.test.context.SpringBootTest
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment
import org.springframework.boot.web.server.LocalServerPort
import org.springframework.context.support.GenericApplicationContext
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig

@SpringJUnitConfig
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT, classes = [MyApplication::class])
class ApplicationStartsTest {
    @LocalServerPort
    protected var port: Int = 0

    @Autowired
    lateinit var context: GenericApplicationContext

    @Test
    fun `application context is initialized`() {
        assertTrue(::context.isInitialized, "Application context should have been injected")
    }

    @Test
    fun `web application port is assigned`() {
        assertTrue(port != 0, "web application port should have been injected")
    }
}

Upvotes: 6

Stefan S.
Stefan S.

Reputation: 73

Solution for spring version greater than 2.0.0, which is working perfectly for me, is:

@Configuration
open class TestTemplateConfig {
    private var localPort: Int? = null

    @EventListener(WebServerInitializedEvent::class)
    fun onServletContainerInitialized(event: WebServerInitializedEvent) {
        localPort = event.webServer.port
    }
}

Upvotes: 2

Related Questions