Created
February 28, 2024 01:35
-
-
Save msbaek/64c5a058661214fb06a710c5d139886c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) | |
| @Transactional | |
| class PostControllerTest extends AbstractTestContainerTest { | |
| @LocalServerPort | |
| int port; | |
| ObjectMapper objectMapper = new ObjectMapper(); | |
| @Autowired | |
| private PostRepfpsository postRepository; | |
| @NotNull | |
| private String hostAndPort() { | |
| return "http://localhost:" + port; | |
| } | |
| @Test | |
| void getAll() { | |
| http.get(hostAndPort() + "/api/posts", (header, body) -> { | |
| header.statusCode.should(equal(200)); | |
| assertThat(body.numberOfElements()).isEqualTo(100); | |
| }); | |
| } | |
| @Test | |
| void shouldFindPostWhenValidPostID() { | |
| http.get(hostAndPort() + "/api/posts/1", (header, body) -> { | |
| header.statusCode.should(equal(200)); | |
| body.get("id").should(equal(1)); | |
| }); | |
| } | |
| @Test | |
| void shouldThrowNotFoundWhenInvalidPostID() { | |
| http.get(hostAndPort() + "/api/posts/201", (header, body) -> { | |
| header.statusCode.should(equal(404)); | |
| }); | |
| } | |
| @Test | |
| void shouldCreateNewPostWhenPostIsValid() { | |
| HttpRequestBody post = http.json( | |
| "id", "101", | |
| "userId", "1", | |
| "body", "101 Body", | |
| "title", "101 Title" | |
| ); | |
| http.post(hostAndPort() + "/api/posts", post, ((header, body) -> { | |
| header.statusCode.should(equal(HttpStatus.CREATED_201)); | |
| body.get("id").should(equal(101)); | |
| })); | |
| } | |
| @Test | |
| void shouldUpdatePostWhenPostIsValid() { | |
| Post post = http.get(hostAndPort() + "/api/posts/1", (header, body) -> { | |
| header.statusCode.should(equal(200)); | |
| return jsonToObject(body.getTextContent(), Post.class); | |
| }); | |
| Post updated = new Post(post.getId()) | |
| .setUserId(post.getUserId()) | |
| .setBody("NEW POST BODY #1") | |
| .setTitle("NEW POST TITLE #1"); | |
| http.put(hostAndPort() + "/api/posts/1", http.json(toJsonString(updated)), (header, body) -> { | |
| header.statusCode.should(equal(200)); | |
| body.get("id").should(equal(1)); | |
| body.get("body").should(equal("NEW POST BODY #1")); | |
| body.get("title").should(equal("NEW POST TITLE #1")); | |
| }); | |
| } | |
| @Test | |
| void shouldDeleteWithValidID() { | |
| http.delete(hostAndPort() + "/api/posts/88", (header, body) -> { | |
| header.statusCode.should(equal(HttpStatus.NO_CONTENT_204)); | |
| }); | |
| postRepository.findById(88).ifPresent(post -> { | |
| fail("Post should be deleted"); | |
| }); | |
| } | |
| private String toJsonString(Post updated) { | |
| try { | |
| return objectMapper.writeValueAsString(updated); | |
| } catch (IOException e) { | |
| throw new RuntimeException(e); | |
| } | |
| } | |
| private Post jsonToObject(String textContent, Class<Post> valueType) { | |
| try { | |
| return objectMapper.readValue(textContent, valueType); | |
| } catch (IOException e) { | |
| throw new RuntimeException(e); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment