restAPI κ΅¬μ‘°λ‘ requestλ @valid λ‘ μ ν¨μ± κ²μ¬λ₯Ό μ§ννκ³ μλ νλ‘μ νΈκ° μλ€
λ μμ μ μΈ μλ² μ 곡μ μν΄ API κ°λ°μ΄ λͺ¨λ μλ£λ μμ μμ ν μ€νΈλ₯Ό μ§ννλ €κ³ νλ€
κ°λ°κΈ°κ°μ΄ 짧μ
κ°λ°μ νλ©° λ¨μ ν μ€νΈλ₯Ό μ§ννλ TDD λ°©μμΌλ‘ κ°λ°νμ§ μμκΈ° λλ¬Έμ
μ΄μ μμ λ¨μ ν μ€νΈλ₯Ό μΌμΌν μ§ννκΈ°λ³΄λ€ ν΅ν© ν μ€νΈλ₯Ό νλ κ²μ΄ λ μ ν©νλ€κ³ νλ¨νλ€
post / body μμ
import ...
@SpringBootTest
@AutoConfigureMockMvc
class MissionControllerTest {
@Autowired
MockMvc mockMvc;
@Autowired
private ObjectMapper objectMapper;
@Test
@WithUserDetails(value = "1232142μ΄μ©κ΅¬userUID")
@Transactional
void createOrPatchMissionSuccess() throws Exception {
String content = objectMapper.writeValueAsString(new MissionInfo(ZonedDateTime.now(), "test"));
mockMvc.perform(post("/v2/mission/info")
.contentType(MediaType.APPLICATION_JSON)
.content(content))
.andExpect(status().isOk())
.andExpect(MockMvcResultMatchers.jsonPath("$.code").value("0000"))
.andDo(print());
}
}
get μμ
@Test
@WithUserDetails(value = "λΌλΌλΌλΌγ
userUID")
@Transactional
void getMissionSuccessRecordSuccess() throws Exception {
mockMvc.perform(get("/v2/mission/success"))
.andExpect(status().isOk())
.andExpect(MockMvcResultMatchers.jsonPath("$.code").value("0000"))
.andDo(print());
}
patch / param μμ
@Test
@WithUserDetails(value = "V8Inlz8q67hhClVGD9gJeRDQ7hY2")
@Transactional
void changeMissionSuccessRecordSuccess() throws Exception {
mockMvc.perform(patch("/v2/mission/success")
.param("id", "1"))
.andExpect(status().isOk())
.andExpect(MockMvcResultMatchers.jsonPath("$.code").value("0000"))
.andDo(print());
}
delete μμ
@Test
@WithUserDetails(value = "γ
γ΄λ―Έγ
γΉμγ
£γ
userUID")
@Transactional
void deleteAccountSuccess() throws Exception {
mockMvc.perform(delete("/v2/accounts"))
.andExpect(status().isOk())
.andExpect(MockMvcResultMatchers.jsonPath("$.code").value("0000"))
.andDo(print());
}
μ°Έκ³ ν μλ£
JUnit μ¬μ©
λ°μν
'κ°λ° > Spring' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
μ€νλ§ μΉ κ°λ° λ°©μ (static / MVC / API) (0) | 2022.03.03 |
---|---|
Spring νλ‘μ νΈ μμ± (Gradle) (0) | 2022.03.03 |
ZonedDateTime (UTC -> KST) (0) | 2022.02.23 |
GraphQL (spring) (0) | 2021.12.18 |
JavaScript μμ μ½λ (axios/restAPI/HackerRank) (0) | 2021.11.18 |