티스토리 뷰
Lombok은 Getter, Setter, 기본생성자, toString 등을 어노테이션으로 자동 생성해 준다.
1. IntelliJ Plugins에서 Lombok을 설치한다 (shift + command + A)
2. build.gradle의 dependencies에 lombok을 추가해준다.
dependencies {
compile('org.springframework.boot:spring-boot-starter-web')
testCompile('org.springframework.boot:spring-boot-starter-test')
compile('org.projectlombok:lombok') // 추가
}
위에 처럼 추가를 해준다
오른쪽의 gradle 메뉴에서 새로고침을 한다
3. Enable annotation processing을 체크한다
IntelliJ IDEA > Preferences > Build, Extension, Deployment > Compiler > Annotation Processor로 간다.
그럼 위의 사진과 같이 Enable annotation processing 항목이 보이고 이를 체크해주면 된다.
1은 한 번 하면 되지만 2와 3은 프로젝트마다 설정해주어야 한다.
import lombok.Getter;
import lombok.RequiredArgsConstructor;
@Getter
@RequiredArgsConstructor
public class HelloResponseDto {
private final String name;
private final int amount;
}
ㄴ lombok을 사용한 코드
@Getter
- 선언된 모든 필드의 get 메소드를 생성해준다
@RequiredArgsConstructor
- 선언된 모든 final 필드가 포함된 생성자를 생성해 준다
- final이 없는 필드는 생성자에 포함되지 않는다
💡 final
final이 붙은 변수는 변경이 불가능하나, setter 또는 생성자로 값을 변경할 수 있다.
import org.junit.Test;
import static org.assertj.core.api.Assertions.assertThat;
public class HelloResponseDtoTest {
@Test
public void 롬복_기능_테스트() {
// give
String name = "test";
int amount = 1000;
// when
HelloResponseDto dto = new HelloResponseDto(name, amount);
// then
assertThat(dto.getName()).isEqualTo(name);
assertThat(dto.getAmount()).isEqualTo(amount);
}
}
command + shift + T: IntelliJ에서 테스트를 빠르게 만들어 주는 단축키
assertThat
- 테스트 검증 라이브러리 assetj의 테스트 검증 메소드
- 인자: 검증하고 싶은 대상
- 메소드 체이닝 가능
isEqualTo
- assetj의 동등 비교 메소드
- assertThat의 값과 isEqualTo의 값이 같으면 테스트 통과
assertJ를 사용했는데 AssertJ가 JUnit의 assertThat 보다 편리한 이유를 보면 이해할 수 있다.
@GetMapping("/hello/dto")
public HelloResponseDto helloDto(@RequestParam("name") String name, @RequestParam("amount") int amount) {
return new HelloResponseDto(name, amount);
}
HelloResponseDto를 사용하기 위한 코드이다.
@GetMapping
- 외부에서 API로 넘긴 파라미터를 가져오는 어노테이션
- 외부에서 name이란 이름으로 넘긴 파라미터(@RequestParam("name")를 메소드 파라미터 name(String)에 저장
@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만 가능
- String 외의 숫자/날짜 등의 데이터는 등록할 때 문자열로 변경
jsonPath
- JSON을 응닶값을 필드별로 검증할 수 있는 메소드
- $를 기준으로 필드명 명시
JSON이 리턴되는 API의 테스트가 통과하는 것을 확인할 수 있었다.
- Total
- Today
- Yesterday
- go 특징
- mysql
- github
- python3
- git
- 네이버 2022 공채
- graphql
- 1차 인터뷰
- 코틀린
- squash merge
- solidity
- 확장 함수
- 주생성자
- 코딩테스트
- Python
- string
- postman
- postman tests
- Basic Type
- downTo
- DGS Framework
- pm.test
- java
- Squash and merge
- postman collection
- 2차 인터뷰
- Kotlin In Action
- pm.expect
- hashcode
- Kotlin
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |