spring-mvckotlinjunit5

How to autowire MockMvc bean correctly


I have a bit of trouble with this.

I'm trying to test the web layer of my Spring boot app (with JUnit5).
I'm using the @WebMvcTest(NoteController::class) to allow me to autowire MockMvc in order to mock requests.

But I get the below error :
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() {

    }
}

Thanks in advance.


Solution

  • thanks for the answers, I put the working answer in Java with Spring Boot 2.2.6:

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

    Note : You can use @WebMvcTest(AuthorController.class) instead of @SpringBootTest + AutoConfigureMockMvc

    With this annotation, it will only load the web layer. If you have dependencies in your controller (services, repositories...) you must use :

    @MockBean
    private MyService service;