반응형
비즈니스 로직 적용 됐는지 응답 메시지 확인
- 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()))
;
}
반응형
반응형
비즈니스 로직 적용 됐는지 응답 메시지 확인
- 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()))
;
}
반응형