일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
- 머신러닝
- 웹앱
- Req
- pds
- 크롤링
- java
- 정처기
- ensemble
- BS
- regressor
- list
- 자바스크립트
- 비전공자
- crawling
- 정보처리기사필기
- AWS
- dataframe
- SOUP
- lombok
- request
- 자바
- sklearn
- javascript
- 정보처리기사
- Intellij
- pandas
- springboot
- APPEND
- BeautifulSoup
- 백준
- Today
- Total
No sweet without sweat
[DAY 4] SpringBoot, AWS로 웹서비스 구현 <그 전 코드 롬복으로 전환> 본문
* 패키지 정리

1. HelloResponseDto 코드를 작성(HelloResponseDto)
package com.jojoldu.book.springboot.web.dto;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
@Getter
@RequiredArgsConstructor
public class HelloResponseDto {
private final String name;
private final int amount;
}
ㅁ Getter - 선언된 모든 필드의 get 메소드를 생성해줍니다.
ㅁ RequiredArgsConsturctor
- 선언된 모든 final 필드가 포함된 생서자를 생성해 줍니다.
- final이 없는 필드는 생성자에 포함되지 않습니다.
2. 테스트코드 작성(HelloResponseDtoTest)
package com.jojoldu.book.springboot.web.dto;
import org.junit.Test;
import static org.assertj.core.api.Assertions.assertThat;
public class HelloResponseDtoTest {
@Test
public void 롬복_기능_테스트(){
// given
String name = "test";
int amount = 1000;
// when
HelloResponseDto dto = new HelloResponseDto(name, amount);
// then
assertThat(dto.getName()).isEqualTo(name);
assertThat(dto.getAmount()).isEqualTo(amount);
}
}
ㅁ AssertThat
- assertj라는 테스트 검증 라이브러리의 검증 메소드
- 검증하고 싶은 대상을 메소드 인자로 받습니다.
- 메소드 체이닝이 지원되어 isEquaTo와 같이 메소드를 이어서 사용할 수 있습니다.
* Junit과 비교하여 assertj의 장점
- CoreMatchers와 달리 추가적으로 라이브러리가 필요하 지 않습니다.
- Junit의 assertThat을 쓰게 되면 is()와 같이 CoreMathcers 라이브러리가 필요합니다.
- 자동완성이 좀 더 확실하게 지원됩니다.
- IDE에서는 CoreMatchers와 같은 matcher 라이브러리의 자동완성 지원이 약합니다.
3. HelloController에다가 새로 만든 ReponseDto 추가
package com.jojoldu.book.springboot.web;
import com.jojoldu.book.springboot.web.dto.HelloResponseDto;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@GetMapping("/hello")
public String hello(){
return "hello";
}
@GetMapping("/hello/dto")
public HelloResponseDto helloDto(@RequestParam("name") String name, @RequestParam("amount") int amount) {
return new HelloResponseDto(name, amount);
}
}
ㅁ @RequestParam
- 외부에서 API로 넘긴 파라미터를 가져오는 어노테이션
- 여기서는 외부에서 name 이란 이름으로 넘긴 파라미터를 메소드 파라미터 name에 저장하게 됨.
4. HelloControllerTest에다가도 추가
package com.jojoldu.book.springboot.web;
import org.junit.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 static org.hamcrest.Matchers.is;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
@RunWith(value = SpringRunner.class)
@WebMvcTest(controllers = HelloController.class)
public class HelloControllerTest {
@Autowired
private MockMvc mvc;
@Test
public void hello가_리턴된다() throws Exception {
String hello = "hello";
mvc.perform(get("/hello"))
.andExpect(status().isOk())
.andExpect(content().string(hello));
}
@Test
public void helloDto가_리턴된다() throws Exception{
String name = "hello";
int amount = 1000;
mvc.perform(get("/hello/dto")
.param("name", name)
.param("amount", String.valueOf(amount)))
.andExpect(status().isOk())
.andExpect(jsonPath("$.name", is(name)))
.andExpect(jsonPath("$.amount", is(amount)));
}
}
ㅁ param
- API 테스트할 때 사용될 요청 파라미터를 설정
- 단, 값은 String만 허용
- 그래서 숫자/날짜 등의 데이터도 등록할 때는 문자열로 변경해야만 사용 가능
ㅁ jsonPath
- JSON 응답값을 필드별로 검증할 수 있는 메소드
- $를 기준으로 필드명을 명시
- 여기서는 name과 amount를 검증하니 $.name, $.amount로 검증
'SpringBoot + AWS 프로젝트' 카테고리의 다른 글
JPA - API를 만들기 (0) | 2022.06.26 |
---|---|
[DAY 5] SpringBoot, AWS로 웹서비스 구현 <롬복 설치> (0) | 2022.04.26 |
[DAY 3] SpringBoot, AWS로 웹서비스 구현 <TOD, 단위테스트 차이, API작성> (0) | 2022.04.24 |
[DAY 2] SpringBoot, AWS로 웹서비스 구현 <깃 커밋을 위한 환경 설정> (0) | 2022.04.04 |
[DAY 1] SpringBoot, AWS로 웹서비스 구현 <의존성 주입을 위한 환경 설정> (0) | 2022.04.03 |