{"id":338,"date":"2020-03-28T21:16:48","date_gmt":"2020-03-28T12:16:48","guid":{"rendered":"https:\/\/www.skyer9.pe.kr\/wordpress\/?p=338"},"modified":"2020-12-27T16:25:44","modified_gmt":"2020-12-27T07:25:44","slug":"from-hello-to-querydsl-spring-boot-test-12-12","status":"publish","type":"post","link":"https:\/\/www.skyer9.pe.kr\/wordpress\/?p=338","title":{"rendered":"[From Hello To QueryDSL] Spring Boot Test (12\/12)"},"content":{"rendered":"<h1>Spring Boot Test<\/h1>\n<p>JUnit 4.x \uae30\uc900\uc73c\ub85c \uc791\uc131\ud569\ub2c8\ub2e4.<\/p>\n<h2>\uac1c\ubc1c\ud658\uacbd<\/h2>\n<ul>\n<li>Spring Boot 2.1.x<\/li>\n<li>Gradle 4.10.2<\/li>\n<li>junit 4.12<\/li>\n<\/ul>\n<h2>build.gradle \uc218\uc815<\/h2>\n<p>build.gradle<\/p>\n<pre><code class=\"language-configuration\">......\ndependencies {\n    \/\/ ......\n    testCompile(&quot;org.springframework.security:spring-security-test&quot;)\n    \/\/ ......\n}\n......<\/code><\/pre>\n<h2>\ud14c\uc2a4\ud2b8\uc6a9 application.properties \uc0dd\uc131<\/h2>\n<p>src\/test\/resources\/application.properties<\/p>\n<pre><code class=\"language-configuration\"># \ube48(empty) \ud30c\uc77c\ub85c \uc0dd\uc131\ud569\ub2c8\ub2e4.\n# \ud30c\uc77c\uc774 \ube44\uc5b4\uc788\uc73c\uba74 \uc2a4\ud504\ub9c1\uc740 \uc790\ub3d9\uc73c\ub85c h2 \ub97c \ub370\uc774\ud0c0\ubca0\uc774\uc2a4\ub85c \uc124\uc815\ud569\ub2c8\ub2e4.<\/code><\/pre>\n<p>\ud30c\uc77c\uc744 \uc0dd\uc131\ud558\uc9c0 \uc54a\uace0 <code>@AutoConfigureTestDatabase<\/code> \ub97c \uc774\uc6a9\ud574 \uc6d0\ub798\uc758 DB \ub97c \uc0ac\uc6a9\ud560 \uc218 \uc788\uc2b5\ub2c8\ub2e4.<\/p>\n<h2>given-when-then<\/h2>\n<p>\ud14c\uc2a4\ud2b8 \ubc29\ubc95\uc740 <code>given-when-then<\/code> \uae30\ubc95\uc744 \uc0ac\uc6a9\ud569\ub2c8\ub2e4.<\/p>\n<p><code>given<\/code> \ud30c\ud2b8\uc5d0\uc11c\ub294 \ud14c\uc2a4\ud2b8\ub97c \uc704\ud55c \uae30\ucd08\ud658\uacbd\uc744 \uad6c\uc131\ud569\ub2c8\ub2e4.<\/p>\n<p><code>when<\/code> \uc5d0\uc11c \ud14c\uc2a4\ud2b8\ucf54\ub4dc\ub97c \uc791\uc131\ud558\uace0, <code>then<\/code> \uc5d0\uc11c \ud14c\uc2a4\ud2b8\uacb0\uacfc\ub97c \uac80\uc99d\ud569\ub2c8\ub2e4.<\/p>\n<h2>\ub3c4\uba54\uc778 \ub808\uc774\uc5b4 \ud14c\uc2a4\ud2b8\ud558\uae30<\/h2>\n<p>src\/test\/java\/kr\/co\/episode\/example\/domain\/posts\/PostsRepositoryTest.java<\/p>\n<pre><code class=\"language-java\">@RunWith(SpringRunner.class)\n@DataJpaTest\n\/\/ @AutoConfigureTestDatabase(replace= AutoConfigureTestDatabase.Replace.NONE)\npublic class PostsRepositoryTest {\n\n    @Autowired\n    private PostsRepository postsRepository;\n\n    @Test\n    public void save() {\n\n        \/\/ given\n        Posts posts = Posts.builder()\n                .title(&quot;title&quot;)\n                .author(&quot;author&quot;)\n                .content(&quot;content&quot;)\n                .build();\n\n        \/\/ when\n        Long id = postsRepository.save(posts).getId();\n\n        \/\/ then\n        Optional&lt;Posts&gt; saved = postsRepository.findById(id);\n        assertTrue(saved.isPresent());\n        assertEquals(saved.get().getTitle(), &quot;title&quot;);\n        assertEquals(saved.get().getAuthor(), &quot;author&quot;);\n        assertEquals(saved.get().getContent(), &quot;content&quot;);\n    }\n\n    @Test\n    public void update() {\n\n        \/\/ given\n        Long id = saveOnePost();\n        Optional&lt;Posts&gt; saved = postsRepository.findById(id);\n\n        \/\/ when\n        assertTrue(saved.isPresent());\n        saved.get().update(&quot;title2&quot;, &quot;content2&quot;);\n        Optional&lt;Posts&gt; updated = postsRepository.findById(id);\n\n        \/\/ then\n        assertTrue(updated.isPresent());\n        assertEquals(updated.get().getTitle(), &quot;title2&quot;);\n        assertEquals(updated.get().getContent(), &quot;content2&quot;);\n    }\n\n    @Test\n    public void findById() {\n\n        \/\/ given\n        Long id = saveOnePost();\n\n        \/\/ when\n        Optional&lt;Posts&gt; saved = postsRepository.findById(id);\n\n        \/\/ then\n        assertTrue(saved.isPresent());\n        assertEquals(saved.get().getTitle(), &quot;title&quot;);\n        assertEquals(saved.get().getAuthor(), &quot;author&quot;);\n        assertEquals(saved.get().getContent(), &quot;content&quot;);\n    }\n\n    @Test\n    public void delete() {\n\n        \/\/ given\n        Long id = saveOnePost();\n\n        \/\/ when\n        postsRepository.deleteById(id);\n\n        \/\/ then\n        Optional&lt;Posts&gt; deleted = postsRepository.findById(id);\n        assertTrue(deleted.isEmpty());\n    }\n\n    @Test\n    public void findAllDesc() {\n\n        \/\/ given\n        Long id = saveOnePost();\n\n        \/\/ when\n        List&lt;Posts&gt; postsList = postsRepository.findAllDesc();\n\n        \/\/ then\n        assertEquals(postsList.size(), 1);\n    }\n\n    private Long saveOnePost() {\n        Posts posts = Posts.builder()\n                .title(&quot;title&quot;)\n                .author(&quot;author&quot;)\n                .content(&quot;content&quot;)\n                .build();\n        return postsRepository.save(posts).getId();\n    }\n}<\/code><\/pre>\n<p>src\/test\/java\/kr\/co\/episode\/example\/domain\/posts\/PostsPagingRepositoryTest.java<\/p>\n<pre><code class=\"language-java\">@RunWith(SpringRunner.class)\n@DataJpaTest\npublic class PostsPagingRepositoryTest {\n\n    @Autowired\n    private PostsRepository postsRepository;\n\n    @Autowired\n    private PostsPagingRepository postsPagingRepository;\n\n    @Test\n    public void search() {\n\n        \/\/ given\n        PostsSearchDto postsSearchDto = PostsSearchDto.builder()\n                .title(&quot;title&quot;)\n                .author(&quot;author&quot;)\n                .orderBy(false)\n                .build();\n\n        Pageable pageable = PageRequest.of(0, 10);\n\n        ArrayList&lt;Posts&gt; postsArrayList = new ArrayList&lt;&gt;();\n        for (int i = 0; i &lt; 10; i++) {\n            Posts post = Posts.builder()\n                    .title(&quot;title&quot; + 1)\n                    .author(&quot;author&quot;)\n                    .content(&quot;content&quot;)\n                    .build();\n\n            postsRepository.save(post);\n            postsArrayList.add(post);\n        }\n\n        \/\/ when\n        Page&lt;Posts&gt; posts = postsPagingRepository.search(pageable, postsSearchDto.toEntity());\n\n        \/\/ then\n        for (int i = 0; i &lt; 1; i++) {\n            String a = posts.getContent().get(i).getTitle();\n            String b = postsArrayList.get(i).getTitle();\n            assertEquals(a, b);\n        }\n    }\n}<\/code><\/pre>\n<h2>\ucee8\ud2b8\ub864\ub7ec \ud14c\uc2a4\ud2b8\ud558\uae30<\/h2>\n<h3>\ud14c\uc2a4\ud2b8\uc6a9 application.properties \uc218\uc815<\/h3>\n<p>\uc2e4\uc81c\ub85c \uc811\uc18d\ud558\uc9c0\ub294 \uc54a\uc73c\ubbc0\ub85c client-id\/client-secret \uc740 \uc544\ubb34 \uac12\uc744 \uc785\ub825\ud574\ub3c4 \ub429\ub2c8\ub2e4.<\/p>\n<p>src\/test\/resources\/application.properties<\/p>\n<pre><code class=\"language-configuration\">spring.security.oauth2.client.registration.google.client-id=aaaa\nspring.security.oauth2.client.registration.google.client-secret=bbbb\nspring.security.oauth2.client.registration.google.scope=profile,email<\/code><\/pre>\n<h3>API \ucee8\ud2b8\ub864\ub7ec \ud14c\uc2a4\ud2b8\ud558\uae30<\/h3>\n<p>\uc2e4\uc81c\ub85c RestController \ub97c \ub79c\ub364\ud3ec\ud2b8\ub85c \uc2e4\ud589\ud558\uace0, <code>@WithMockUser(roles=&quot;USER&quot;)<\/code> \ub97c \uc774\uc6a9\ud574 \uad8c\ud55c\uc744 \ubd80\uc5ec\ud569\ub2c8\ub2e4.<\/p>\n<p>src\/test\/java\/kr\/co\/episode\/example\/web\/PostsApiControllerTest.java<\/p>\n<pre><code class=\"language-java\">@RunWith(SpringRunner.class)\n@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)\npublic class PostsApiControllerTest {\n\n    @LocalServerPort\n    private int port;\n\n    @Autowired\n    private PostsRepository postsRepository;\n\n    @Autowired\n    private WebApplicationContext context;\n\n    private MockMvc mvc;\n\n    @Before\n    public void setup() {\n        mvc = MockMvcBuilders\n                .webAppContextSetup(context)\n                .apply(springSecurity())\n                .build();\n    }\n\n    @After\n    public void tearDown() throws Exception {\n        postsRepository.deleteAll();\n    }\n\n    @Test\n    @WithMockUser(roles=&quot;USER&quot;)\n    public void save() throws Exception {\n\n        \/\/ given\n        String title = &quot;title&quot;;\n        String content = &quot;content&quot;;\n        String author = &quot;author&quot;;\n        PostsSaveRequestDto requestDto = PostsSaveRequestDto.builder()\n                .title(title)\n                .content(content)\n                .author(author)\n                .build();\n\n        String url = &quot;http:\/\/localhost:&quot; + port + &quot;\/api\/v1\/posts&quot;;\n\n        \/\/ when\n        \/\/ org.springframework.test.web.servlet \uc744 \uc784\ud3ec\ud2b8\ud569\ub2c8\ub2e4.\n        mvc.perform(post(url)\n                .contentType(MediaType.APPLICATION_JSON_UTF8)\n                .content(new ObjectMapper().writeValueAsString(requestDto)))\n                .andExpect(status().isOk());\n\n        \/\/ then\n        List&lt;Posts&gt; all = postsRepository.findAll();\n        \/\/ org.assertj.core.api.Assertions \uc744 \uc784\ud3ec\ud2b8\ud569\ub2c8\ub2e4.\n        assertThat(all.get(0).getTitle()).isEqualTo(title);\n        assertThat(all.get(0).getContent()).isEqualTo(content);\n    }\n\n    @Test\n    @WithMockUser(roles=&quot;USER&quot;)\n    public void update() throws Exception {\n\n        \/\/ given\n        Posts savedPosts = postsRepository.save(Posts.builder()\n                .title(&quot;title&quot;)\n                .content(&quot;content&quot;)\n                .author(&quot;author&quot;)\n                .build());\n\n        Long updateId = savedPosts.getId();\n        String expectedTitle = &quot;title2&quot;;\n        String expectedContent = &quot;content2&quot;;\n\n        PostsUpdateRequestDto requestDto = PostsUpdateRequestDto.builder()\n                .title(expectedTitle)\n                .content(expectedContent)\n                .build();\n\n        String url = &quot;http:\/\/localhost:&quot; + port + &quot;\/api\/v1\/posts\/&quot; + updateId;\n\n        \/\/ when\n        mvc.perform(put(url)\n                .contentType(MediaType.APPLICATION_JSON_UTF8)\n                .content(new ObjectMapper().writeValueAsString(requestDto)))\n                .andExpect(status().isOk());\n\n        \/\/ then\n        List&lt;Posts&gt; all = postsRepository.findAll();\n        assertThat(all.get(0).getTitle()).isEqualTo(expectedTitle);\n        assertThat(all.get(0).getContent()).isEqualTo(expectedContent);\n    }\n}<\/code><\/pre>\n<h3>\ucee8\ud2b8\ub864\ub7ec \ud14c\uc2a4\ud2b8\ud558\uae30<\/h3>\n<pre><code class=\"language-java\">@RunWith(SpringRunner.class)\n@SpringBootTest(webEnvironment = RANDOM_PORT)\npublic class IndexControllerTest {\n\n    @Autowired\n    private PostsRepository postsRepository;\n\n    @Autowired\n    private TestRestTemplate restTemplate;\n\n    @Before\n    public void setUp() throws Exception {\n        postsRepository.save(Posts.builder()\n                .title(&quot;title title title&quot;)\n                .content(&quot;content content content&quot;)\n                .author(&quot;author author author&quot;)\n                .build());\n    }\n\n    @After\n    public void tearDown() throws Exception {\n        postsRepository.deleteAll();\n    }\n\n    @Test\n    public void index() {\n\n        \/\/ given\n\n        \/\/ when\n        String body = this.restTemplate.getForObject(&quot;\/&quot;, String.class);\n\n        \/\/ then\n        \/\/ org.assertj.core.api.Assertions \uc744 \uc784\ud3ec\ud2b8\ud569\ub2c8\ub2e4.\n        \/\/ org.junit.Assert \uac00 \uc784\ud3ec\ud2b8\ub418\uc5b4 \uc788\uc73c\uba74 \uc81c\uac70\ud569\ub2c8\ub2e4.\n        assertThat(body).contains(&quot;title title title&quot;);\n    }\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Spring Boot Test JUnit 4.x \uae30\uc900\uc73c\ub85c \uc791\uc131\ud569\ub2c8\ub2e4. \uac1c\ubc1c\ud658\uacbd Spring Boot 2.1.x Gradle 4.10.2 junit 4.12 build.gradle \uc218\uc815 build.gradle &#8230;&#8230; dependencies { \/\/ &#8230;&#8230; testCompile(&quot;org.springframework.security:spring-security-test&quot;) \/\/ &#8230;&#8230; } &#8230;&#8230; \ud14c\uc2a4\ud2b8\uc6a9 application.properties \uc0dd\uc131 src\/test\/resources\/application.properties # \ube48(empty) \ud30c\uc77c\ub85c \uc0dd\uc131\ud569\ub2c8\ub2e4. # \ud30c\uc77c\uc774 \ube44\uc5b4\uc788\uc73c\uba74 \uc2a4\ud504\ub9c1\uc740 \uc790\ub3d9\uc73c\ub85c h2 \ub97c \ub370\uc774\ud0c0\ubca0\uc774\uc2a4\ub85c \uc124\uc815\ud569\ub2c8\ub2e4. \ud30c\uc77c\uc744 \uc0dd\uc131\ud558\uc9c0 \uc54a\uace0 @AutoConfigureTestDatabase \ub97c \uc774\uc6a9\ud574 \uc6d0\ub798\uc758 DB \ub97c \uc0ac\uc6a9\ud560\u2026 <span class=\"read-more\"><a href=\"https:\/\/www.skyer9.pe.kr\/wordpress\/?p=338\">Read More &raquo;<\/a><\/span><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-338","post","type-post","status-publish","format-standard","hentry","category-spring-boot-2-1"],"_links":{"self":[{"href":"https:\/\/www.skyer9.pe.kr\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/338","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.skyer9.pe.kr\/wordpress\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.skyer9.pe.kr\/wordpress\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.skyer9.pe.kr\/wordpress\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.skyer9.pe.kr\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=338"}],"version-history":[{"count":13,"href":"https:\/\/www.skyer9.pe.kr\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/338\/revisions"}],"predecessor-version":[{"id":1535,"href":"https:\/\/www.skyer9.pe.kr\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/338\/revisions\/1535"}],"wp:attachment":[{"href":"https:\/\/www.skyer9.pe.kr\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=338"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.skyer9.pe.kr\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=338"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.skyer9.pe.kr\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=338"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}