81-2: Spring MockMvc

jk·2024년 4월 26일
0

kdt 풀스택

목록 보기
124/127



1. 스프링 컨트롤러를 테스트 하는 2가지 방법은?

//code1
@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())
		;
	}
}
//code2
@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())
		;
	}
}



2. 게시판에서 페이징시 2페이지 10개의 값을 확인 하도록 테스트 코드를 작성하시오.

	@Test
	void testAjaxList() throws Exception {
		mockMvc.perform(MockMvcRequestBuilders.get("/board/ajax_list").queryParam("page", "2"))
			.andExpect(MockMvcResultMatchers.status().isOk())
			.andDo(print())
		;
	}
profile
Brave but clumsy

0개의 댓글