๋ฐ์ํ
๋น์ฆ๋์ค ๋ก์ง ์ ์ฉ ๋๋์ง ์๋ต ๋ฉ์์ง ํ์ธ
- offline ๊ณผ free ๊ฐ ํ์ธ
๋น์ฆ๋์ค ๋ก์ง
- ์ผ๋จ offline์ location์ ๊ฐ์ด ๋ค์ด์์ผ๋ฉด ์คํ๋ผ์ธ์ด๊ณ , ๊ฐ์ด ์์ผ๋ฉด ์จ๋ผ์ธ์ผ๋ก ์งํ์ด ๋๋ค.
- free๊ฐ์ basePrice์, MaxPrice๊ฐ ๋ ๋ค 0์ผ๋ , free ์ด๋ค.
Event.class
- ๋๋ฉ์ธ ํด๋์ค์๋ค๊ฐ ๋น์ฆ๋์ค ๋ก์ง์ ์ ์ฉ์์ผ์ค๋ค.
public void update() { this.free = (basePrice == 0) && (maxPrice == 0); this.offline = location != null; }
EventTests.class
@Test
public void testOffine() {
// Given
Event event = Event.builder()
.location("๊ฐ๋จ์ญ")
.build();
// When
event.update();
// Then
assertThat(event.isOffline()).isEqualTo(true);
// Given
event = Event.builder()
.build();
// When
event.update();
// Then
assertThat(event.isOffline()).isEqualTo(false);
}
@Test
public void testFree(){
// Given
Event event = Event.builder()
.basePrice(0)
.maxPrice(0)
.build();
// When
event.update();
// Then
assertThat(event.isFree()).isEqualTo(true);
// Given
Event event = Event.builder()
.basePrice(0)
.maxPrice(100)
.build();
// When
event.update();
// Then
assertThat(event.isFree()).isEqualTo(false);
}
๋ง์ฝ ํ
์คํธ ํ ์์ด ๋ฌด์ํ ๋ง๋ค๋ฉด, ์ผ์ผํ ๋ค ์์ฑํ๋๊ฑด ์ ๋ง ๊ท์ฐฎ์ ๊ฒ์ด๋ค.
์ด๋ด๋ ์ฌ์ฉํ ์ ์๋ ์์ฃผ ์ข์ ์ ๋
ธํ
์ด์
์ด ์๋ค.
maven dependency ์ถ๊ฐ
<!-- https://mvnrepository.com/artifact/pl.pragmatists/JUnitParams -->
<dependency>
<groupId>pl.pragmatists</groupId>
<artifactId>JUnitParams</artifactId>
<version>1.1.1</version>
<scope>test</scope>
</dependency>
์ ์ฉ๋ฒ
Junit5 ์์๋ ์ด๋ฐ์์ผ๋ก ์์ฑํด์ผํ๋ค.
@ParameterizedTest
@CsvSource({
"0,0,true",
"100, 0, false"
})
๋๋,
@MethodSource("method Name")
ํ ์คํธ ํด๋์ค
@RunWith(JUnitParamsRunner.class)
class EventTest {
.
. //์ฝ๋ ์๋ต
.
@ParameterizedTest
@MethodSource("parametersForOffline")
public void offline_์ธ์ง_์๋์ง(String location, boolean offline) {
// Given
Event event = Event.builder()
.location(location)
.build();
// When
event.update();
// Then
assertThat(event.isOffline()).isEqualTo(offline);
}
private static Object[] parametersForOffline() {
return new Object[] {
new Object[] {"korea", true},
new Object[] {null , false}
};
}
@ParameterizedTest
@MethodSource("parametersForTestFree")
public void testFree(int basePrice, int maxPrice, boolean free) {
// Given
Event event = Event.builder()
.basePrice(basePrice)
.maxPrice(maxPrice)
.build();
// When
event.update();
// Then
assertThat(event.isFree()).isEqualTo(free);
}
// type-safe ํ๊ฒ ๋ฉ์๋๋ก ๋นผ๋จ๋ค.
private static Object[] parametersForTestFree() {
return new Object[]{
new Object[] {0, 0, true},
new Object[] {100, 0, false},
new Object[] {0, 100, false}
};
}
EventControllerTest.class
@Test
@TestDescription("๋น์ฆ๋์ค ๋ก์ง ์ ์ฉ ํ
์คํธ")
public void createEvent_logic() throws Exception {
EventDto event = EventDto.builder()
.name("Spring")
.description("REST API Development with Spring")
.beginEnrollmentDateTime(LocalDateTime.of(2020, 9, 7, 2, 45))
.closeEnrollmentDateTime(LocalDateTime.of(2020, 9, 8, 2, 45))
.beginEventDateTime(LocalDateTime.of(2020, 9, 9, 2, 45))
.endEventDateTime(LocalDateTime.of(2020, 9, 10, 2, 45))
.basePrice(100)
.maxPrice(200)
.limitOfEnrollment(100)
.location("Daejoen")
.build();
//Mock๊ฐ์ฒด๋ก ๋ฐ์๊ธฐ ๋๋ฌธ์ save๋ ์๋ ๊ฒ์ด๊ณ , NullpointerException์ด ๋ฐ์ํ ๊ฒ์ด๋ค.
//๊ทธ๋ฆฌํ์ฌ Mockito.when(eventRepository.save(event)).thenReturn(event);
// eventRepository.save๊ฐ ํธ์ถ์ด ๋๋ฉด -> ๊ทธ๋ค์ event๋ฅผ ๋ฆฌํดํ๋ผ.
// Mockito.when(eventRepository.save(event)).thenReturn(event);
mockMvc.perform(post("/api/events/")
.contentType(MediaType.APPLICATION_JSON)//๋ณธ๋ฌธ ์์ฒญ์ json์ ๋ด์์ ๋ณด๋ด๊ณ ์๋ค๊ณ ์๋ ค์ค.
.accept(MediaTypes.HAL_JSON)//HAL_JSON์ผ๋ก ๋ฐ๋๋ค.
.content(objectMapper.writeValueAsString(event)))//์์ฒญ ๋ณธ๋ฌธ์ ๋ฃ์ด์ค๋ค. objectMapper๋ก event๋ฅผ json์ผ๋ก ๋ณํํ
.andDo(print())//์ด๋ค ์๋ต๊ณผ ์์ฒญ์ ๋ฐ์๋์ง ํ์ธ๊ฐ๋ฅ.
.andExpect(status().isCreated())//201์์ฒญ์ด ๋ค์ด์๋์ง?
.andExpect(jsonPath("id").exists()) //json์ id๊ฐ ์๋์ง?
.andExpect(header().exists(HttpHeaders.LOCATION))//ํค๋์ Location์ด ์๋์ง
.andExpect(header().string(HttpHeaders.CONTENT_TYPE, MediaTypes.HAL_JSON_VALUE))//content-type์ "application/hal+json"๊ฐ ๋์ค๋์ง?
.andExpect(jsonPath("free").value(false))
.andExpect(jsonPath("offline").value(true))
.andExpect(jsonPath("eventStatus").value(EventStatus.PUBLISHED.name()))
;
}
๋ฐ์ํ