{"id":102,"date":"2017-10-09T20:12:41","date_gmt":"2017-10-09T11:12:41","guid":{"rendered":"http:\/\/ito-u-oti.com\/?p=102"},"modified":"2018-05-05T19:13:34","modified_gmt":"2018-05-05T10:13:34","slug":"post-102","status":"publish","type":"post","link":"https:\/\/ito-u-oti.com\/?p=102","title":{"rendered":"Spring Framework\u306eUnit\u30c6\u30b9\u30c8\u5b9f\u88c5\u65b9\u6cd5\u30001-2.Repository\u30c6\u30b9\u30c8(Junit4, spring-test)"},"content":{"rendered":"<h2 id=\"outline__1\">Spring Framework\u306eUnit\u30c6\u30b9\u30c8\u5b9f\u88c5\u65b9\u6cd5\u30001-2.Repository\u30c6\u30b9\u30c8(Junit4, spring-test)<\/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\">1-2.Repository\u30c6\u30b9\u30c8\u3000Junit4,Spring-test\u30e9\u30a4\u30d6\u30e9\u30ea\u3092\u4f7f\u7528\u3057\u305f\u30d1\u30bf\u30fc\u30f3<\/h2>\n<p>1-1\u3068\u30bd\u30fc\u30b9\u306e\u6d41\u308c\u306f\u540c\u3058\u3067\u3059\u304c\u3001\u30c7\u30fc\u30bf\u306e\u30bb\u30c3\u30c8\u30a2\u30c3\u30d7\u65b9\u6cd5\u304c\u9055\u3044\u307e\u3059\u3002<br \/>\n\u4eca\u56de\u306f@sql\u30a2\u30ce\u30c6\u30fc\u30b7\u30e7\u30f3\u3092\u4f7f\u7528\u3057\u3066sql\u30d5\u30a1\u30a4\u30eb\u304b\u3089\u30c7\u30fc\u30bf\u3092\u30bb\u30c3\u30c8\u30a2\u30c3\u30d7\u3057\u3066\u307e\u3059\u3002<br \/>\n\u305f\u3060\u3001@sql\u3067\u3067\u304d\u308b\u306e\u306f\u30c7\u30fc\u30bf\u306e\u30bb\u30c3\u30c8\u30a2\u30c3\u30d7\u7b49\u306e\u4e8b\u524d\u6e96\u5099\u7cfb\u306e\u51e6\u7406\u3067\u3042\u308a\u3001<br \/>\n\u30c6\u30b9\u30c8\u5f8c\u306e\u30c7\u30fc\u30bf\u691c\u8a3c\u3067\u306fJdbcTemplate\u3092\u4f7f\u7528\u3057\u3066\u3044\u307e\u3059\u3002<\/p>\n<h3 id=\"outline__2_1\">\u3010pom.xml\u3011<\/h3>\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>TodoRepository.java<br \/>\n\u30c6\u30b9\u30c8\u5bfe\u8c61\u306fupdate\u30e1\u30bd\u30c3\u30c9<\/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<h3 id=\"outline__2_3\">\u3010\u30c6\u30b9\u30c8\u30af\u30e9\u30b9\u3011<\/h3>\n<p>TodoRepositoryTestVerSql.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 TodoRepositoryTestVerSql {\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        \/\/@sql\u30a2\u30ce\u30c6\u30fc\u30b7\u30e7\u30f3\u3067\u6307\u5b9a\u3057\u305fsql\u30d5\u30a1\u30a4\u30eb\u306b\u3088\u3063\u3066\u30bb\u30c3\u30c8\u30a2\u30c3\u30d7\u3092\u5b9f\u884c\u3059\u308b\u305f\u3081\u3001\u51e6\u7406\u306a\u3057\r\n    }\r\n    \r\n    @Test\r\n    @Rollback\r\n    \/\/@sql\u30a2\u30ce\u30c6\u30fc\u30b7\u30e7\u30f3\u3067sql\u30d5\u30a1\u30a4\u30eb\u3092\u547c\u3073\u51fa\u3057\u3066\u30bb\u30c3\u30c8\u30a2\u30c3\u30d7\u3059\u308b\u3002\r\n    @Sql(&quot;classpath:database\/test_data.sql&quot;)\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<p>@Before\u306e\u4e8b\u524d\u6e96\u5099\u306e\u30bb\u30c3\u30c8\u30a2\u30c3\u30d7\u90e8\u5206\u306e\u8a18\u8f09\u304c\u306a\u304f\u306a\u3063\u305f\u306e\u3067\u3001\u305d\u306e\u5206\u30b3\u30fc\u30c9\u304c\u3059\u3063\u304d\u308a\u3057\u307e\u3057\u305f\u3002<br \/>\n\u3042\u3068\u3053\u3053\u306b\u306f\u8a18\u8f09\u3057\u3066\u306a\u3044\u3067\u3059\u304c\u3001\u30c6\u30fc\u30d6\u30eb\u30c7\u30fc\u30bf\u306e\u4f5c\u6210\u7528\u30e1\u30bd\u30c3\u30c9\u3082\u4e0d\u8981\u306b\u306a\u3063\u3066\u307e\u3059\uff08sql\u30d5\u30a1\u30a4\u30eb\u306b\u5916\u3060\u3057\u3057\u305f\u305f\u3081\uff09<br \/>\n@sql\u306f\u30af\u30e9\u30b9\u5358\u4f4d\u3001\u30e1\u30bd\u30c3\u30c9\u5358\u4f4d\u306e\u6307\u5b9a\u304c\u53ef\u80fd\u3067\u3059\u3002<\/p>\n<h3 id=\"outline__2_4\">\u3010\u305d\u306e\u4ed6\u8a2d\u5b9a\u3011<\/h3>\n<p>test-context.xml<br \/>\njdbctemplate\u3092\u8a2d\u5b9a\u3057\u3066\u307e\u3059<\/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>test_data.sql<\/p>\n<pre class=\"brush: sql; title: ; notranslate\" title=\"\">\r\n\/* define the schemas. *\/\r\nCREATE TABLE IF NOT EXISTS todo (todo_id VARCHAR(36) PRIMARY KEY, todo_title VARCHAR(30), finished BOOLEAN, created_at TIMESTAMP);\r\nDELETE FROM todo;\r\n\r\n\/* load the records. *\/\r\nINSERT INTO todo (todo_id, todo_title, finished, created_at) VALUES ('cceae402-c5b1-440f-bae2-7bee19dc17fb', 'one', false, '2017-10-01 15:39:17.888');\r\nINSERT INTO todo (todo_id, todo_title, finished, created_at) VALUES ('5dd4ba78-ff5b-423b-aa2a-a07118aeaf90', 'two', false, '2017-10-01 15:39:19.981');\r\nINSERT INTO todo (todo_id, todo_title, finished, created_at) VALUES ('e3bdb9af-3dde-40b7-b5fb-4b388567ab45', 'three', false, '2017-10-01 15:39:28.437');\r\ncommit;\r\n<\/pre>\n<p>DB\u4e0a\u306b\u30c6\u30fc\u30d6\u30eb\u304c\u306a\u3051\u308c\u3070\u30c6\u30fc\u30d6\u30eb\u306e\u4f5c\u6210\u304b\u3089\u3084\u3063\u3066\u307e\u3059\u3002<\/p>\n<p>DBunit\u306a\u3069\u306e\u30bb\u30c3\u30c8\u30a2\u30c3\u30d7\u7528\u306e\u30e9\u30a4\u30d6\u30e9\u30ea\u3092\u4f7f\u308f\u306a\u3044\u6642\u306e\u30bb\u30c3\u30c8\u30a2\u30c3\u30d7\u65b9\u6cd5\u3068\u3057\u3066\u3044\u3044\u304b\u3082\u3057\u308c\u307e\u305b\u3093\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-pattern2\">Spring-Unit-Test-pattern2<\/a><\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Spring Framework\u306eUnit\u30c6\u30b9\u30c8\u5b9f\u88c5\u65b9\u6cd5\u30001-2.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,1],"tags":[11,13,10,42,44,41],"class_list":["post-102","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-junit","category-maven","category-mybatis","category-repository-test","category-spring","category-1","tag-junit","tag-mybatis","tag-spring","tag-spring-test","tag-sql","tag-unittest"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/ito-u-oti.com\/index.php?rest_route=\/wp\/v2\/posts\/102","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=102"}],"version-history":[{"count":4,"href":"https:\/\/ito-u-oti.com\/index.php?rest_route=\/wp\/v2\/posts\/102\/revisions"}],"predecessor-version":[{"id":430,"href":"https:\/\/ito-u-oti.com\/index.php?rest_route=\/wp\/v2\/posts\/102\/revisions\/430"}],"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=102"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ito-u-oti.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=102"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ito-u-oti.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=102"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}