如何正确自动连接MockMvc bean

萨玛灵魂

我对此有些麻烦。

我正在尝试测试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() {

    }
}

提前致谢。

萨玛灵魂

感谢您的回答,我将有效的答案放在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

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

@MockBean
private MyService service;

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章