{"id":133,"date":"2017-12-03T03:38:01","date_gmt":"2017-12-02T18:38:01","guid":{"rendered":"http:\/\/ito-u-oti.com\/?p=133"},"modified":"2018-05-05T21:05:24","modified_gmt":"2018-05-05T12:05:24","slug":"post-133","status":"publish","type":"post","link":"https:\/\/ito-u-oti.com\/?p=133","title":{"rendered":"Spring Framework\u306eUnit\u30c6\u30b9\u30c8\u5b9f\u88c5\u65b9\u6cd5\u30003-3.Controller\u30c6\u30b9\u30c8(Junit4, spring-test, MockMvc(WebAppContextSetupMode), MockClass)"},"content":{"rendered":"<h2 id=\"outline__1\">Spring Framework\u306eUnit\u30c6\u30b9\u30c8\u5b9f\u88c5\u65b9\u6cd5\u30003-3.Controller\u30c6\u30b9\u30c8(Junit4, spring-test, MockMvc(WebAppContextSetupMode), MockClass)<\/h2>\n<div class=\"pointBox\">\u3010\u30b5\u30f3\u30d7\u30eb\u30bd\u30fc\u30b9\u3011<br \/>\n<a href=\"http:\/\/terasolunaorg.github.io\/guideline\/5.3.0.RELEASE\/ja\/index.html\">TERASOLUNA Server Framework for Java (5.x) Development Guideline<\/a><br \/>\n<a href=\"http:\/\/terasolunaorg.github.io\/guideline\/5.3.0.RELEASE\/ja\/Tutorial\/TutorialTodo.html\">\u30b5\u30f3\u30d7\u30eb\u30bd\u30fc\u30b9\u306f\u3053\u3061\u3089<\/a><br \/>\n\uff08\u3053\u308c\u306eMyBatis3\u3092\u4f7f\u7528\u3057\u305f\u30d1\u30bf\u30fc\u30f3\u3067\u4f5c\u6210\u3057\u3066\u307e\u3059\u3002\uff09<\/div>\n<h2 id=\"outline__2\">3-3.Controller\u30c6\u30b9\u30c8\u3000Junit4, spring-test\u30e9\u30a4\u30d6\u30e9\u30ea\u3001MockClass\u306b\u3088\u308b\u30e2\u30c3\u30af\u5316\u3092\u4f7f\u7528\u3057\u305f\u30d1\u30bf\u30fc\u30f3(WebAppContextSetupMode)<\/h2>\n<p>controller\u30af\u30e9\u30b9\u306e\u30c6\u30b9\u30c8\u3067\u3059\u3002<br \/>\nService\u30af\u30e9\u30b9\u306e\u30e1\u30bd\u30c3\u30c9\u3092\u30e2\u30c3\u30af\u30af\u30e9\u30b9\u3092\u7528\u3044\u3066\u30e2\u30c3\u30af\u5316\u3057\u3066\u3044\u307e\u3059\u3002<br \/>\n<a href=\"https:\/\/ito-u-oti.com\/spring-framework%E3%81%AEunit%E3%83%86%E3%82%B9%E3%83%88%E5%AE%9F%E8%A3%85%E6%96%B9%E6%B3%95%E3%80%803-1-controller%E3%83%86%E3%82%B9%E3%83%88junit4-spring-test-mockmvcstandalonesetupmode-mockcl\/\">3-1<\/a>\u3067\u3082\u30e2\u30c3\u30af\u30af\u30e9\u30b9\u306b\u3088\u308b\u30e2\u30c3\u30af\u5316\u3092\u884c\u3044\u307e\u3057\u305f\u304c\u3001\u4eca\u56de\u306fMockMvc\u306eWebAppContextSetupMode\u30d1\u30bf\u30fc\u30f3\u3092\u4f7f\u3063\u3066\u30e2\u30c3\u30af\u5316\u3057\u307e\u3059\u3002<\/p>\n<h3 id=\"outline__2_1\">\u3010pom.xml\u3011<\/h3>\n<p>Junit\u3001spring-test\u30e9\u30a4\u30d6\u30e9\u30ea\u304c\u5fc5\u8981\u3067\u3059\u3002<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n&lt;dependencies&gt;\r\n    &lt;dependency&gt;\r\n        &lt;groupId&gt;junit&lt;\/groupId&gt;\r\n        &lt;artifactId&gt;junit&lt;\/artifactId&gt;\r\n        &lt;scope&gt;test&lt;\/scope&gt;\r\n    &lt;\/dependency&gt;\r\n\r\n    &lt;dependency&gt;\r\n        &lt;groupId&gt;org.springframework&lt;\/groupId&gt;\r\n        &lt;artifactId&gt;spring-test&lt;\/artifactId&gt;\r\n        &lt;scope&gt;test&lt;\/scope&gt;\r\n    &lt;\/dependency&gt;\r\n&lt;\/dependencies&gt;\r\n<\/pre>\n<h3 id=\"outline__2_2\">\u3010\u30c6\u30b9\u30c8\u5bfe\u8c61\u30af\u30e9\u30b9\u3011<\/h3>\n<p>TodoController.java<br \/>\n\u30c6\u30b9\u30c8\u5bfe\u8c61\u306ffinish\u30e1\u30bd\u30c3\u30c9<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\n@Controller\r\n@RequestMapping(&quot;todo&quot;)\r\npublic class TodoController {\r\n\r\n    @Inject\r\n    TodoService todoService;\r\n    \r\n    @Inject\r\n    Mapper beanMapper;\r\n    \r\n    @ModelAttribute\r\n    public TodoForm setUpForm() {\r\n        TodoForm form = new TodoForm();\r\n        return form;\r\n    }\r\n    \r\n    @RequestMapping(value = &quot;finish&quot;, method = RequestMethod.POST)\r\n    public String finish(@Validated({Default.class}) TodoForm form, BindingResult bindingResult, Model model, RedirectAttributes attributes) {\r\n        if(bindingResult.hasErrors()) {\r\n            return list(model);\r\n        }\r\n        \r\n        try {\r\n            todoService.finish(form.getTodoId());\r\n        }catch (BusinessException e) {\r\n            \/\/ TODO: handle exception\r\n            model.addAttribute(e.getResultMessages());\r\n            return list(model);\r\n        }\r\n        \r\n        attributes.addFlashAttribute(ResultMessages.success().add(ResultMessage.fromText(&quot;Finished successfully!&quot;)));\r\n        return &quot;redirect:\/todo\/list&quot;;\r\n    }\r\n}\r\n<\/pre>\n<p>\uff08\u5bfe\u8c61\u30e1\u30bd\u30c3\u30c9\u306e\u51e6\u7406\u306e\u90e8\u5206\u3060\u3051\u629c\u7c8b\u3057\u3066\u307e\u3059\u3002\uff09<\/p>\n<h3 id=\"outline__2_3\">\u3010\u30c6\u30b9\u30c8\u30af\u30e9\u30b9\u3011<\/h3>\n<p>TodoControllerTestVerWebAppContext<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\n@RunWith(SpringJUnit4ClassRunner.class)\r\n\/\/\u8a2d\u5b9a\u30d5\u30a1\u30a4\u30eb\u306e\u8aad\u307f\u8fbc\u307f\r\n@ContextConfiguration({\r\n    &quot;classpath:META-INF\/spring\/test-context-webappcontextsetup.xml&quot;,\r\n    &quot;classpath:META-INF\/spring\/test-mvc.xml&quot;})\r\n\/\/\u8a2d\u5b9a\u30d5\u30a1\u30a4\u30eb\u306e\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3068\u3001ServletAPI\u306b\u4f9d\u5b58\u3059\u308b\u5404\u7a2e\u30e2\u30c3\u30af\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u306e\u30a4\u30f3\u30b8\u30a7\u30af\u30b7\u30e7\u30f3\u3092\u884c\u3046\r\n@WebAppConfiguration\r\npublic class TodoControllerTestVerWebAppContext {\r\n    \/\/applicationContext.xml\r\n    MockMvc mockMvc;\r\n    \r\n    @Inject\r\n    WebApplicationContext wac;\r\n    \r\n    @Before\r\n    public void setUp() throws Exception {\r\n        \/\/webAppContextSetup\u30e2\u30fc\u30c9\u3067mockMvc\u3092\u8d77\u52d5\r\n        mockMvc = MockMvcBuilders.webAppContextSetup(wac).build();\r\n    }\r\n    \r\n    \/\/\u6b63\u5e38\u52d5\u4f5c\u306e\u30d1\u30bf\u30fc\u30f3\r\n    @Test\r\n    public void testFinish() throws Exception {\r\n        \r\n        \/\/Controller\u306b\u6295\u3052\u308b\u30ea\u30af\u30a8\u30b9\u30c8\u3092\u4f5c\u6210\r\n        MockHttpServletRequestBuilder getRequest = \r\n                MockMvcRequestBuilders.post(&quot;\/todo\/finish&quot;)\r\n                                        .param(&quot;todoId&quot;, &quot;cceae402-c5b1-440f-bae2-7bee19dc17fb&quot;)\r\n                                        .param(&quot;todoTitle&quot;, &quot;one&quot;);\r\n        \r\n        \/\/Controller\u306b\u30ea\u30af\u30a8\u30b9\u30c8\u3092\u6295\u3052\u308b\r\n        ResultActions results = mockMvc.perform(getRequest);\r\n        \r\n        \/\/\u7d50\u679c\u691c\u8a3c\r\n        results.andDo(print());\r\n        results.andExpect(status().isFound());\r\n        results.andExpect(view().name(&quot;redirect:\/todo\/list&quot;));\r\n        \r\n        \/\/FlashMap\u306e\u30c7\u30fc\u30bf\u3092\u53d6\u5f97(model.addFlashAttribute\u306b\u8a2d\u5b9a\u3057\u305fmessage\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u306etext\u3092\u53d6\u5f97\u3059\u308b\u3002\u534a\u3070\u5f37\u5f15)\r\n        FlashMap flashMap = results.andReturn().getFlashMap();\r\n        Collection&lt;Object&gt; collection = flashMap.values();\r\n        for(Object obj : collection) {\r\n            ResultMessages messages = (ResultMessages) obj;\r\n            ResultMessage message = messages.getList().get(0);\r\n            String text = message.getText();\r\n            assertEquals(&quot;Finished successfully!&quot;, text);\r\n        }\r\n    }\r\n}\r\n<\/pre>\n<p>\u30c6\u30b9\u30c8\u30af\u30e9\u30b9\u306f\u6b63\u5e38\u52d5\u4f5c\u30d1\u30bf\u30fc\u30f3\u3060\u3051\u3067\u3059\u3002<br \/>\n<a href=\"https:\/\/ito-u-oti.com\/spring-framework%E3%81%AEunit%E3%83%86%E3%82%B9%E3%83%88%E5%AE%9F%E8%A3%85%E6%96%B9%E6%B3%95%E3%80%803-1-controller%E3%83%86%E3%82%B9%E3%83%88junit4-spring-test-mockmvcstandalonesetupmode-mockcl\/\">3-1<\/a>\u3067\u3084\u3063\u305f\u3088\u3046\u306a\u7570\u5e38\u7cfb\u306e\u30d1\u30bf\u30fc\u30f3\u3082\u5165\u308c\u305f\u304b\u3063\u305f\u306e\u3067\u3059\u304c\u3001\u5225\u306e\u30e2\u30c3\u30af\u30af\u30e9\u30b9\u3092\u4f7f\u7528\u3057\u3066\u3044\u308b\u305f\u3081\u3053\u306e\u30af\u30e9\u30b9\u30d5\u30a1\u30a4\u30eb\u3067\u306f\u5165\u308c\u307e\u305b\u3093\u3067\u3057\u305f\u3002<br \/>\n3-1\u3067\u306e\u30e2\u30c3\u30af\u30af\u30e9\u30b9\u306e\u4f7f\u7528\u4f8b\u3067\u306f\u30c6\u30b9\u30c8\u30e1\u30bd\u30c3\u30c9\u306e\u4e2d\u3067\u4f7f\u7528\u3059\u308b\u30e2\u30c3\u30af\u30af\u30e9\u30b9\u3092\u6307\u5b9a\u3057\u3066\u5207\u308a\u66ff\u3048\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3057\u305f\u3002<br \/>\n\u4e00\u65b9\u3067\u4eca\u56de\u306e\u30c6\u30b9\u30c8\u30af\u30e9\u30b9\u306e\u5834\u5408\u3001xml\u306e\u8a2d\u5b9a\u30d5\u30a1\u30a4\u30eb\u306e\u4e2d\u3067\u30e2\u30c3\u30af\u30af\u30e9\u30b9\u3092\u5ba3\u8a00\u3057\u307e\u3059\u3002\u305d\u3057\u3066\u3001\u30c6\u30b9\u30c8\u30af\u30e9\u30b9\u306e\u4e00\u756a\u6700\u521d\u306b\u8a2d\u5b9a\u30d5\u30a1\u30a4\u30eb\u3092\u8aad\u307f\u8fbc\u3080\u5ba3\u8a00\u3092\u884c\u3046\u305f\u3081\u3001\u30c6\u30b9\u30c8\u30e1\u30bd\u30c3\u30c9\u3054\u3068\u306e\u30e2\u30c3\u30af\u30af\u30e9\u30b9\u306e\u5207\u308a\u66ff\u3048\u304c\u3067\u304d\u307e\u305b\u3093\u3002\u3088\u3063\u3066\u3001\u30c6\u30b9\u30c8\u30e1\u30bd\u30c3\u30c9\u3054\u3068\u306b\u4f7f\u7528\u3059\u308b\u30e2\u30c3\u30af\u30af\u30e9\u30b9\u3092\u5207\u308a\u66ff\u3048\u305f\u3044\u5834\u5408\u306f\u30c6\u30b9\u30c8\u30af\u30e9\u30b9\u305d\u306e\u3082\u306e\u3092\u5225\u306b\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002<\/p>\n<h3 id=\"outline__2_4\">\u3010\u305d\u306e\u4ed6\u8a2d\u5b9a\u3011<\/h3>\n<p>test-context-webappcontextsetup.xml<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n&lt;!-- \u30e2\u30c3\u30af\u5bfe\u8c61\u306e\u30af\u30e9\u30b9\u3092exclude-filter\u3067\u30b3\u30f3\u30dd\u30fc\u30cd\u30f3\u30c8\u30b9\u30ad\u30e3\u30f3\u5bfe\u8c61\u304b\u3089\u9664\u5916\u3059\u308b --&gt;\r\n&lt;context:component-scan base-package=&quot;todo.domain&quot;&gt;\r\n    &lt;context:exclude-filter type=&quot;assignable&quot; expression=&quot;todo.domain.service.todo.TodoServiceImpl&quot;\/&gt;\r\n&lt;\/context:component-scan&gt;\r\n\r\n&lt;!-- \u30e2\u30c3\u30af\u30af\u30e9\u30b9\u3092\u30e2\u30c3\u30af\u5bfe\u8c61\u30af\u30e9\u30b9\u306b\u898b\u305b\u304b\u3051\u3066\u8a2d\u5b9a\u3059\u308b --&gt;\r\n&lt;bean id=&quot;TodoserviceImplMock&quot; class=&quot;todo.app.todo.TestFinishMock&quot; \/&gt;\r\n<\/pre>\n<p>\u8a2d\u5b9a\u30d5\u30a1\u30a4\u30eb\u306e\u4e2d\u3067\u3001\u30e2\u30c3\u30af\u5316\u3059\u308b\u30af\u30e9\u30b9\u3092conponent-scan\u5bfe\u8c61\u304b\u3089\u9664\u5916\u3057\u3001\u4ee3\u308f\u308a\u306b\u30e2\u30c3\u30af\u30af\u30e9\u30b9\u3092\u8aad\u307f\u8fbc\u307e\u305b\u307e\u3059\u3002<\/p>\n<p>TestFinishMock<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npublic class TestFinishMock implements TodoService{\r\n    \r\n    \/\/\u30e2\u30c3\u30af\u7528\u306e\u623b\u308a\u5024\u30c7\u30fc\u30bf\u3092\u4f5c\u6210\r\n    public Todo getTodoTrueData() throws Exception {\r\n        \r\n        Todo todo = new Todo();\r\n        todo.setTodoId(&quot;cceae402-c5b1-440f-bae2-7bee19dc17fb&quot;);\r\n        todo.setTodoTitle(&quot;one&quot;);\r\n        todo.setFinished(true);\r\n        SimpleDateFormat sdFormat = new SimpleDateFormat(&quot;yyyy-MM-dd HH:mm:ss.SSS&quot;);\r\n        String strDate = &quot;2017-10-01 15:39:17.888&quot;;\r\n        Date date1 = sdFormat.parse(strDate);\r\n        todo.setCreatedAt(date1);\r\n        \r\n        return todo;\r\n    }\r\n}\r\n<\/pre>\n<p>\u8aad\u307f\u8fbc\u307e\u305b\u305f\u30e2\u30c3\u30af\u30af\u30e9\u30b9\u3067\u3059\u3002implements\u3067\u30e2\u30c3\u30af\u5bfe\u8c61\u306e\u30af\u30e9\u30b9\u304c\u5b9f\u88c5\u3057\u3066\u3044\u305f\u30a4\u30f3\u30bf\u30d5\u30a7\u30fc\u30b9\u3092\u6307\u5b9a\u3057\u3066\u3044\u308b\u305f\u3081\u3001DI\u30b3\u30f3\u30c6\u30ca\u4e0a\u3067\u81ea\u52d5\u7684\u306bTodoService\u30a4\u30f3\u30bf\u30d5\u30a7\u30fc\u30b9\u3068\u3053\u306e\u30e2\u30c3\u30af\u30af\u30e9\u30b9\u304c\u7d10\u3065\u3051\u3089\u308c\u307e\u3059\u3002<\/p>\n<p>WebAppContextSetupMode\u3067\u30e2\u30c3\u30af\u5316\u3092\u884c\u3063\u305f\u6642\u306f\u3001\u4e00\u3064\u306e\u30af\u30e9\u30b9\u30d5\u30a1\u30a4\u30eb\u5185\u3067\u306f\u30e2\u30c3\u30af\u30af\u30e9\u30b9\u306e\u4f7f\u3044\u5206\u3051\u304c\u3067\u304d\u306a\u3044\u305f\u3081\u3001\u4f8b\u3048\u3070\u6b63\u5e38\u7cfb\u30a8\u30e9\u30fc\u7cfb\u3067\u4f7f\u7528\u3059\u308b\u30e2\u30c3\u30af\u30af\u30e9\u30b9\u3092\u5909\u3048\u305f\u3044\u3068\u3044\u3063\u305f\u5834\u5408\u30af\u30e9\u30b9\u30d5\u30a1\u30a4\u30eb\u3054\u3068\u5206\u3051\u306a\u3051\u308c\u3070\u3044\u3051\u306a\u3044\u3068\u3044\u3046\u3068\u3053\u308d\u304c\u3061\u3087\u3063\u3068\u9762\u5012\u304b\u3082\u3057\u308c\u306a\u3044\u3067\u3059\u3002\uff08\u3082\u3057\u304b\u3057\u305f\u3089\u3044\u3044\u65b9\u6cd5\u304c\u3042\u308b\u304b\u3082\u3057\u308c\u307e\u305b\u3093\u304c\u30fb\u30fb\u30fb\uff09<br \/>\nStandAloneSetup\u30d1\u30bf\u30fc\u30f3\u306b\u306f\u306a\u3044\u5927\u304d\u306a\u30e1\u30ea\u30c3\u30c8\u3068\u3057\u3066\u306f\u3084\u3063\u3071\u308aDI\u30b3\u30f3\u30c6\u30ca\u3092\u4f7f\u7528\u3057\u3066Service\u3068Repository\u3082\u4f7f\u3063\u305f\u7d50\u5408\u7684\u306a\u30c6\u30b9\u30c8\u304c\u884c\u3048\u308b\u3068\u3044\u3046\u3053\u3068\u3067\u3059\u306d\u3002<br \/>\n\u306a\u306e\u3067\u5358\u4f53\u8a66\u9a13\u306b\u306fStandAloneSetup\u3001\u7d50\u5408\u8a66\u9a13\u306b\u306fWebAppContextSetup\u3068\u3044\u3046\u3088\u3046\u306b\u4f7f\u3048\u308b\u306e\u3067\u306f\u306a\u3044\u3067\u3057\u3087\u3046\u304b\u3002<\/p>\n<p><span class=\"markerYellow\">\u30b5\u30f3\u30d7\u30eb\u30bd\u30fc\u30b9\u306fgithub\u3067\u516c\u958b\u3057\u3066\u307e\u3059\u3002<br \/>\n<a href=\"https:\/\/github.com\/itouoti12\/Spring-Unit-Test-pattern13\">Spring Unit Test pattern13<\/a><\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Spring Framework\u306eUnit\u30c6\u30b9\u30c8\u5b9f\u88c5\u65b9\u6cd5\u30003-3.Controller\u30c6\u30b9\u30c8(Jun [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":93,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[54,27,25,24,1],"tags":[11,53,55,10,42,41,57],"class_list":["post-133","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-controller-test","category-junit","category-maven","category-spring","category-1","tag-junit","tag-mockito","tag-mockmvc","tag-spring","tag-spring-test","tag-unittest","tag-webappcontextsetup"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/ito-u-oti.com\/index.php?rest_route=\/wp\/v2\/posts\/133","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=133"}],"version-history":[{"count":3,"href":"https:\/\/ito-u-oti.com\/index.php?rest_route=\/wp\/v2\/posts\/133\/revisions"}],"predecessor-version":[{"id":447,"href":"https:\/\/ito-u-oti.com\/index.php?rest_route=\/wp\/v2\/posts\/133\/revisions\/447"}],"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=133"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ito-u-oti.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=133"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ito-u-oti.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=133"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}