[Spring] @PostMapping 예제

[Spring] @PostMapping 예제

ControllerTest.java

@RunWith(SpringRunner.class) @WebMvcTest(ItemController.class) public class ItemControllerTests { @Autowired private MockMvc mvc; @MockBean private ItemService itemService; @Test public void create() throws Exception { mvc.perform(post("/items")) .andExpect(status().isCreated()) .andExpect(header().string("location", "/items/1234")); } }

빈칸에 우측 클릭 후 아래와 같이 선택

Controller.java

1) post 코드 작성

@RestController public class ItemController { @Autowired private ItemService itemService; @PostMapping("/items") public ResponseEntity create() throws URISyntaxException { URI location = new URI("/items/1000"); return ResponseEntity.created(location).body(""); } }

2) 테스트 코드

@PostMapping("/items") public ResponseEntity create() throws URISyntaxException { String name = "post1000"; String address = "postadd"; Item item = new Item(1000L, name, address); itemService.addItem(item); URI location = new URI("/items/"+item.getId()); return ResponseEntity.created(location).body("{}"); }

3)

@PostMapping("/items") public ResponseEntity create(@RequestBody Item resource) throws URISyntaxException { String name = "post1000"; String address = "postadd"; Item item = new Item(1000L, name, address); itemService.addItem(item); URI location = new URI("/items/"+item.getId()); return ResponseEntity.created(location).body("{}"); }

Item 은 데이터 모델이다.

아래는 모델 코드이다.

import jdk.internal.jimage.ImageStrings; import java.awt.*; import java.util.ArrayList; import java.util.List; public class Item { private String name; private String address; private Long id; private List menuItems =new ArrayList(); public Item() { } public Item(Long id, String name, String address) { this.id = id; this.name =name; this.address= address; } }

constructor 추가시 final 때문에 에러표시가 뜨는데 not final을 선택해서 에러를 없앤다.

ControllerTest.java

1) get할때는 mvc.perform 앞 쪽에 given을 썼었는데 post는 verify를 사용

@Test public void create() throws Exception { Object item= new Item(1000L, "post1000", "postadd"); itemService.addItem(item); mvc.perform(post("/items")) .andExpect(status().isCreated()) .andExpect(header().string("location", "/items/1000")) .andExpect(content().string("{}")); verify(itemService).addItem(item); }

2) item 부분을 빼고 아래처럼 테스트할 수도 있다.

@Test public void create() throws Exception { mvc.perform(post("/items")) .andExpect(status().isCreated()) .andExpect(header().string("location", "/items/1000")) .andExpect(content().string("{}")); verify(itemService).addItem(any()); }

3) json 설정 추가

@Test public void create() throws Exception { mvc.perform(post("/items") .contentType(MediaType.APPLICATION_JSON) .content("{\"name\":\"post1000\",\"address\":\"post1000\"}")) .andExpect(status().isCreated()) .andExpect(header().string("location", "/items/1000")) .andExpect(content().string("{}")); verify(itemService).addItem(any()); }

httpie로 테스트

from http://batzero.tistory.com/17 by ccl(A) rewrite - 2020-03-24 03:54:21

댓글

이 블로그의 인기 게시물

데이터 바인딩 추상화 - propertyEditor

[sts] spring boot groovy 적용 실행 하기

2020 LCK 롤챔스 Spring 경기 재개 및 일정