package com.frombooktobook.frombooktobookbackend.controller.post;
import com.frombooktobook.frombooktobookbackend.domain.post.Post;
import com.frombooktobook.frombooktobookbackend.service.PostService;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
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 PostController {
private final PostService postService;
@PostMapping("/post")
public ResponseEntity<PostResponseDto> CreatePost(
@RequestBody PostCreateRequestDto requestDto) {
Post post = postService.savePost(requestDto.toEntity());
return ResponseEntity.ok()
.body(new PostResponseDto(post));
}
package com.frombooktobook.frombooktobookbackend.controller.post;
import com.frombooktobook.frombooktobookbackend.domain.post.Post;
import com.frombooktobook.frombooktobookbackend.domain.post.PostRepository;
import com.frombooktobook.frombooktobookbackend.service.PostService;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test;
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.web.server.LocalServerPort;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import java.util.List;
import static org.assertj.core.api.Assertions.assertThat;
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class PostControllerTest {
@LocalServerPort
private int port;
@Autowired
private TestRestTemplate restTemplate;
@Autowired
private PostRepository postRepository;
@AfterEach
public void tearDown() throws Exception {
postRepository.deleteAll();
}
@Test
public void post_저장() throws Exception{
//given
String bookName="test bookName";
String bookAuthor="test bookAuthor";
String content = "test content";
int rate = 5;
PostCreateRequestDto requestDto = PostCreateRequestDto.builder()
.bookName(bookName)
.bookAuthor(bookAuthor)
.title(null)
.content(content)
.rate(rate)
.build();
String url = "http://localhost:"+port+"/post";
// when
ResponseEntity<PostResponseDto> responseEntity = restTemplate
.postForEntity(url,requestDto,PostResponseDto.class);
//then
assertThat(responseEntity.getStatusCode()).isEqualTo(HttpStatus.OK);
List<Post> postList = postRepository.findAll();
Post post = postList.get(0);
assertThat(post.getBookName()).isEqualTo(bookName);
String expectedTitle = bookName+"을 읽고";
assertThat(post.getTitle()).isEqualTo(expectedTitle);
}
}
위의 코드로 테스트를 몇 번 해본 결과, 계속해서 테스트를 실패했다.
실패 사인은 HttpStatus 의 값이 200 ok 가 아니라 302 found가 발생한다는 것이었다.
뭐가 문젠지 모르겠어서 로직을 한참 들여다보며 이거 바꿔보고 저거 바꿔보고 하던 와중에,
다음과 같이 controller를 변경하자 테스트가 성공적으로 끝났다.
package com.frombooktobook.frombooktobookbackend.controller.post;
import com.frombooktobook.frombooktobookbackend.domain.post.Post;
import com.frombooktobook.frombooktobookbackend.service.PostService;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
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 PostController {
private final PostService postService;
@PostMapping("/post")
public ResponseEntity<PostResponseDto> CreatePost(
@RequestBody PostCreateRequestDto requestDto) {
try {
Post post = postService.savePost(requestDto.toEntity());
return ResponseEntity.ok()
.body(new PostResponseDto(post));
} catch(Exception e) {
System.out.println(e);
}
return ResponseEntity.ok()
.body(new PostResponseDto(requestDto.toEntity()));
}
}
오류 메세지가 뜰 것임을 예상하고 오류를 print하는 로직을 추가해본 것이었는데,
예상과 달리 테스트가 그냥 성공으로 끝나서 조금 당황스럽기는 했으나 . . . 어쨌든 오류가 해결되었으니 기쁘긴하다 . .
찝찝 . .
'Project > FromBookToBook' 카테고리의 다른 글
[FBTB] 3. 로그인 기능 구현 (with Oauth2) (0) | 2022.04.07 |
---|---|
[FBTB] 2. 독후감 목록 기능 구현 (0) | 2022.04.01 |
[FBTB] 1. 독후감 작성 기능 구현 (0) | 2022.03.30 |
[Error] UserControllerTest 중 Error (0) | 2022.03.28 |
spring - react , 백엔드-프론트엔드 연동하기 연습 (0) | 2022.03.27 |