on
[SpringBoot/AWS EC2] MongoDB - 2 등록
[SpringBoot/AWS EC2] MongoDB - 2 등록
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
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 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.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); } } Colored by Color Scripter
from http://hjkongkong.tistory.com/12 by ccl(A) rewrite - 2021-12-09 23:01:12