API를 만들기전에 이전에 테스트할때 사용했던 @RunWith에 대해서 궁금증이 생겨서 작성하게되었다.
@SpringBootTest라는 어노테이션이 있는데 왜 @Runwith(SpringRunner.class)를 사용할까 알아보았는데 '@SpringBootTest를 사용하면 application context를 전부 로딩해서 자칫 잘못하면 무거운 프로젝트로서의 역할을 할 수 있습니다.'
라고합니다. 그래서 @SpringBootTest보다는 가볍고 필요한 조건에 맞춰서 진행할 수 있는 @Runwith(SpringRunner.class)를 사용합니다!
이전에 혼자서 만들어본 프로젝트에서도 등록/수정/조회 API를 만들었었는데 내가 스스로 만들었던 방법과 책에서 알려주는 방법의 차이를 기억하고 책으로 배우는 API에서는 조금 더 효율적으로 작성할 수 있는 방법을 배울 수 있다고 생각합니다!
등록을 하기위해 클래스 설정부터 해보겠습니다.
먼저 Entity클래스를 Request/Response 클래스로 사용하는 것을 절대로 안된다!
Entity클래스는 DB와 연결된 핵심 클래스이기때문에 요청/응답 처리는 Dto로 진행한다.
PostSaveRequestDto
package com.qkrtprjs.springbootproject.web.dto;
import com.qkrtprjs.springbootproject.domain.posts.Posts;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
@Getter
@NoArgsConstructor
public class PostsSaveRequestDto {
private String title;
private String content;
private String author;
@Builder
public PostsSaveRequestDto(String title, String content, String author) {
this.title = title;
this.content = content;
this.author = author;
}
public Posts toEntity() {
return Posts.builder()
.title(title)
.content(content)
.author(author)
.build();
}
}
PostApiController
package com.qkrtprjs.springbootproject.web;
import com.qkrtprjs.springbootproject.domain.posts.PostsRepository;
import com.qkrtprjs.springbootproject.service.posts.PostsService;
import com.qkrtprjs.springbootproject.web.dto.PostsSaveRequestDto;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
@RequiredArgsConstructor
@RestController
public class PostsApiController {
private final PostsService postsService;
@PostMapping("/api/v1/posts")
public Long save(@RequestBody PostsSaveRequestDto postsSaveRequestDto) {
return postsService.save(postsSaveRequestDto);
}
}
PostsService
package com.qkrtprjs.springbootproject.service.posts;
import com.qkrtprjs.springbootproject.domain.posts.PostsRepository;
import com.qkrtprjs.springbootproject.web.dto.PostsSaveRequestDto;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@RequiredArgsConstructor
@Service
public class PostsService {
private PostsRepository postsRepository;
@Transactional
public Long save(PostsSaveRequestDto postsSaveRequestDto) {
return postsRepository.save(postsSaveRequestDto.toEntity()).getId();
}
}
등록을 하기위한 서비스,컨트롤러, Dto를 만들어주었다면 테스트를 진행해본다!
테스트 진행
package com.qkrtprjs.springbootproject.web;
import com.qkrtprjs.springbootproject.domain.posts.Posts;
import com.qkrtprjs.springbootproject.domain.posts.PostsRepository;
import com.qkrtprjs.springbootproject.web.dto.PostsSaveRequestDto;
import org.junit.After;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.boot.test.web.server.LocalServerPort;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.test.context.junit4.SpringRunner;
import java.util.List;
import static org.assertj.core.api.Assertions.assertThat;
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class PostsApiControllerTest {
@LocalServerPort
private int port; //랜덤으로 지정된 포트 번호
@Autowired
private TestRestTemplate restTemplate; //JPA기능을 테스트하기위한, POST메서드 방법으로 테스트하기위한
@Autowired
private PostsRepository postsRepository;
@After
public void tearDown() throws Exception {
postsRepository.deleteAll();
}
@Test
public void Posts_등록된다() throws Exception {
//given
String title = "title";
String content = "content";
PostsSaveRequestDto postsSaveRequestDto = PostsSaveRequestDto.builder()
.title(title)
.content(content)
.author("author")
.build();
String url = "http://localhost:" + port + "/api/v1/posts"; //데이터를 보낼주소
//when
ResponseEntity<Long> responseEntity = restTemplate.postForEntity(url, postsSaveRequestDto, Long.class);
//post방법으로 url에 postsSaveRequestDto를 보내겠다 리턴타입은 Long
//then
assertThat(responseEntity.getStatusCode()).isEqualTo(HttpStatus.OK);
assertThat(responseEntity.getBody()).isGreaterThan(0L);
List<Posts> postsList = postsRepository.findAll();
assertThat(postsList.get(0).getTitle()).isEqualTo(title);
assertThat(postsList.get(0).getContent()).isEqualTo(content);
}
}
TestRestTemplate의 postForEntity 메서드를 사용해서 post방법으로 테스트
정상!
이제 수정과 조회도 테스트해보자
테스트 하기 이전에 클래스 설정
PostApiController 메서드추가
@PutMapping("/api/v1/posts/{id}")
public Long update(@PathVariable Long id, @RequestBody PostsUpdateRequestDto requestDto) {
return postsService.update(id, requestDto);
}
@GetMapping("/api/v1/posts/{id}")
public PostsResponseDto findById(@PathVariable Long id) {
return postsService.findById(id);
}
이전 토이프로젝트에서는 거의 모든 기능을 POST 방법으로 해결했는데 여기서는 PUT 방법을 사용한다!
PostUpdateRequestDto
package com.qkrtprjs.springbootproject.web.dto;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
@Getter
@Builder
@AllArgsConstructor
public class PostsUpdateRequestDto {
private String title;
private String content;
}
서비스 수정
@Transactional
public Long update(Long id, PostsUpdateRequestDto requestDto) {
Posts posts = postsRepository.findById(id).orElseThrow(() ->
new IllegalArgumentException("해당 게시글이 없습니다. id=" + id)
);
posts.update(requestDto.getTitle(), requestDto.getContent());
return id;
}
public PostsResponseDto findById(Long id) {
Posts posts = postsRepository.findById(id).orElseThrow(() -> new IllegalArgumentException("해당 게시글이 없습니다! id : " + id));
return new PostsResponseDto(posts);
}
위에 코드에서 update부분을 보면 update 쿼리를 날리지않은것을 볼 수 있다.
하지만 update 쿼리를 날리지않았지만 update 작업을 수행합니다. 그 이유는 바로 영속성 컨텍스트 때문입니다. 트랜잭션 안에서 db에 있는 데이터를 접근해서 갖고온다면 이 데이터는 영속성 컨텍스트에 저장된다고 말하고 상태를 기억하게됩니다! 그래서 이 가져온 데이터 값이 변경된다면 트랜잭션이 끝나는 지점에서 변경상태를 확인하고 변경되었다면 자동으로 반영이 되게 하는 것입니다! 이를 '더티체킹' 이라고 합니다!
만들어 놓은 클래스로 테스트 진행
@Test
public void posts_수정된다() {
//given
//수정시킬 entity 저장
Posts posts = postsRepository.save(
Posts.builder()
.title("title")
.content("content")
.author("author")
.build()
);
//수정시킬 정보
Long updateId = posts.getId();
String expectedTitle = "title2";
String expectedContent = "content2";
//수정을 위한 dto 설정
PostsUpdateRequestDto requestDto = PostsUpdateRequestDto.builder()
.title(expectedTitle)
.content(expectedContent)
.build();
//요청할 주소
String url = "http://localhost:" + port + "/api/v1/posts/" + updateId;
//요청할 주소로 보낼 httpEntity 객체
HttpEntity<PostsUpdateRequestDto> requestEntity = new HttpEntity<>(requestDto);
//when
//요청을 보냈을때에
ResponseEntity<Long> responseEntity = restTemplate.exchange(url, HttpMethod.PUT, requestEntity, Long.class);
//then
assertThat(responseEntity.getStatusCode()).isEqualTo(HttpStatus.OK);
assertThat(responseEntity.getBody()).isGreaterThan(0L);
List<Posts> postsList = postsRepository.findAll();
assertThat(postsList.get(0).getTitle()).isEqualTo(expectedTitle);
assertThat(postsList.get(0).getContent()).isEqualTo(expectedContent);
}
더티 체킹 update 테스트 정상적으로 성공
조회 테스트는 직접 SpringApplication을 실행시켜서 확인해보자
h2 console을 이용해서 insert쿼리를 하나 날려준다음에 http://localhost:8080/api/v1/posts/1
로 접속한다면 id가 1 인 posts 의 정보가 확인될것이다.
완료!
보통 엔티티는 해당 데이터의 생성시간과 수정시간을 포함한다. 언제 만들어졌는지 ,언제 수정되었는지는 차후 유지보수에 있어서 굉장히 중요한 정보이기 때문이다. 매번 DB에 삽입하기전 갱신 하기전에 날짜 데이터를 등록/수정 하는 코드가 여기저기 들어가게되는데 단순하고 반복적인 코드가 메소드에 포함된다면 너무나 귀찮고 지저분해지기때문에 이를 해결하기위해 JPA Auditing을 사용해보자.
BaseTimeEntity 생성
package com.qkrtprjs.springbootproject.domain;
import lombok.Getter;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;
import javax.persistence.EntityListeners;
import javax.persistence.MappedSuperclass;
import java.time.LocalDate;
import java.time.LocalDateTime;
@Getter
@MappedSuperclass //@Entity 설정이붙은 클래스들이 상속받을경우 칼럼으로 인식하도록
@EntityListeners(AuditingEntityListener.class) //auditing기능 포함
//@MappedSupderClass를 적요이킨 클래스를 단독으로 사용될 일이 없기때문에 abstract를 추가해서 추상클래스로 만들어준다.
//추상클래스는 선언해놓고 자식클래스에서 메서드를 완성하도록 유도하는 클래스이다.
public abstract class BaseTimeEntity {
@CreatedDate
private LocalDateTime createdDate;
@LastModifiedDate
private LocalDateTime modifiedDate;
}
@MappedSupderClass를 적요이킨 클래스를 단독으로 사용될 일이 없기때문에 abstract를 추가해서 추상클래스로 만들어준다.
추상클래스는 선언해놓고 자식클래스에서 메서드를 완성하도록 유도하는 클래스이다.
기능이 잘 작동하는지 테스트
@Test
public void BaseTimeEntity_등록() {
//given
LocalDateTime now = LocalDateTime.of(2023, 8, 29, 0, 0, 0);
postsRepository.save(Posts.builder()
.title("title")
.content("content")
.author("author")
.build());
//when
List<Posts> all = postsRepository.findAll();
//then
Posts posts = all.get(0);
System.out.println(posts);
System.out.println(posts.getCreatedDate());
System.out.println(posts.getModifiedDate());
assertThat(posts.getCreatedDate()).isAfter(now);
assertThat(posts.getModifiedDate()).isAfter(now);
}
정상적으로 성공!