spring - Kotlin - lateinit TestRestTemplate 没有为集成测试初始化

标签 spring spring-boot kotlin

我有一个正在使用 kotlin 开发的 spring-boot 应用程序——总体上进展顺利。 ( Spring 1.5.6.RELEASE, Kotlin 1.1.4-3)

无论如何,在查看了一些示例代码后,我正在添加我的第一个 Controller 测试,并且遇到了这个烦人的错误:

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

我尝试过的一些附加操作:
- 确保我没有使用过时的 TestRestTemplate
- 尝试使用 setter 注入(inject)而不是字段注入(inject),但浪费时间。
- 禁用 kotlin 编译器插件

最佳答案

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

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

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

关于spring - Kotlin - lateinit TestRestTemplate 没有为集成测试初始化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46192828/

相关文章:

java - 为什么 Spring MVC 应用程序不显示?

arrays - 如何使用mockito模拟包含对象的数组?

java - 为什么我的错误被 Kapt 和 Kotlin 掩盖了?如何增加错误限制?

spring - 启用 HiddenHttpMethodFilter 后使用 Spring MVC 3.0.2 上传多个文件

java - @RelatedTo(elementClass = Concept.class)RelatedTo.elementClass 必须是类升

java - MySQLNonTransientConnectionException : Client does not support authentication protocol requested by server; consider upgrading MySQL client

java - 为什么我的 Spring Boot (mongo) bean 没有被创建/使用?

java - 尽管抛出相同的异常,但预期异常的 JUnit 测试仍失败

java - 无法使用外部库编译 Spring Boot 应用程序

kotlin - 合并Kotlin流