【发布时间】:2019-07-03 19:56:01
【问题描述】:
我有点麻烦。
我正在尝试测试我的 Spring Boot 应用程序的 Web 层(使用 JUnit5)。
我正在使用@WebMvcTest(NoteController::class) 允许我自动连接MockMvc 以模拟请求。
但我收到以下错误:kotlin.UninitializedPropertyAccessException: lateinit property mvc has not been initialized
NoteControllerTest
import org.hamcrest.Matchers.`is`
import org.junit.Test
import org.junit.jupiter.api.extension.ExtendWith
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest
import org.springframework.http.MediaType
import org.springframework.test.context.junit.jupiter.SpringExtension
import org.springframework.test.web.servlet.MockMvc
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post
import org.springframework.test.web.servlet.result.MockMvcResultMatchers.*
@ExtendWith(SpringExtension::class)
@WebMvcTest(NoteController::class)
class NoteControllerTest {
@Autowired
private lateinit var mvc: MockMvc
@Test
fun should_create_a_note() {
mvc.perform(
post("/notes"))
.andExpect(status().isCreated)
.andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8))
.andExpect(jsonPath("$.content", `is`("my content")))
}
}
NoteController
import fr.$$.$$.api.CreateNote
import fr.$$.$$.api.FetchNote
import fr.$$.$$.resources.Note
import fr.$$.$$.resources.toResource
import org.springframework.http.HttpStatus
import org.springframework.http.ResponseEntity
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.PostMapping
import org.springframework.web.bind.annotation.RestController
import java.net.URI
@RestController("/notes")
class NoteController(val createNote: CreateNote,
val fetchNote: FetchNote) {
@GetMapping
fun getAllNotes(): ResponseEntity<List<Note>> {
return ResponseEntity(fetchNote.all().toResource(), HttpStatus.OK)
}
@PostMapping
fun createNote(): ResponseEntity<Note> {
val note = createNote.with("my content").toResource()
return ResponseEntity.created(URI("")).body(note)
}
}
SmartNotesApplicationTest
import org.junit.Test
import org.junit.jupiter.api.extension.ExtendWith
import org.springframework.boot.test.context.SpringBootTest
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT
import org.springframework.test.context.junit.jupiter.SpringExtension
@ExtendWith(SpringExtension::class)
@SpringBootTest(webEnvironment = RANDOM_PORT)
class SmartNotesApplicationTest {
@Test
fun contextLoad() {
}
}
提前致谢。
【问题讨论】:
-
您正在混合使用 JUnit 4 和 JUnit 5 类型(Test 来自 JUnit 4,但 ExtendWith 来自 JUnit 5)。
-
请注意,使用 JUnit5,除了 WebMvcTest 之外,您不需要 ExtendWith(SpringExtension::class),因为 WebMvcTest 已经使用 ExtendWith(SpringExtension::class) 进行了元注释
-
谢谢,没注意到!
标签: spring-mvc kotlin junit5