develog

[junit5] controller test @WebMvcTest 본문

Dev/junit

[junit5] controller test @WebMvcTest

냐옴 2020. 3. 2. 02:14

 

import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.jupiter.api.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.test.context.junit.jupiter.SpringExtension;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;

import static org.assertj.core.api.Assertions.assertThat;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;

@ExtendWith(SpringExtension.class)
@WebMvcTest(controllers = {MyController.class})
public class MyControllerTest {

    @Autowired
    MockMvc mockMvc;

    @Autowired
    ObjectMapper objectMapper;

    @Test
    void test_00() throws Exception {
        assertThat(mockMvc).isNotNull();
        assertThat(objectMapper).isNotNull();

        MvcResult mvcResult = mockMvc.perform(get("/myhello/test"))
                .andDo(print())
                .andReturn();

        System.out.println("mvcResult = " + mvcResult);

        String content = mvcResult.getResponse().getContentAsString();
        System.out.println("content = " + content);
    }
}

'Dev > junit' 카테고리의 다른 글

[junit] gradle junit5  (0) 2020.04.21
[junit5] mockito  (0) 2020.03.07
[junit5] controller test @SpringBootTest  (0) 2020.02.29
[Junit] Test Assertion  (0) 2019.12.23
[junit4] pom.xml  (0) 2019.08.27
Comments