[springboot] junit4 + controller test [mockMvc]

gradle 기본셋팅 후


webcontroller 생성

@RestController
public class WebController {
@GetMapping
public String test() {
return "hello";
}
}


test code 작성

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 static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;


@SpringBootTest
@AutoConfigureMockMvc
class WebControllerTest {
@Autowired
private MockMvc mockMvc;

@Test
void findsTaskById() throws Exception {
mockMvc.perform(get("/"))
.andDo(print())
.andExpect(status().isOk())
.andExpect(content().string("hello"));
}
}


기본 path "/" 호출시 status가 ok이면서 content가 hello 인지 확인