3์ฐจ ํ๋ก์ ํธ ๋๋ Spring Rest Docs
๋ฅผ ์ด์ฉํด์ API๋ฌธ์๋ฅผ ์๋ํํด์ผ ํ๋ค.
Spring Rest Docs
๋ฅผ ์ฌ์ฉํ๊ธฐ ์ํด์๋ ํ์๋ก ํ
์คํธ ์ฝ๋๋ฅผ ์์ฑํด์ผ ํ๋ค. ํ
์คํธ๊ฐ ๋ชจ๋ ์ฑ๊ณตํด์ผ API๋ฌธ์๊ฐ ์์ฑ๋๋ ๊ฒ์ด๋ค.
Spring Rest Docs
๋ฅผ ๋ฐ๋ก ์ ์ฉํด๋ณด๊ธฐ ์ ์ Spring์์ ๊ฐ์ฅ ๋ง์ด ์ฐ๋ ํ
์คํธ ํ๋ ์์ํฌ์ธ Junit5
์ ๋ํด ์์๋ณด์.
@Test
@BeforeEach, @AfterEach
@BeforeAll, @AfterAll
@SpringBootTest
@WebMvcTest
@ExtendWith
@WebMvcTest(TestController.class)
@ExtendWith(MockitoExtension.class)
ํน์ ์์๋ฅผ ๊ธฐ์ค์ผ๋ก ํ ์คํธ์ ์์๋ฅผ ์ง์ ํ ์ ์๋ค.
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
public class TestClass {
@Test
@Order(1)
void ํ
์คํธ1() { ... }
@Test
@Order(2)
void ํ
์คํธ2() { ... }
}
ํ ์คํธ ๋ฉ์๋๋ฅผ ๋ฐ๋ณตํด์ ์คํํด์ผ ํ ๋,
public class TestClass {
@RepeatedTest(10)
void ํ
์คํธ1() { ... }
}
csv ํ์ ๋ฐ์ดํฐ๋ฅผ ํ๋ผ๋ฏธํฐ๋ก ๋ฐ๊ธฐ
@ParameterizedTest
@CsvSource({
"key1, 1",
"key2, 2",
"key3, 3",
})
void ํ
์คํธ(String key, int value) { ... }
csvํ์ผ์ ๋ฐ์ดํฐ๋ฅผ ํ๋ผ๋ฏธํฐ๋ก ๋ฐ๊ธฐ
@ParameterizedTest
@CsvFileSource(resources="/filename.csv")
void ํ
์คํธ(String key, int value) { ... }
์ธ์ ๊ฐ ์ธ ์ผ์ด ์์์ง๋.. ?
@MockBean(JpaMetamodelMappingContext.class)
@WebMvcTest(UserController.class)
@ExtendWith({MockitoExtension.class})
class UserControllerTest {
@MockBean
UserService userService;
@MockBean
UserDetailsServiceImpl userDetailsService;
@Autowired
MockMvc mockMvc;
@Test
@DisplayName("์ผ๋ฐ ํ์๊ฐ์
ํ
์คํธ")
void ์ผ๋ฐ_ํ์๊ฐ์
() throws Exception {
// Given
SignupRequestDto dto = SignupRequestDto.builder()
.username("test")
.password("password")
.reviewer(false)
.build();
// When
doReturn(new User(1L, dto.getUsername(), dto.getPassword(), UserRole.ROLE_USER, 0, new ArrayList<>()))
.when(userService).saveUser(dto);
Gson gson = new Gson();
// Then
mockMvc.perform(MockMvcRequestBuilders.post("/user/signup")
.contentType(MediaType.APPLICATION_JSON)
.content(gson.toJson(dto)))
.andDo(print())
.andExpectAll(
status().isOk(),
jsonPath("$.result").value("success")
);
}
}
์ฐธ๊ณ
https://donghyeon.dev/junit/2021/04/11/JUnit5-%EC%99%84%EB%B2%BD-%EA%B0%80%EC%9D%B4%EB%93%9C/