본문으로 바로가기

 

1. pom.xml에 다음 라이브러리를 설정한다.

<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-test</artifactId>
	<scope>test</scope>
</dependency>

 

2. 테스트할 클래스를 선택한 후 JunitTest4를 선택한다.

 

@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.MOCK)
@AutoConfigureMockMvc
public class UserConrollerTest {

	@Autowired
	MockMvc mockMvc;
	
	@Test
	public void test() throws Exception {
		mockMvc.perform(MockMvcRequestBuilders.get("/user/all"))
			   .andExpect(MockMvcResultMatchers.status().isOk())
			   .andExpect(MockMvcResultMatchers.content().contentType(MediaType.APPLICATION_JSON_UTF8))
			   .andDo(MockMvcResultHandlers.print());
			
	}

}

 

MOCK: 톰켓 x, RANDON_PORT : 톰켓 O, NONE: 서블릿 환경 X