如何正确自动连接MockMvc bean

时间:2019-07-03 19:56:01

标签: spring-mvc kotlin junit5

对此我有点麻烦。

我正在尝试测试Spring引导应用程序的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() {

    }
}

谢谢。

2 个答案:

答案 0 :(得分:1)

感谢您的回答,我通过Spring Boot 2.2.6将有效的回答放在Java中:

AuthorController.class

import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping(path = "/authors")

public class AuthorController {
    @GetMapping("/health")
    public boolean healthcheck() {
        return true;
    }
}

AuthorControllerIT.class

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultHandlers;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;

@SpringBootTest
@AutoConfigureMockMvc
class AuthorControllerIT {

    @Autowired
    private MockMvc mockMvc;

    @Test
    public void test_web_layer() throws Exception {
        mockMvc.perform(MockMvcRequestBuilders.get("/authors/health"))
                .andDo(MockMvcResultHandlers.print())
                .andExpect(MockMvcResultMatchers.status().isOk())
                .andExpect(MockMvcResultMatchers.content().string("true"));
    }
}

注意: 您可以使用@WebMvcTest(AuthorController.class)代替@SpringBootTest + AutoConfigureMockMvc

使用此注释,它将仅加载网络图层。 如果您的控制器(服务,存储库...)中有依赖项,则必须使用:

@MockBean
private MyService service;

答案 1 :(得分:0)

我注入了auto,然后为每个测试构建了一个新的WebApplicationContext

MockMvc