I have a spring-boot app in development using kotlin - overall things are going well. (spring 1.5.6.RELEASE, kotlin 1.1.4-3)
Anyway, I was adding my first controller test after reviewing some example code, and am running into this annoying error:
kotlin.UninitializedPropertyAccessException: lateinit property restTemplate has not been initialized
kotlin.UninitializedPropertyAccessException: lateinit property testRestTemplate has not been initialized
at com.thingy.controllers.ProductSetControllerTest.getTestRestTemplate(ProductSetControllerTest.kt:16)
at com.thingy.controllers.ProductSetControllerTest.testGet(ProductSetControllerTest.kt:20)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:80)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:714)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:901)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1231)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:127)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:111)
at org.testng.TestRunner.privateRun(TestRunner.java:767)
at org.testng.TestRunner.run(TestRunner.java:617)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:334)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:329)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:291)
at org.testng.SuiteRunner.run(SuiteRunner.java:240)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1198)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1123)
at org.testng.TestNG.run(TestNG.java:1031)
at org.testng.IDEARemoteTestNG.run(IDEARemoteTestNG.java:72)
at org.testng.RemoteTestNGStarter.main(RemoteTestNGStarter.java:123)
Here's the test class
import org.junit.runner.RunWith
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.test.context.SpringBootTest
import org.springframework.boot.test.web.client.TestRestTemplate
import org.springframework.test.context.junit4.SpringRunner
import org.testng.Assert
import org.testng.annotations.Test
@RunWith(SpringRunner::class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
class ProductSetControllerTest {
@Autowired
lateinit var restTemplate: TestRestTemplate
@Test
fun testGet() {
val forObject = restTemplate.getForObject("/", String::class.java)
Assert.assertEquals(forObject, "gettest")
}
}
Some addl things I've tried: - ensured I'm not using deprecated TestRestTemplate - tried using setter injection instead of field injection, but was a waste of time. - disabled kotlin compiler plugins
Spring Boot does not automatically instantiate TestRestTemplate
bean(duh!).
You need to define it yourself and then you will be able to use it.
@Bean
open fun restTemplate(): TestRestTemplate {
return TestRestTemplate()
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.