Spring Test 2022/04/19
파일명 ProductRestControllerTest.java
package com.example;
import static org.assertj.core.api.Assertions.assertThat;
import com.example.entity.ProductCountEntity;
import com.example.entity.ProductEntity;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;
public class ProductRestControllerTest {
RestTemplate restTemplate;
@BeforeEach
public void setup() {
restTemplate = new RestTemplate();
}
// http://127.0.0.1:9090/ROOT/api/product/insert.json
// @ModelAttribute
@Test
public void insertTest(){
// 전송하고자 하는 값 생성
MultiValueMap<String, Object> body = new LinkedMultiValueMap<>();
body.add("name", "테스트용");
body.add("price", 1234L);
// header생성
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
HttpEntity<MultiValueMap<String, Object>> entity = new HttpEntity<>(body, headers);
ResponseEntity<String> result = restTemplate.exchange("http://127.0.0.1:9090/ROOT/api/product/insert.json",
HttpMethod.POST, entity, String.class);
assertThat(result.getStatusCode().toString()).isEqualTo("200 OK");
}
// http://127.0.0.1:9090/ROOT/api/product/update.json
// @RequestBody
@Test
public void updateTest(){
// 전송하고자 하는 값 생성
ProductEntity product = new ProductEntity();
product.setNo(1004L);
product.setName("가나다라");
product.setPrice(9999L);
// header생성
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
HttpEntity<ProductEntity> entity
= new HttpEntity<>(product, headers);
ResponseEntity<String> result
= restTemplate.exchange("http://127.0.0.1:9090/ROOT/api/product/update.json",
HttpMethod.PUT, entity, String.class);
assertThat(result.getStatusCode().toString()).isEqualTo("200 OK");
}
// 127.0.0.1:9090/ROOT/api/productcount/insert.json
// @RequestBody
// {cnt : 12, type:"I" , product:{no:1007}}
@Test
public void InsertProductCount(){
ProductCountEntity pcount = new ProductCountEntity();
pcount.setCnt(-2L);
pcount.setType("O");
ProductEntity product = new ProductEntity();
product.setNo(1018L); //1007번 물품
pcount.setProduct(product);
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
HttpEntity<ProductCountEntity> entity
= new HttpEntity<>(pcount, headers);
ResponseEntity<String> result
= restTemplate.exchange("http://127.0.0.1:9090/ROOT/api/productcount/insert.json",
HttpMethod.POST, entity, String.class);
System.out.println("===== result.getBody() =====");
System.out.println(result.getBody());
// assertThat(result.getStatusCode().toString()).isEqualTo("200 OK");
assertThat(result.getBody().toString()).contains("\"status\":200");
}
// 재고 수량 조회
// 127.0.0.1:9090/ROOT/api/productcount/insert.json
// @RequestBody
@Test
public void selectCount(){
String url = "http://127.0.0.1:9090/ROOT/api/productcount/selectcount.json?no=1007";
String result = restTemplate.getForObject(url,String.class);
System.out.println("===== result =====");
System.out.println(result);
assertThat(result).contains("\"status\":200");
}
}
Author And Source
이 문제에 관하여(Spring Test 2022/04/19), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://velog.io/@anrkfl/20220419-Spring-Test저자 귀속: 원작자 정보가 원작자 URL에 포함되어 있으며 저작권은 원작자 소유입니다.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)