1. 스프링 컨트롤러를 테스트 하는 2가지 방법은?
@ExtendWith(MockitoExtension.class)
class SomethingControllerTest {
@Autowired
private MockMvc mockMvc;
@BeforeEach
void setUp() {
mockMvc = MockMvcBuilders.standaloneSetup(new SomethingController()).build();
}
@Test
void testSomething() throws Exception {
mockMvc.perform(MockMvcRequestBuilders.get("/somewhere"))
.andExpect(MockMvcResultMatchers.status().isOk())
.andDo(print())
;
}
}
@SpringBootTest
@AutoConfigureMockMvc
class BoardControllerTest {
@Autowired
private MockMvc mockMvc;
@Test
void test1() throws Exception {
mockMvc.perform(MockMvcRequestBuilders.get("/something1"))
.andExpect(MockMvcResultMatchers.status().isOk())
.andDo(print())
;
}
@Test
void test2() throws Exception {
mockMvc.perform(MockMvcRequestBuilders.get("/something2").queryParam("parameter1", "value"))
.andExpect(MockMvcResultMatchers.status().isOk())
.andDo(print())
;
}
}