[SpringBoot/AWS EC2] MongoDB - 3 Update

[SpringBoot/AWS EC2] MongoDB - 3 Update

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 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101

package com.example.springboot.web; import com.example.springboot.domain.posts.Posts; import com.example.springboot.domain.posts.PostsRepository; import com.example.springboot.web.dto.PostsSaveRequestDto; import com.example.springboot.web.dto.PostsUpdateRequestDto; 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.web.server.LocalServerPort; import org.springframework.http.HttpEntity; import org.springframework.http.HttpMethod; 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; @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 requestDto = PostsSaveRequestDto.builder() .title(title) .content(content) .author( "author" ) .build(); String url = "http://localhost:" + port + "/api/v1/posts" ; //when ResponseEntity < Long > responseEntity = restTemplate.postForEntity(url,requestDto,Long. class ); //then assertThat(responseEntity.getStatusCode()).isEqualTo(HttpStatus.OK); assertThat(responseEntity.getBody()).isGreaterThan(0L); List < Posts > all = postsRepository.findAll(); assertThat(all.get( 0 ).getTitle()).isEqualTo(title); assertThat(all.get( 0 ).getContent()).isEqualTo(content); } @Test public void Posts_수정() throws Exception{ //given Posts savedPosts = postsRepository.save(Posts.builder() .title( "title" ) .content( "content" ) .author( "author" ) .build()); Long updateId = savedPosts.getId(); String expectedTitle = "title2" ; String expectedContent = "content2" ; PostsUpdateRequestDto requestDto = PostsUpdateRequestDto.builder() .title(expectedTitle) .content(expectedContent) .build(); String url = "http://localhost:" + port + "/api/v1/posts" + updateId; 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 > all = postsRepository.findAll(); assertThat(all.get( 0 ).getTitle()).isEqualTo(expectedTitle); assertThat(all.get( 0 ).getContent()).isEqualTo(expectedContent); } } Colored by Color Scripter

from http://hjkongkong.tistory.com/13 by ccl(A) rewrite - 2021-12-12 22:02:27