{"id":83,"date":"2017-10-09T18:59:30","date_gmt":"2017-10-09T09:59:30","guid":{"rendered":"http:\/\/ito-u-oti.com\/?p=83"},"modified":"2018-05-05T19:10:03","modified_gmt":"2018-05-05T10:10:03","slug":"post-83","status":"publish","type":"post","link":"https:\/\/ito-u-oti.com\/?p=83","title":{"rendered":"Spring Framework\u306eUnit\u30c6\u30b9\u30c8\u5b9f\u88c5\u65b9\u6cd5\u30001-1.Repository\u30c6\u30b9\u30c8(Junit4, spring-test)"},"content":{"rendered":"<h2 id=\"outline__1\">Spring Framework\u306eUnit\u30c6\u30b9\u30c8\u5b9f\u88c5\u65b9\u6cd5\u30001-1.Repository\u30c6\u30b9\u30c8(Junit4, spring-test)<\/h2>\n<p>\u5099\u5fd8\u9332\u304b\u306d\u3066Spring Framework\u306eUnit\u30c6\u30b9\u30c8\u306e\u5b9f\u88c5\u65b9\u6cd5\u3092\u30d1\u30bf\u30fc\u30f3\u5225\u306b\u307e\u3068\u3081\u3066\u3044\u304d\u307e\u3059\u3002<\/p>\n<div class=\"pointBox\">\u5143\u306e\u30b5\u30f3\u30d7\u30eb\u306fTERASOLUNA Framework\u306e\u30c1\u30e5\u30fc\u30c8\u30ea\u30a2\u30eb\u30b5\u30f3\u30d7\u30eb\u3092\u4f7f\u7528\u3057\u3066\u3044\u307e\u3059\u3002<br \/>\nTERASOLUNA\u3068\u3044\u3046NTT\u30c7\u30fc\u30bf\u304c\u4f5c\u3063\u305fOSS\u3067\u3059\u304c\u3001\u5b9f\u969b\u306f\u307b\u3068\u3093\u3069Spring Framework\u306b\u306a\u3063\u3066\u307e\u3059\u3002<br \/>\nSpring Framework\u306e\u6700\u521d\u306e\u52c9\u5f37\u7528\u3068\u3057\u3066\u306f\u4f7f\u3048\u308b\u304b\u3082\u3057\u308c\u307e\u305b\u3093\u3002<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<p>\u3067\u306f\u3044\u304d\u307e\u3059\u3002<\/p>\n<h2 id=\"outline__2\">1-1.Repository\u30c6\u30b9\u30c8\u3000Junit4,Spring-test\u30e9\u30a4\u30d6\u30e9\u30ea\u3092\u4f7f\u7528\u3057\u305f\u30d1\u30bf\u30fc\u30f3<\/h2>\n<p>\u30fbDB\u3078\u30c7\u30fc\u30bf\u306e\u30bb\u30c3\u30c8\u30a2\u30c3\u30d7<br \/>\n\u30fb\u30c6\u30b9\u30c8\u7528\u30c7\u30fc\u30bf\u306e\u4f5c\u6210<br \/>\n\u30fb\u30e1\u30bd\u30c3\u30c9\u306e\u30c6\u30b9\u30c8<br \/>\n\u30fb\u7d50\u679c\u306e\u691c\u8a3c<br \/>\n\u30fb\u30c6\u30b9\u30c8\u5b9f\u884c\u5f8c\u306eDB\u72b6\u614b\u306e\u78ba\u8a8d<br \/>\n\u306e\u6d41\u308c\u3092\u5b9f\u88c5\u3057\u307e\u3059\u3002<\/p>\n<h3 id=\"outline__2_1\">\u3010pom.xml\u3011<\/h3>\n<p>\u30c6\u30b9\u30c8\u306b\u5fc5\u8981\u306a\u30e9\u30a4\u30d6\u30e9\u30ea\u306f\u4ee5\u4e0b<\/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<p>spring-test\u306fSpring\u306e\u6a5f\u80fd\u3067\u3042\u308bDI\u30b3\u30f3\u30c6\u30ca\u3092\u6709\u52b9\u306b\u3057\u305f\u308a\u3057\u307e\u3059\u3002<\/p>\n<p>\u672c\u6765\u3067\u3042\u308c\u3070\u3001Repository\u7528\u306e\u30a4\u30f3\u30bf\u30d5\u30a7\u30fc\u30b9\u306b\u5bfe\u3057\u3066implement\u3057\u305f\u5b9f\u88c5\u30af\u30e9\u30b9\u3092\u4f5c\u6210\u3057\u3001\u305d\u3053\u3067DB\u3078\u306e\u63a5\u7d9a\u3068\u304b\u8272\u3005\u3057\u3066\u3044\u304f\u306e\u3067\u3059\u304c\u3001<br \/>\nmybatis\u306e\u5834\u5408\u30a4\u30f3\u30bf\u30d5\u30a7\u30fc\u30b9\u3068\u305d\u308c\u306b\u7d10\u3065\u3044\u305fsql\u6587\u304c\u66f8\u304b\u308c\u305fxml\u304c\u3042\u308c\u3070\u5b9f\u88c5\u30af\u30e9\u30b9\u3092\u4f5c\u6210\u3059\u308b\u5fc5\u8981\u304c\u306a\u3044\u3067\u3059\u3002<br \/>\n\u3068\u3044\u3046\u3053\u3068\u3067\u3001\u5b9f\u88c5\u30af\u30e9\u30b9\u304c\u5b58\u5728\u3057\u306a\u3044\u306e\u3067\u30a4\u30f3\u30bf\u30d5\u30a7\u30fc\u30b9\u3092\u4f7f\u3063\u3066\u9593\u63a5\u7684\u306bRepository\u306e\u30c6\u30b9\u30c8\u3092\u3057\u3066\u3044\u304f\u3053\u3068\u306b\u306a\u308a\u307e\u3059\u3002<br \/>\n\u305d\u306e\u305f\u3081\u3001Spring-test\u3092\u4f7f\u3063\u3066\u30a4\u30f3\u30bf\u30d5\u30a7\u30fc\u30b9\u3092\u5a92\u4ecb\u306b\u3057\u3066DI\u30b3\u30f3\u30c6\u30ca\u304b\u3089\u81ea\u52d5\u3067\u4f5c\u6210\u3055\u308c\u308b\u5b9f\u88c5\u30af\u30e9\u30b9\u3092\u5f15\u3063\u5f35\u308a\u51fa\u3059\u611f\u3058\u3067\u3059\u306d\u3002<\/p>\n<h3 id=\"outline__2_2\">\u3010\u30c6\u30b9\u30c8\u5bfe\u8c61\u30af\u30e9\u30b9\u3011<\/h3>\n<p>TodoRepository.java<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npublic interface TodoRepository {\r\n   Todo findOne(String todoId);\r\n\r\n    Collection&lt;Todo&gt; findAll();\r\n\r\n    void create(Todo todo);\r\n\r\n    boolean update(Todo todo);\r\n\r\n    void delete(Todo todo);\r\n\r\n    long countByFinished(boolean finished);\r\n}\r\n<\/pre>\n<p>\u4eca\u56de\u306f\u3053\u306e\u4e2d\u306eupdate\u30e1\u30bd\u30c3\u30c9\u306b\u3064\u3044\u3066\u30c6\u30b9\u30c8\u30b3\u30fc\u30c9\u3092\u4f5c\u3063\u3066\u3044\u3053\u3046\u3068\u601d\u3044\u307e\u3059\u3002<\/p>\n<h3 id=\"outline__2_3\">\u3010\u30c6\u30b9\u30c8\u30af\u30e9\u30b9\u3011<\/h3>\n<p>TodoRepositoryTest.java<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\n@RunWith(SpringJUnit4ClassRunner.class)\r\n@ContextConfiguration(locations = {&quot;classpath:META-INF\/spring\/test-context.xml&quot;})\r\n@Transactional\r\npublic class TodoRepositoryTest {\r\n\r\n    @Inject\r\n    TodoRepository target;\r\n    \r\n    @Inject\r\n    JdbcTemplate jdbctemplate;\r\n    \r\n    @Before\r\n    public void setUp() throws Exception {\r\n        \r\n        \/\/JdbcTemplate\u3067\u30c6\u30fc\u30d6\u30eb\u30c7\u30fc\u30bf\u3092\u521d\u671f\u5316\u3059\u308b\r\n        jdbctemplate.update(&quot;DELETE FROM todo&quot;);\r\n        \r\n        \/\/\u30c6\u30fc\u30d6\u30eb\u306b\u30c7\u30fc\u30bf\u3092\u30bb\u30c3\u30c8\u30a2\u30c3\u30d7\u3059\u308b\uff08setUpTabledata\u30e1\u30bd\u30c3\u30c9\u306f\u30c7\u30fc\u30bf\u4f5c\u6210\u7528private\u30e1\u30bd\u30c3\u30c9\uff09\r\n        List&lt;Todo&gt; todoList = setUpTabledata();\r\n        for(int i=0; i &lt; todoList.size(); i++) {\r\n            Todo todo = todoList.get(i);\r\n            \/\/JdbcTemplate\u3067\u30bb\u30c3\u30c8\u30a2\u30c3\u30d7\r\n            jdbctemplate.update(&quot;INSERT INTO todo (todo_id,todo_title,finished,created_at) VALUES(?,?,?,?)&quot;\r\n                    ,todo.getTodoId()\r\n                    ,todo.getTodoTitle()\r\n                    ,todo.isFinished()\r\n                    ,todo.getCreatedAt()\r\n                    );\r\n        }\r\n    }\r\n    \r\n    @Test\r\n    @Rollback\r\n    public void testUpdate() throws Exception {\r\n        \/\/\u30c6\u30b9\u30c8\u7528\u306e\u30c7\u30fc\u30bf\u3092\u4f5c\u6210\r\n        SimpleDateFormat sdFormat = new SimpleDateFormat(&quot;yyyy-MM-dd HH:mm:ss.SSS&quot;);\r\n        String todoId = &quot;cceae402-c5b1-440f-bae2-7bee19dc17fb&quot;;\r\n        Todo testDataTodo = getTodoData(todoId);\r\n        testDataTodo.setFinished(true);\r\n        \r\n        \/\/update\u30e1\u30bd\u30c3\u30c9\u306e\u30c6\u30b9\u30c8\r\n        boolean actTodo = target.update(testDataTodo);\r\n\r\n        \/\/\u7d50\u679c\u691c\u8a3c\r\n        assertEquals(actTodo, true);\r\n        \r\n        \/\/\u671f\u5f85\u5024\u306e\u4f5c\u6210\r\n        Todo exptodo = new Todo();\r\n        exptodo.setTodoId(&quot;cceae402-c5b1-440f-bae2-7bee19dc17fb&quot;);\r\n        exptodo.setTodoTitle(&quot;one&quot;);\r\n        exptodo.setFinished(true);\r\n        String strDate = &quot;2017-10-01 15:39:17.888&quot;;\r\n        Date date = sdFormat.parse(strDate);\r\n        exptodo.setCreatedAt(date);\r\n        \r\n        \/\/\u51e6\u7406\u5f8c\u30c7\u30fc\u30bf\u306e\u53d6\u5f97\uff08getTodoData\u30e1\u30bd\u30c3\u30c9\u306fDB\u304b\u3089\u30c6\u30b9\u30c8\u5f8c\u306b\u5909\u66f4\u3055\u308c\u305f\u30c7\u30fc\u30bf\u3092\u53d6\u5f97\u3059\u308bprivate\u30e1\u30bd\u30c3\u30c9\uff09\r\n        Todo actTestDataTodo = getTodoData(todoId);\r\n        \r\n        \/\/\u30e1\u30bd\u30c3\u30c9\u5b9f\u884c\u5f8c\u30c6\u30fc\u30d6\u30eb\u30c7\u30fc\u30bf\u691c\u8a3c\r\n        \/\/date\u578b\u306e\u8868\u793a\u5f62\u5f0f\u304c\u7570\u306a\u308b\u305f\u3081\u3001\u6642\u523b\u6587\u5b57\u5217\u306b\u5909\u63db\u3057\u3066\u6bd4\u8f03\u3057\u3066\u3044\u308b\r\n        assertEquals(exptodo.getTodoId(), actTestDataTodo.getTodoId());\r\n        assertEquals(exptodo.getTodoTitle(), actTestDataTodo.getTodoTitle());\r\n        assertEquals(exptodo.isFinished(), actTestDataTodo.isFinished());\r\n        assertEquals(sdFormat.format(exptodo.getCreatedAt()) ,sdFormat.format(actTestDataTodo.getCreatedAt()));\r\n    }\r\n}\r\n<\/pre>\n<h3 id=\"outline__2_4\">\u3010\u305d\u306e\u4ed6\u8a2d\u5b9a\u3011<\/h3>\n<p>test-context.xml<br \/>\nJdbcTemplate\u3092\u4f7f\u3048\u308b\u3088\u3046\u306b\u8a2d\u5b9a\u3057\u307e\u3059\u3002<br \/>\n\u57fa\u672c\u306fapplicationContext.xml\u306e\u8a2d\u5b9a\u3092\u4e38\u30d1\u30af\u30ea\u3057\u3066\u3001\u4e0d\u5fc5\u8981\u306a\u3082\u306e\u3092\u524a\u3063\u305f\u611f\u3058\u3067\u3059\u3002<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n&lt;!-- jdbcTemplate\u306e\u8a2d\u5b9a --&gt;\r\n&lt;bean class=&quot;org.springframework.jdbc.core.JdbcTemplate&quot;&gt;\r\n    &lt;constructor-arg ref=&quot;dataSource&quot; \/&gt;\r\n&lt;\/bean&gt;\r\n<\/pre>\n<p>JDBCTemplate\u3092\u4f7f\u3063\u305f\u30b7\u30f3\u30d7\u30eb\u306a\u30c6\u30b9\u30c8\u30b3\u30fc\u30c9\u3067\u3059\u3002<br \/>\n\u305f\u3060\u3001\u30c6\u30b9\u30c8\u7528\u306e\u30c7\u30fc\u30bf\u3092\u5927\u91cf\u306b\u4f5c\u6210\u3059\u308b\u3068\u304b\u3001DB\u306e\u30bb\u30c3\u30c8\u30a2\u30c3\u30d7\u304c\u8907\u96d1\u306a\u611f\u3058\u306b\u306a\u308b\u5834\u5408\u306f\u30c6\u30b9\u30c8\u30b3\u30fc\u30c9\u306e\u53ef\u8aad\u6027\u304c\u843d\u3061\u305d\u3046\u3067\u3059\u306d\u3002<br \/>\n\u3067\u304d\u308b\u306a\u3089Dbunit\u3068\u304b\u4f7f\u3063\u305f\u65b9\u304c\u3044\u3044\u3067\u3059\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-pattern1\">Spring-Unit-Test-pattern1<\/a><\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Spring Framework\u306eUnit\u30c6\u30b9\u30c8\u5b9f\u88c5\u65b9\u6cd5\u30001-1.Repository\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":[27,25,28,40,24],"tags":[43,11,13,10,42,41],"class_list":["post-83","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-junit","category-maven","category-mybatis","category-repository-test","category-spring","tag-jdbctemplate","tag-junit","tag-mybatis","tag-spring","tag-spring-test","tag-unittest"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/ito-u-oti.com\/index.php?rest_route=\/wp\/v2\/posts\/83","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=83"}],"version-history":[{"count":9,"href":"https:\/\/ito-u-oti.com\/index.php?rest_route=\/wp\/v2\/posts\/83\/revisions"}],"predecessor-version":[{"id":428,"href":"https:\/\/ito-u-oti.com\/index.php?rest_route=\/wp\/v2\/posts\/83\/revisions\/428"}],"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=83"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ito-u-oti.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=83"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ito-u-oti.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=83"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}