Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | |||
5 | 6 | 7 | 8 | 9 | 10 | 11 |
12 | 13 | 14 | 15 | 16 | 17 | 18 |
19 | 20 | 21 | 22 | 23 | 24 | 25 |
26 | 27 | 28 | 29 | 30 | 31 |
Tags
- 인프런
- Greedy
- 자바
- java
- kotlin
- db
- 알고리즘
- 김영한
- Spring Boot
- 백준
- springdatajpa
- Servlet
- AOP
- Android
- SpringBoot
- 스프링 핵심 원리
- Thymeleaf
- http
- Proxy
- JPQL
- jpa
- Exception
- JDBC
- QueryDSL
- 스프링 핵심 기능
- pointcut
- 그리디
- spring
- 스프링
- transaction
Archives
- Today
- Total
개발자되기 프로젝트
JUnit Test 본문
1. @WebMvcTest, @AutoConfigureMockMvc
MVC를 test하기 위해서는 모든 SpringBoot를 불러올 필요가 없음.
@WebMvcTest
@AutoConfigureMockMvc
internal class ExceptionApiControllerTest {
}
2. MockMvc
@Autowired
lateinit var mockMvc: MockMvc
3. Test1
@Test
fun helloTest(){
mockMvc.perform(
MockMvcRequestBuilders.get("/api/exception/hello")
).andExpect(
MockMvcResultMatchers.status().isOk
).andExpect(
MockMvcResultMatchers.content().string("hello")
).andDo(MockMvcResultHandlers.print())
}
4. Test2
@Test
fun getTest() {
val queryParameters = LinkedMultiValueMap<String, String>()
queryParameters.add("name", "abc")
queryParameters.add("age", "20")
mockMvc.perform(
MockMvcRequestBuilders.get("/api/exception").queryParams(queryParameters)
).andExpect(
MockMvcResultMatchers.status().isOk
).andExpect(
MockMvcResultMatchers.content().string("abc 20")
)
}
@Test
fun getTestFail() {
val queryParameters = LinkedMultiValueMap<String, String>()
queryParameters.add("name", "abc")
queryParameters.add("age", "5")
mockMvc.perform(
MockMvcRequestBuilders.get("/api/exception").queryParams(queryParameters)
).andExpect(
MockMvcResultMatchers.status().isBadRequest
).andExpect(
MockMvcResultMatchers.content().contentType(MediaType.APPLICATION_JSON)
).andExpect(
MockMvcResultMatchers.jsonPath("\$.result_code").value("FAIL")
).andExpect(
MockMvcResultMatchers.jsonPath("\$.errors[0].field").value("age")
).andExpect(
MockMvcResultMatchers.jsonPath("\$.errors[0].value").value("5")
)
}
5. Test3
@Test
fun postTest() {
val userRequest = UserRequest().apply {
this.name = "abc"
this.age = 10
this.phoneNumber = "010-1111-1111"
this.address = "서울"
this.email = "aaa@naver.co"
this.createdAt = "2020-01-01 10:00:00"
}
val json = jacksonObjectMapper().writeValueAsString(userRequest)
mockMvc.perform(
MockMvcRequestBuilders.post("/api/exception")
.content(json)
.contentType(MediaType.APPLICATION_JSON)
).andExpect(
MockMvcResultMatchers.status().isOk
).andExpect(
MockMvcResultMatchers.jsonPath("\$.name").value("abc")
)
}
@Test
fun postTestFail() {
val userRequest = UserRequest().apply {
this.name = "abc"
this.age = -1
this.phoneNumber = "010-1111-1111"
this.address = "서울"
this.email = "aaa@naver.co"
this.createdAt = "2020-01-01 10:00:00"
}
val json = jacksonObjectMapper().writeValueAsString(userRequest)
mockMvc.perform(
MockMvcRequestBuilders.post("/api/exception")
.content(json)
.contentType(MediaType.APPLICATION_JSON)
).andExpect(
MockMvcResultMatchers.status().isBadRequest
).andExpect(
MockMvcResultMatchers.jsonPath("\$.name").value("abc")
)
}
6. GitHub
'인프런 > [인프런] Kotlin으로 개발하는 Spring Boot Web MVC' 카테고리의 다른 글
JUnit2 (0) | 2022.04.29 |
---|---|
예외처리: Exception Handler (0) | 2022.04.25 |
예외처리: Controleller Advice (0) | 2022.04.25 |
JSR-380 Bean Validation : custom annotaion (0) | 2022.04.25 |
JSR-380 Bean Validation (0) | 2022.04.24 |
Comments