2017-09-13 43 views
1

我有一个使用kotlin进行开发的spring-boot应用程序 - 总体情况进展顺利。 (春1.5.6.RELEASE,科特林1.1.4-3)Kotlin - lateinit TestRestTemplate无法初始化集成测试

反正我是将我的第一个控制器测试回顾一些示例代码后,和我遇到了这个恼人的错误:

kotlin.UninitializedPropertyAccessException :lateinit财产restTemplate尚未初始化

kotlin.UninitializedPropertyAccessException:lateinit财产testRestTemplate尚未初始化

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) 

下面是测试类

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") 
    } 
} 

有些事情ADDL我已经试过: - 确保我没有使用过时TestRestTemplate - 使用setter注入,而不是字段注入试过,但浪费时间。 - 禁用kotlin编译器插件

回答

2

Spring Boot不会自动实例化TestRestTemplate bean(duh!)。

您需要自己定义它,然后才能使用它。

@Bean 
open fun restTemplate(): TestRestTemplate { 
    return TestRestTemplate() 
} 
+0

也许我失去了一些东西,但是从javadoc的https://docs.spring.io/autorepo/docs/spring-boot/1.4.x-SNAPSHOT/api/org/springframework/boot/test /web/client/TestRestTemplate.html它说的是相反的“如果您使用的是SpringBootTest批注,TestRestTemplate自动可用,并且可以自动装入您的测试。” –

+0

通过扩展AbstractTestNGSpringContextTests,也就是类ProductSetControllerTests:AbstractTestNGSpringContextTests(){ –

+0

@michaelsalmon有趣,它用于不这样做 - 无论如何,我很高兴它解决了,我将尽快删除我的答案 –

相关问题