I'm trying to get a SpringBootTest (Spring2.5.4) r...
# getting-started
s
I'm trying to get a SpringBootTest (Spring2.5.4) running after switching to Kotlin (1.7.20). So this is the link with the Java example: https://www.appsdeveloperblog.com/testresttemplate-http-post-example/ This is my code with constructor injection instead of property injection via
@Autowired
.
Copy code
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
class ValidationTest(
    private val restTemplate: TestRestTemplate,
) {
    @LocalServerPort
    private val port = 0

    @Test
    fun greetingShouldReturnDefaultMessage() {
        Assertions.assertThat(restTemplate.getForObject("<http://localhost>:$port/", String::class.java)).contains("405")
    }
...
}
Unfortunatle the constructor injection fails before the test is even entered. No ParameterResolver for TestRestTemplate can be found.
Copy code
No ParameterResolver registered for parameter [org.springframework.boot.test.web.client.TestRestTemplate restTemplate] in constructor [public de.fhirvalidationserver.ValidationTest(org.springframework.boot.test.web.client.TestRestTemplate)].
 org.junit.jupiter.api.extension.ParameterResolutionException: No ParameterResolver registered for parameter [org.springframework.boot.test.web.client.TestRestTemplate restTemplate] in constructor [public de.fhirvalidationserver.ValidationTest(org.springframework.boot.test.web.client.TestRestTemplate)].
I assumed SpringBootTest would instanciate an instance of TestRestTemplate for me. Was that the mistake or is something else missing?
s
@Ivan Pavlov I didn't and using it fixed it 😃👍
443 Views