【发布时间】:2021-12-28 07:34:19
【问题描述】:
我在尝试获取 TestRestTemplate 时遇到错误。有什么方法可以获取 TestRestTemplate 或者测试 ErrorController?
错误日志:https://pastebin.com/XVPU9qrb
package io.kuark.ppalli
import org.junit.jupiter.api.Test
import org.springframework.boot.test.context.SpringBootTest
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT
import org.springframework.test.context.ActiveProfiles
@ActiveProfiles("test")
@SpringBootTest(classes = [PpalliApi::class], webEnvironment = RANDOM_PORT)
class PpalliApiTest {
@Test
fun contextLoads() {}
}
import org.junit.jupiter.api.Test
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest
import org.springframework.boot.test.web.client.TestRestTemplate
import kotlin.test.assertEquals
@WebMvcTest
class FallbackErrorControllerTest {
@Autowired
lateinit var http: TestRestTemplate
@Test
fun handleError() {
val result = http.getForObject("/error", String::class.java);
assertEquals("""{"code": "NOT_FOUND"}""", result)
}
}
【问题讨论】:
标签: spring spring-boot kotlin spring-boot-test spring-mvc-test