Notice
Recent Posts
Recent Comments
Link
«   2025/08   »
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31
Tags
more
Archives
Today
Total
관리 메뉴

devNyong

[ SpringBoot ] api test , jpa test 코드 본문

springboot

[ SpringBoot ] api test , jpa test 코드

devNyong 2022. 5. 18. 17:55

 

@RunWith(SpringRunner.class) // 스프링부트테스트와 junit 사이의 연결자 기능
@ActiveProfiles("local")
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class ApiControllerTest {

  @LocalServerPort
  private int port;

  @Autowired
  private TestRestTemplate restTemplate;

  @Autowired
  private Repository repository;

  @Test
  public void insert() {
    String url = "http://localhost:" + port + "/api/v1/test";
    ResponseEntity<Long> responseEntity = restTemplate.postForEntity(url, createData(), Long.class);
    assertThat(responseEntity.getStatusCode(), equalTo(HttpStatus.OK));
  }

  @Test
  public void update() {
    String url = "http://localhost:" + port + "/api/v1/test";
    HttpEntity<ProjectDTO> requestEntity = new HttpEntity<ProjectDTO>(updateData(idx));
    ResponseEntity<Long> responseEntity = restTemplate.exchange(url, HttpMethod.PUT, requestEntity, Long.class);
    assertThat(responseEntity.getStatusCode(), equalTo(HttpStatus.OK));
  }

  @Test
  public void selectOne() {
    String url = "http://localhost:" + port + "/api/v1/test/" + idx;
    ResponseEntity<Object> responseEntity = restTemplate.getForEntity(url, Object.class);
    assertThat(responseEntity.getStatusCode(), equalTo(HttpStatus.OK));
  }

  @Test
  public void selectAll() {
	String url = "http://localhost:" + port + "api/v1/test/list?page=0&size=5&sort=idx,desc";
    SearchCriteriaDTO searchDTO = new SearchCriteriaDTO();
    searchDTO.setKey("serviceNm");
    searchDTO.setValue("service");
    searchDTO.setOperator("like");
    List<SearchCriteriaDTO> list = new ArrayList<SearchCriteriaDTO>();
    list.add(searchDTO);
    TestDTO reqDTO = new TestDTO();
    reqDTO.setSearchCriterias(list);
    ResponseEntity<Object> responseEntity = restTemplate.postForEntity(url, reqDTO, Object.class);
    assertThat(responseEntity.getStatusCode(), equalTo(HttpStatus.OK));
  }

  @Test
  public void delete() {
	HttpHeaders httpHeaders = new HttpHeaders();
    HttpEntity<String> httpEntity = new HttpEntity<String>(httpHeaders);
    String url = "http://localhost:" + port + "/api/v1/test/" + pidx;
    ResponseEntity<Long> responseEntity = restTemplate.exchange(url, HttpMethod.DELETE, httpEntity, Long.class);
  }
}

'springboot' 카테고리의 다른 글

[Oracle] mybatis 날짜 조회  (0) 2022.10.07
스프링 서버 경로 구하기  (0) 2022.06.15
Rest Api Exception 처리 ExceptionAdvice  (0) 2022.05.31
Comments