공부한것들을 정리하는 블로그 입니다.
2. 가게목록 조회(REST API 및 단위테스트 실습) 본문
반응형
REST API 실습 예제를 작성 후, Junit 을 이용하여 단위테스트를 실행해보자.
0. 구성도
1. WelcomeController 클래스를 생성 후 코드입력
package kr.com.cyh.RRS.interfaces;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class WelcomeController {
@GetMapping("/")
public String hello() {
return "Hello, World!!";
}
}
2. RestaurantReservationSystemApplication 을 실행
package kr.com.cyh.RRS;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class RestaurantReservationSystemApplication {
public static void main(String[] args) {
SpringApplication.run(RestaurantReservationSystemApplication.class, args);
}
}
3. 웹페이지에서 확인
4. RestaurantController 추가
package kr.com.cyh.RRS.interfaces;
import kr.com.cyh.RRS.domain.Restaurant;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.ArrayList;
import java.util.List;
@RestController
public class RestaurantController {
@GetMapping("/restaurants")
public List<Restaurant> list() {
List<Restaurant> restaurants = new ArrayList<>();
Restaurant restaurant = new Restaurant("Bob zip", "Seoul", 1004L);
restaurants.add(restaurant);
return restaurants;
}
}
5. Restaurant 추가
package kr.com.cyh.RRS.domain;
import org.springframework.web.bind.annotation.RestController;
//@RestController
public class Restaurant {
private final String address;
private final String name;
private final long id;
public Restaurant(String name, String address, long id) {
this.name = name;
this.address = address;
this.id = id;
}
public String getName() {
return name;
}
public String getAddress() {
return address;
}
public String getInformation() {
return name + " in " + address;
}
public Long getId() {
return id;
}
}
6. RestaurantControllerTest 추가 및 단위테스트 실행
package kr.com.cyh.RRS.interfaces;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import sun.tools.jconsole.JConsole;
import static org.hamcrest.Matchers.containsString;
import static org.junit.Assert.*;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
@RunWith(SpringRunner.class)
@WebMvcTest(RestaurantController.class)
public class RestaurantControllerTest {
@Autowired
private MockMvc mvc;
@Test
public void test() throws Exception {
mvc.perform(get("/restaurants"))
.andExpect(status().isOk())
.andExpect(content().json( //json array
"[{\"address\":\"Seoul\",\"name\":\"Bob zip\",\"id\":1004,\"information\":\"Bob zip in Seoul\"}]"
));
// .andExpect(content().json(
// //json object
// "{'data':[{'useRegEx':'false','hosts':'v2v2v2'}]}"
// ));
}
@Test
public void list() throws Exception {
mvc.perform(get("/restaurants"))
.andExpect(status().isOk())
.andExpect(content().string(
containsString("\"id\":1004")
))
.andExpect(content().string(
containsString("\"name\":\"Bob zip\"")
));
}
}
7. RestaurantTests 추가 및 단위테스트 실행
package kr.com.cyh.RRS.domain;
import org.junit.jupiter.api.Test;
import static org.hamcrest.core.Is.is;
import static org.junit.Assert.*;
public class RestaurantTests {
@Test
public void creation() {
Restaurant restaurant = new Restaurant("Bob zip", "Seoul", 1004L);
assertThat(restaurant.getId(), is(1004L));
assertThat(restaurant.getName(), is("Bob zip"));
assertThat(restaurant.getAddress(), is("Seoul"));
}
@Test
public void information() {
Restaurant restaurant = new Restaurant("Bob zip", "Seoul", 1004L);
assertThat(restaurant.getInformation(), is("Bob zip in Seoul"));
}
}
8. 웹페이지에서 확인
반응형
'(2020) 사이드 프로젝트 > RRS(게시판-SpringBoot)' 카테고리의 다른 글
3. 상세 조회(Repository, RepositoryImpl 추가) (0) | 2020.01.09 |
---|---|
1. 프로젝트 생성 (0) | 2020.01.09 |
0. RRS(Restaurant-Reservation-System) 설정 (0) | 2020.01.09 |
Comments