반응형
05. 05. Test Driven Development - 06. 06. REST API
TDD
Test Driven Development
: Red + Green+ refactoring : red ( 테스트 코드 오류 가 나는 것을 보고 초록이 되게 refactoring 한다.)
REST API
representational state transfer
package com.fastcampus.eatgo.domain;
import java.util.ArrayList;
import java.util.List;
public class Restaurant {
private final String name;
private final String location;
private final Long id;
private List<MenuItem> menuItems = new ArrayList<MenuItem>();
public Restaurant(Long id, String name, String location) {
this.name = name;
this.id = id;
this.location = location;
}
public List<MenuItem> getMenuItems() {
return menuItems;
}
public void setMenuItems(List<MenuItem> menuItems) {
// this.menuItems = menuItems;
for (MenuItem menuItem : menuItems) {
addMenuItem(menuItem);
}
}
public String getLocation() {
return location;
}
public Object getName() {
return this.name;
}
public Object getInformation() {
return this.name + " in " + this.location;
}
public Long getId() {
return id;
}
public void addMenuItem(MenuItem menuItem) {
menuItems.add(menuItem);
}
}
package com.fastcampus.eatgo.domain;
import org.junit.Test;
import static org.hamcrest.core.Is.is;
import static org.junit.Assert.*;
public class RestaurantTests {
@Test
public void creation() {
Restaurant restaurant = new Restaurant(1004L, "Bob zip", "Seoul");
assertThat(restaurant.getName(), is("Bob zip"));
assertThat(restaurant.getId(), is(1004L));
assertThat(restaurant.getLocation(), is("Seoul"));
}
@Test
public void information() {
Restaurant restaurant = new Restaurant(1004L, "Bob zip", "Seoul");
assertThat(restaurant.getInformation(), is("Bob zip in Seoul"));
}
}
package com.fastcampus.eatgo.application;
import com.fastcampus.eatgo.domain.*;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import java.util.ArrayList;
import java.util.List;
import static org.hamcrest.core.Is.is;
import static org.junit.Assert.*;
import static org.mockito.BDDMockito.given;
public class RestaurantServiceTest {
private RestaurantService restaurantService;
@Mock //가짜 객체
private RestaurantRepository restaurantRepository;
@Mock //가짜 객체
private MenuItemRepository menuItemRepository;
// 스프링이 해줄수 없는 작업들을 미리 한다, 예) 서비스와 레포지토리 연결
@Before //모든 테스트 시작전에 반드시 이것을 해본다.
public void setUp() {
MockitoAnnotations.initMocks(this);//가짜객체 초기화들
// restaurantRepository = new RestaurantRepositoryImpl();
// menuItemRepository = new MenuItemRepositoryImpl();
mockRestaurantRespository();
mockMenuItemRespository();
restaurantService = new RestaurantService(restaurantRepository, menuItemRepository);
}
private void mockMenuItemRespository() {
List<MenuItem> menuItems = new ArrayList<>();
menuItems.add(new MenuItem("Kimchi"));
given(menuItemRepository.findAllByRestaurantId(1004L)).willReturn(menuItems);
}
private void mockRestaurantRespository() {
List<Restaurant> restaurants = new ArrayList<>();
Restaurant restaurant1 = new Restaurant(1004L, "Bob zip", "Seoul");
Restaurant restaurant2 = new Restaurant(2020L, "Bob2 zip", "Seoul");
restaurants.add(restaurant1);
restaurants.add(restaurant2);
given(restaurantRepository.findById(1004L)).willReturn(restaurant1);
given(restaurantRepository.findAll()).willReturn(restaurants);
}
@Test
public void getRestaurant() {
Restaurant restaurant = restaurantService.getRestaurant(1004L);
assertThat(restaurant.getId(), is(1004L));
MenuItem menuItem = restaurant.getMenuItems().get(0);
assertThat(menuItem.getName(), is("Kimchi"));
}
@Test
public void getRestaurants() {
List<Restaurant> restaurants = restaurantService.getRestaurants();
assertThat(restaurants.get(0).getId(), is(1004L));
}
}
반응형
'스프링, 자바' 카테고리의 다른 글
[패스트캠퍼스 수강 후기] 자바 인강 100% 환급 챌린지 45회차 미션 (0) | 2020.09.23 |
---|---|
[패스트캠퍼스 수강 후기] 자바 인강 100% 환급 챌린지 44회차 미션 (0) | 2020.09.22 |
[패스트캠퍼스 수강 후기] 자바 인강 100% 환급 챌린지 42회차 미션 (0) | 2020.09.20 |
[패스트캠퍼스 수강 후기] 자바 인강 100% 환급 챌린지 41회차 미션 (0) | 2020.09.19 |
[패스트캠퍼스 수강 후기] 자바 인강 100% 환급 챌린지 40회차 미션 (0) | 2020.09.18 |