{"id":1103,"date":"2019-09-25T21:31:11","date_gmt":"2019-09-25T12:31:11","guid":{"rendered":"https:\/\/ito-u-oti.com\/?p=1103"},"modified":"2019-09-25T21:31:15","modified_gmt":"2019-09-25T12:31:15","slug":"post-1103","status":"publish","type":"post","link":"https:\/\/ito-u-oti.com\/?p=1103","title":{"rendered":"\u3010Spring Boot\u3011@SpringBootTest\u306e\u30c6\u30b9\u30c8\u304c\u91cd\u3044\u3068\u304d\u306e\u4ee3\u66ff\u7b56\u3010Junit\u3011"},"content":{"rendered":"\n<p>\u30d7\u30ed\u30b8\u30a7\u30af\u30c8\u5185\u306e\u4f9d\u5b58\u30af\u30e9\u30b9\u304c\u5897\u52a0\u3059\u308b\u306b\u3064\u308c\u3066\u3001\u4f9d\u5b58\u30af\u30e9\u30b9\u5168\u3066\u3092\u8aad\u307f\u8fbc\u3080SpringBootTest\u306f\u8d77\u52d5\u304c\u91cd\u304f\u306a\u3063\u3066\u3044\u304d\u307e\u3059\u3002<br>\u5168\u3066\u306e\u30c6\u30b9\u30c8\u30af\u30e9\u30b9\u3092SpringBootTest\u3067\u5b9f\u884c\u3059\u308b\u3068\u3001\u5168\u4f53\u306e\u30c6\u30b9\u30c8\u5b9f\u884c\u6642\u9593\u304c\u304b\u306a\u308a\u9577\u304f\u306a\u3063\u3066\u3057\u307e\u3044\u958b\u767a\u306b\u5f71\u97ff\u3092\u53ca\u307c\u3057\u307e\u3059\u3002<br>\u5c11\u3057\u3067\u3082\u30c6\u30b9\u30c8\u5b9f\u884c\u6642\u9593\u3092\u6e1b\u3089\u3059\u305f\u3081\u306b\u3001\u5168\u3066SpringBootTest\u3092\u4f7f\u3063\u3066\u30c6\u30b9\u30c8\u3092\u3059\u308b\u306e\u3067\u306f\u306a\u304f\u30ec\u30a4\u30e4\u3054\u3068\u306b\u5fc5\u8981\u306a\u4f9d\u5b58\u30af\u30e9\u30b9\u306e\u307f\u3092\u8aad\u307f\u8fbc\u307e\u305b\u308b\u3053\u3068\u3067\u6642\u9593\u3092\u77ed\u304f\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002<\/p>\n\n\n\n<h2 id=\"outline__1\" class=\"wp-block-heading\">Controller\u5c64\uff08@WebMvcTest\uff09<\/h2>\n\n\n\n<p>MockMvc\u7528\u3067\u306eController\u5c64\u306b\u5fc5\u8981\u306a\u4f9d\u5b58\u30af\u30e9\u30b9\u3092\u8aad\u307f\u8fbc\u307f\u307e\u3059\u3002<\/p>\n\n\n\n<h3 id=\"outline__1_1\" class=\"wp-block-heading\">\u30c6\u30b9\u30c8\u5bfe\u8c61<\/h3>\n\n\n\n<p>DemoApi.java<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: java; title: ; notranslate\" title=\"\">\npublic interface DemoApi {\n\n    @RequestMapping(&quot;\/demoApi&quot;)\n    ResponseEntity&lt;DemoApiDto&gt; get(DemoApiDto body);\n}\n<\/pre><\/div>\n\n\n<h3 id=\"outline__1_2\" class=\"wp-block-heading\">\u4fee\u6b63\u524d\uff08@SpringBootTest\uff09<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: java; title: ; notranslate\" title=\"\">\n@SpringBootTest\npublic class DemoApiSpringBootTest {\n\n    private MockMvc mockMvc;\n\n    @Autowired\n    private DemoApi demoApi;\n\n    @BeforeEach\n    void setUp() {\n        mockMvc = MockMvcBuilders.standaloneSetup(demoApi)\n                .setControllerAdvice(new DemoHandler())\n                .build();\n    }\n\n    @Test\n    void isOK() throws Exception {\n        \/\/ GIVEN\n        DemoApiDto demoApiDto = DemoApiDto.builder()\n                .demoName(&quot;hogeDemo&quot;)\n                .demoObjectDto(\n                        DemoObjectDto.builder()\n                                .demoObjName(&quot;pugeObjName&quot;)\n                                .build())\n                .build();\n\n        ObjectMapper objectMapper = new ObjectMapper();\n        String request = objectMapper.writeValueAsString(demoApiDto);\n\n        \/\/ WHEN\n        \/\/ THEN\n        this.mockMvc\n                .perform(get(&quot;\/demoApi&quot;)\n                        .contentType(MediaType.APPLICATION_JSON_UTF8)\n                        .content(request))\n                .andDo(MockMvcResultHandlers.print())\n                .andExpect(status().isOk());\n    }\n}\n<\/pre><\/div>\n\n\n<h3 id=\"outline__1_3\" class=\"wp-block-heading\">\u4fee\u6b63\u5f8c\uff08@WebMvcTest\uff09<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: java; title: ; notranslate\" title=\"\">\n@WebMvcTest(controllers = DemoApiController.class)\npublic class DemoApiWebMvcTest {\n\n    private MockMvc mockMvc;\n\n    @Autowired\n    private DemoApi demoApi;\n\n    @BeforeEach\n    void setUp() {\n        mockMvc = MockMvcBuilders.standaloneSetup(demoApi)\n                .setControllerAdvice(new DemoHandler())\n                .build();\n    }\n\n    @Test\n    void isOK() throws Exception {\n        \/\/ GIVEN\n        DemoApiDto demoApiDto = DemoApiDto.builder()\n                .demoName(&quot;hogeDemo&quot;)\n                .demoObjectDto(\n                        DemoObjectDto.builder()\n                                .demoObjName(&quot;pugeObjName&quot;)\n                                .build())\n                .build();\n\n        ObjectMapper objectMapper = new ObjectMapper();\n        String request = objectMapper.writeValueAsString(demoApiDto);\n\n        \/\/ WHEN\n        \/\/ THEN\n        this.mockMvc\n                .perform(get(&quot;\/demoApi&quot;)\n                        .contentType(MediaType.APPLICATION_JSON_UTF8)\n                        .content(request))\n                .andDo(MockMvcResultHandlers.print())\n                .andExpect(status().isOk());\n    }\n}\n<\/pre><\/div>\n\n\n<p>Controller\u304c\u3055\u3089\u306b\u4f9d\u5b58\u30af\u30e9\u30b9\u3092\u6301\u3063\u3066\u3044\u308b\u5834\u5408\u306f\u3001includeFilters\u30aa\u30d7\u30b7\u30e7\u30f3\u3092\u4f7f\u3044\u307e\u3059\u3002<br>\u4f8b\u3048\u3070\u3001\u3053\u306eController\u30af\u30e9\u30b9\u306b\u300cHogeComponent\u300d\u3068\u300cPugeService\u300d\u304c\u4f9d\u5b58\u3057\u3066\u3044\u305f\u5834\u5408\u306f\u4ee5\u4e0b\u306e\u3088\u3046\u306bWebMvcTest\u3092\u4f7f\u3044\u307e\u3059\u3002<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: java; title: ; notranslate\" title=\"\">\n@WebMvcTest(controllers = DemoApiController.class,\n    includeFilters = @ComponentScan.Filter(classes = {HogeComponent.class, PugeService.class}, type = FilterType.ASSIGNABLE_TYPE))\n<\/pre><\/div>\n\n\n<h2 id=\"outline__2\" class=\"wp-block-heading\">Service\u3001Component\u5c64\uff08@SpringJUnitConfig\uff09<\/h2>\n\n\n\n<p>\u5fc5\u8981\u306a\u4f9d\u5b58\u30af\u30e9\u30b9\u3060\u3051\u3092\u8aad\u307f\u8fbc\u307e\u305b\u307e\u3059\u3002<\/p>\n\n\n\n<h3 id=\"outline__1_1\" class=\"wp-block-heading\">\u30c6\u30b9\u30c8\u5bfe\u8c61<\/h3>\n\n\n\n<p>DemoService.java(Component\u3092\u4f9d\u5b58\u30af\u30e9\u30b9\u3068\u3057\u3066\u6301\u3063\u3066\u3044\u308b)<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: java; title: ; notranslate\" title=\"\">\n@Service\n@RequiredArgsConstructor\nclass DemoService {\n\n    private  final DemoComponent demoComponent;\n\n    int triangleAreaTimesHeightByCalculation(int base, int height, int times){\n\n        int timesHeight = demoComponent.timesCalculation(height,times);\n\n        return base * timesHeight;\n    }\n}\n<\/pre><\/div>\n\n\n<h3 id=\"outline__2_2\" class=\"wp-block-heading\"> \u4fee\u6b63\u524d\uff08@SpringBootTest\uff09 <\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: java; title: ; notranslate\" title=\"\">\n@SpringBootTest\npublic class DemoServiceDISpringBootTest {\n\n    @Autowired\n    private DemoService target;\n\n    @Test\n    void triangleAreaTimesHeightByCalculationTest(){\n\n        \/\/GIVEN\n        int base = 6;\n        int height = 3;\n        int times = 2;\n        int excepted = 36;\n\n        \/\/WHEN\n        int actual = target.triangleAreaTimesHeightByCalculation(base, height,times);\n\n        \/\/THEN\n        assertThat(actual).isEqualTo(excepted);\n    }\n}\n<\/pre><\/div>\n\n\n<h3 id=\"outline__2_3\" class=\"wp-block-heading\"> \u4fee\u6b63\u5f8c\uff08@SpringJUnitConfig\uff09 <\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: java; title: ; notranslate\" title=\"\">\n@SpringJUnitConfig(classes = DemoServiceDISpringJunitConfigTest.Config.class)\npublic class DemoServiceDISpringJunitConfigTest {\n\n    @Autowired\n    private DemoService target;\n\n    @ComponentScan({\n            &quot;com.example.demo.service&quot;,\n            &quot;com.example.demo.component&quot;\n    })\n    static class Config {\n    }\n\n    @Test\n    void triangleAreaTimesHeightByCalculationTest() {\n\n        \/\/GIVEN\n        int base = 6;\n        int height = 3;\n        int times = 2;\n        int excepted = 36;\n\n        \/\/WHEN\n        int actual = target.triangleAreaTimesHeightByCalculation(base, height, times);\n\n        \/\/THEN\n        assertThat(actual).isEqualTo(excepted);\n    }\n}\n<\/pre><\/div>\n\n\n<p>static\u30af\u30e9\u30b9\u3092\u4f5c\u308a\u3001\u4f9d\u5b58\u3059\u308b\u30af\u30e9\u30b9\u3092ComponentScan\u5bfe\u8c61\u306b\u3057\u3066\u3044\u3057\u307e\u3059\u3002<br>\u305d\u306e\u30af\u30e9\u30b9\u3092\u30a2\u30ce\u30c6\u30fc\u30b7\u30e7\u30f3\u3067\u6307\u5b9a\u3059\u308b\u3053\u3068\u3067\u3001\u5bfe\u8c61\u306e\u30af\u30e9\u30b9\u306e\u307f\u304c\u8aad\u307f\u8fbc\u307e\u308c\u307e\u3059\u3002<\/p>\n\n\n\n<h2 id=\"outline__3\" class=\"wp-block-heading\">Repository\u5c64\uff08@MybatisTest\uff09<\/h2>\n\n\n\n<p>Mybatis\u3067\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u30a2\u30af\u30bb\u30b9\u3092\u884c\u3063\u3066\u3044\u308b\u5834\u5408\u306b\u4f7f\u3048\u307e\u3059\u3002<br>\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u30a2\u30af\u30bb\u30b9\u306b\u5fc5\u8981\u306a\u4f9d\u5b58\u30af\u30e9\u30b9\u3092\u8aad\u307f\u8fbc\u307f\u307e\u3059\u3002<\/p>\n\n\n\n<h3 id=\"outline__1_1\" class=\"wp-block-heading\">\u30c6\u30b9\u30c8\u5bfe\u8c61<\/h3>\n\n\n\n<p>AddressMapper.java<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: java; title: ; notranslate\" title=\"\">\n@Mapper\npublic interface AddressMapper {\n    int create(Address address);\n\n    @Results(id = &quot;AddressByDistinct&quot;,\n            value = {\n                    @Result(column = &quot;address_id&quot;, property = &quot;addressId&quot;),\n                    @Result(column = &quot;address&quot;, property = &quot;address&quot;),\n                    @Result(column = &quot;address2&quot;, property = &quot;address2&quot;),\n                    @Result(column = &quot;district&quot;, property = &quot;district&quot;),\n                    @Result(column = &quot;city_id&quot;, property = &quot;city.cityId&quot;),\n                    @Result(column = &quot;postal_code&quot;, property = &quot;postalCode&quot;),\n                    @Result(column = &quot;phone&quot;, property = &quot;phone&quot;),\n                    @Result(column = &quot;last_update&quot;, property = &quot;lastUpdate&quot;)\n            })\n    @Select(&quot;SELECT * FROM address WHERE district = #{district}&quot;)\n    List&lt;Address&gt; getAddressByDistrict(@Param(&quot;district&quot;) String distinct);\n}\n<\/pre><\/div>\n\n\n<h3 id=\"outline__3_2\" class=\"wp-block-heading\"> \u4fee\u6b63\u524d\uff08@SpringBootTest\uff09  <\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: java; title: ; notranslate\" title=\"\">\n@SpringBootTest\nclass AddressMapperSpringBootTest {\n\n    @Autowired\n    private AddressMapper addressMapper;\n\n    @Test\n    void assertionTest() {\n\n        \/\/GIVEN\n        String distinct = &quot;Bihar&quot;;\n\n        \/\/WHEN\n        List&lt;Address&gt; actual = addressMapper.getAddressByDistrict(distinct);\n\n        \/\/THEN\n        \/\/\u30d5\u30a3\u30fc\u30eb\u30c9\u306e\u5024\u304c\u5168\u3066Null\u3067\u306f\u306a\u3044\n        assertThat(actual).extracting(&quot;address&quot;).allMatch(Objects::nonNull);\n        \/\/\u30d5\u30a3\u30fc\u30eb\u30c9\u306e\u5024\u304c\u5168\u3066\u7a7a\u6587\u5b57\n        assertThat(actual).extracting(&quot;address2&quot;).allMatch(i -&gt; &quot;&quot;.equals(i));\n        \/\/\u30d5\u30a3\u30fc\u30eb\u30c9\u306e\u5024\u304c\u5168\u3066\u30ea\u30b9\u30c8\u3067\u6307\u5b9a\u3057\u305f\u5024\n        assertThat(actual).extracting(&quot;city.cityId&quot;).allMatch(\n                i -&gt; List.of(264, 110, 346).contains(i));\n    }\n}\n<\/pre><\/div>\n\n\n<h3 id=\"outline__3_3\" class=\"wp-block-heading\">   \u4fee\u6b63\u5f8c\uff08@MybatisTest\uff09<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: java; title: ; notranslate\" title=\"\">\n@MybatisTest\nclass AddressMapperMybatisTest {\n\n    @Autowired\n    private AddressMapper addressMapper;\n\n    @Test\n    void assertionTest() {\n\n        \/\/GIVEN\n        String distinct = &quot;Bihar&quot;;\n\n        \/\/WHEN\n        List&lt;Address&gt; actual = addressMapper.getAddressByDistrict(distinct);\n\n        \/\/THEN\n        \/\/\u30d5\u30a3\u30fc\u30eb\u30c9\u306e\u5024\u304c\u5168\u3066Null\u3067\u306f\u306a\u3044\n        assertThat(actual).extracting(&quot;address&quot;).allMatch(Objects::nonNull);\n        \/\/\u30d5\u30a3\u30fc\u30eb\u30c9\u306e\u5024\u304c\u5168\u3066\u7a7a\u6587\u5b57\n        assertThat(actual).extracting(&quot;address2&quot;).allMatch(i -&gt; &quot;&quot;.equals(i));\n        \/\/\u30d5\u30a3\u30fc\u30eb\u30c9\u306e\u5024\u304c\u5168\u3066\u30ea\u30b9\u30c8\u3067\u6307\u5b9a\u3057\u305f\u5024\n        assertThat(actual).extracting(&quot;city.cityId&quot;).allMatch(\n                i -&gt; List.of(264, 110, 346).contains(i));\n    }\n}\n<\/pre><\/div>\n\n\n<p>\u3042\u3089\u304b\u3058\u3081\u30d7\u30ed\u30d1\u30c6\u30a3\u30d5\u30a1\u30a4\u30eb\u306a\u3069\u3067\u8a2d\u5b9a\u3057\u3066\u3044\u308b\u63a5\u7d9a\u5148\u306eDB\u3092\u30c6\u30b9\u30c8\u3067\u4f7f\u7528\u3059\u308b\u5834\u5408\u306f\u3001@AutoConfigureTestDatabase\u30a2\u30ce\u30c6\u30fc\u30b7\u30e7\u30f3\u3082\u4f7f\u7528\u3057\u307e\u3059\u3002<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: java; title: ; notranslate\" title=\"\">\n@MybatisTest\n@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE)\nclass AddressMapperMybatisTest {\n\n    @Autowired\n    private AddressMapper addressMapper;\n\n    @Test\n    void assertionTest() {\n\n        \/\/GIVEN\n        String distinct = &quot;Bihar&quot;;\n\n        \/\/WHEN\n        List&lt;Address&gt; actual = addressMapper.getAddressByDistrict(distinct);\n\n        \/\/THEN\n        \/\/\u30d5\u30a3\u30fc\u30eb\u30c9\u306e\u5024\u304c\u5168\u3066Null\u3067\u306f\u306a\u3044\n        assertThat(actual).extracting(&quot;address&quot;).allMatch(Objects::nonNull);\n        \/\/\u30d5\u30a3\u30fc\u30eb\u30c9\u306e\u5024\u304c\u5168\u3066\u7a7a\u6587\u5b57\n        assertThat(actual).extracting(&quot;address2&quot;).allMatch(i -&gt; &quot;&quot;.equals(i));\n        \/\/\u30d5\u30a3\u30fc\u30eb\u30c9\u306e\u5024\u304c\u5168\u3066\u30ea\u30b9\u30c8\u3067\u6307\u5b9a\u3057\u305f\u5024\n        assertThat(actual).extracting(&quot;city.cityId&quot;).allMatch(\n                i -&gt; List.of(264, 110, 346).contains(i));\n    }\n}\n<\/pre><\/div>","protected":false},"excerpt":{"rendered":"<p>\u30d7\u30ed\u30b8\u30a7\u30af\u30c8\u5185\u306e\u4f9d\u5b58\u30af\u30e9\u30b9\u304c\u5897\u52a0\u3059\u308b\u306b\u3064\u308c\u3066\u3001\u4f9d\u5b58\u30af\u30e9\u30b9\u5168\u3066\u3092\u8aad\u307f\u8fbc\u3080SpringBootTest\u306f [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":93,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[24,6],"tags":[238,232],"class_list":["post-1103","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-spring","category-programing","tag-spring-boot","tag-test"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/ito-u-oti.com\/index.php?rest_route=\/wp\/v2\/posts\/1103","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ito-u-oti.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ito-u-oti.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ito-u-oti.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ito-u-oti.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1103"}],"version-history":[{"count":2,"href":"https:\/\/ito-u-oti.com\/index.php?rest_route=\/wp\/v2\/posts\/1103\/revisions"}],"predecessor-version":[{"id":1105,"href":"https:\/\/ito-u-oti.com\/index.php?rest_route=\/wp\/v2\/posts\/1103\/revisions\/1105"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/ito-u-oti.com\/index.php?rest_route=\/wp\/v2\/media\/93"}],"wp:attachment":[{"href":"https:\/\/ito-u-oti.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1103"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ito-u-oti.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1103"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ito-u-oti.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1103"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}