{"id":105,"date":"2017-10-09T20:52:34","date_gmt":"2017-10-09T11:52:34","guid":{"rendered":"http:\/\/ito-u-oti.com\/?p=105"},"modified":"2018-05-05T19:15:38","modified_gmt":"2018-05-05T10:15:38","slug":"post-105","status":"publish","type":"post","link":"https:\/\/ito-u-oti.com\/?p=105","title":{"rendered":"Spring Framework\u306eUnit\u30c6\u30b9\u30c8\u5b9f\u88c5\u65b9\u6cd5\u30001-3.Repository\u30c6\u30b9\u30c8(Junit4, spring-test, DBUnit)"},"content":{"rendered":"<h2 id=\"outline__1\">Spring Framework\u306eUnit\u30c6\u30b9\u30c8\u5b9f\u88c5\u65b9\u6cd5\u30001-3.Repository\u30c6\u30b9\u30c8(Junit4, spring-test, DBUnit)<\/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-3.Repository\u30c6\u30b9\u30c8\u3000Junit4,Spring-test,DBUnit\u30e9\u30a4\u30d6\u30e9\u30ea\u3092\u4f7f\u7528\u3057\u305f\u30d1\u30bf\u30fc\u30f3<\/h2>\n<p>\u30bb\u30c3\u30c8\u30a2\u30c3\u30d7\u3092\u7c21\u5358\u306b\u3059\u308b\u305f\u3081\u306b\u3001DBUnit\u3092\u4f7f\u3063\u3066xml\u30d5\u30a1\u30a4\u30eb\u304b\u3089\u30c7\u30fc\u30bf\u306e\u30bb\u30c3\u30c8\u30a2\u30c3\u30d7\u3092\u884c\u3044\u307e\u3059\u3002<\/p>\n<h3 id=\"outline__2_1\">\u3010pom.xml\u3011<\/h3>\n<p>DBUnit\u30e9\u30a4\u30d6\u30e9\u30ea\u3092\u4f7f\u3046\u305f\u3081\u306b\u8ffd\u52a0\u3057\u3066\u307e\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\r\n    &lt;dependency&gt;\r\n        &lt;groupId&gt;org.dbunit&lt;\/groupId&gt;\r\n        &lt;artifactId&gt;dbunit&lt;\/artifactId&gt;\r\n        &lt;version&gt;2.5.4&lt;\/version&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>TodoRepositoryTestVerDBunit.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-dbunit.xml&quot;})\r\n@Transactional\r\npublic class TodoRepositoryTestVerDBunit extends DataSourceBasedDBTestCase {\r\n\r\n    private final String RESOURCE_DIR = &quot;src\/test\/resources\/META-INF\/dbunit\/&quot;;\r\n    \r\n    @Inject\r\n    TodoRepository target;\r\n    \r\n    @Inject\r\n    JdbcTemplate jdbctemplate;\r\n    \r\n    @Inject\r\n    private TransactionAwareDataSourceProxy dataSourceTest;\r\n    \r\n    @Before\r\n    public void setUp() throws Exception {\r\n        super.setUp();\r\n        \r\n        \/* \u610f\u5473\u306a\u3044\u304b\u3082\u3057\u308c\u306a\u3044\u3051\u3069\u3001\u3053\u3053\u3067\u5b9f\u884c\u3059\u308b\u3053\u3068\u3067\u30d5\u30a1\u30a4\u30eb\u3068\uff24\uff22\u306e\u683c\u7d0d\u5f8c\u306e\u6574\u5408\u6027\u78ba\u8a8d\u304c\u3067\u304d\u308b\u3002\r\n        IDataSet databaseDataSet = getConnection().createDataSet();\r\n        ITable actualTable = databaseDataSet.getTable(&quot;todo&quot;);\r\n        IDataSet expectedDataSet = new FlatXmlDataSetBuilder().build(new File(RESOURCE_DIR + &quot;test_data.xml&quot;));\r\n        ITable expectedTable = expectedDataSet.getTable(&quot;todo&quot;);\r\n        Assertion.assertEquals(expectedTable, actualTable);\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\uff08getTodoData\u30e1\u30bd\u30c3\u30c9\u306fDB\u304b\u3089\u30c7\u30fc\u30bf\u3092\u53d6\u5f97\u3059\u308bprivate\u30e1\u30bd\u30c3\u30c9\u3002\u53d6\u5f97\u3057\u305f\u30c7\u30fc\u30bf\u3092\u66f8\u304d\u63db\u3048\u3066\u66f4\u65b0\u3059\u308b\u3002\uff09\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        \/\/\u30e1\u30bd\u30c3\u30c9\u5b9f\u884c\u5f8c\u30c6\u30fc\u30d6\u30eb\u30c7\u30fc\u30bf\u691c\u8a3c\r\n        \/\/\u3053\u3053\u3067\u6307\u5b9a\u3057\u305fxml\u30d5\u30a1\u30a4\u30eb\u306b\u306f\u3001\u30c6\u30b9\u30c8\u5bfe\u8c61\u306e\u30e1\u30bd\u30c3\u30c9\u5b9f\u884c\u5f8c\u306b\u66f4\u65b0\u3055\u308c\u305f\u30c6\u30fc\u30d6\u30eb\u306e\u5185\u5bb9\u306e\u671f\u5f85\u5024\u3092\u8a18\u8ff0\u3057\u3066\u3044\u308b\r\n        Assertion.assertEquals(getexpectedTable(RESOURCE_DIR + &quot;compare_data.xml&quot;), getactualTable());\r\n        \r\n    }\r\n    \r\n    \/\/\u30c6\u30fc\u30d6\u30eb\u304b\u3089\u30c7\u30fc\u30bf\u3092\u53d6\u5f97\r\n    private ITable getactualTable() throws Exception {\r\n        \r\n        IDataSet databaseDataSet = getConnection().createDataSet();\r\n        ITable actualTable = databaseDataSet.getTable(&quot;todo&quot;);\r\n        \r\n        return actualTable;\r\n    }\r\n    \r\n    \/\/\u671f\u5f85\u5024\u3068\u306a\u308b\u30c7\u30fc\u30bf\u3092\u30d5\u30a1\u30a4\u30eb\u304b\u3089\u53d6\u5f97\u3059\u308b\u30e1\u30bd\u30c3\u30c9\u3002\u5f15\u6570\u306f\u30d5\u30a1\u30a4\u30eb\u306e\u30d1\u30b9\r\n    private ITable getexpectedTable(String filepath) throws Exception  {\r\n        \r\n        IDataSet expectedDataSet = new FlatXmlDataSetBuilder().build(new File(filepath));\r\n        ITable expectedTable = expectedDataSet.getTable(&quot;todo&quot;);\r\n        \r\n        return expectedTable;\r\n    }\r\n    \r\n    \/\/DBunit\u3092\u4f7f\u7528\u3059\u308b\u305f\u3081\u306e\u30aa\u30fc\u30d0\u30fc\u30e9\u30a4\u30c9\r\n    @Override\r\n    protected DataSource getDataSource() {\r\n        \/\/ TODO \u81ea\u52d5\u751f\u6210\u3055\u308c\u305f\u30e1\u30bd\u30c3\u30c9\u30fb\u30b9\u30bf\u30d6\r\n        return dataSourceTest;\r\n    }\r\n    \r\n    \/\/DBunit\u3092\u4f7f\u7528\u3059\u308b\u305f\u3081\u306e\u30aa\u30fc\u30d0\u30fc\u30e9\u30a4\u30c9\r\n    @Override\r\n    protected IDataSet getDataSet() throws Exception {\r\n        \/\/ TODO \u81ea\u52d5\u751f\u6210\u3055\u308c\u305f\u30e1\u30bd\u30c3\u30c9\u30fb\u30b9\u30bf\u30d6\r\n        return new FlatXmlDataSetBuilder().build(new FileInputStream(RESOURCE_DIR + &quot;test_data.xml&quot;));\r\n    }\r\n}\r\n<\/pre>\n<p>DBUnit\u306e\u51e6\u7406\u306b\u3064\u3044\u3066\u3001xml\u30d5\u30a1\u30a4\u30eb\u3092\u8aad\u307f\u8fbc\u3080\u3088\u3046\u306b\u30e1\u30bd\u30c3\u30c9\u3092\u30aa\u30fc\u30d0\u30fc\u30e9\u30a4\u30c9\u3057\u3066\u307e\u3059\u3002<\/p>\n<h3 id=\"outline__2_4\">\u3010\u305d\u306e\u4ed6\u8a2d\u5b9a\u3011<\/h3>\n<p>test-context-dbunit.xml<br \/>\nDBunit\u3092\u6709\u52b9\u306b\u3059\u308b\u305f\u3081\u306b\u5c11\u3057test-context.xml\uff08<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%801-1-repository%E3%83%86%E3%82%B9%E3%83%88junit4-spring-test\/\">1-1\u53c2\u7167<\/a>\uff09\u3092\u66f8\u304d\u63db\u3048\u3066\u307e\u3059\u3002<br \/>\n\u3053\u308c\u3092\u3057\u306a\u3044\u3068DBunit\u304c\u3046\u307e\u304f\u52d5\u3044\u3066\u304f\u308c\u306a\u3044\u3089\u3057\u3044\u30fb\u30fb\u30fb<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n&lt;bean id=&quot;log4jDataSource&quot; class=&quot;net.sf.log4jdbc.Log4jdbcProxyDataSource&quot;&gt;\r\n    &lt;constructor-arg index=&quot;0&quot; ref=&quot;realDataSource&quot; \/&gt;\r\n&lt;\/bean&gt;\r\n    \r\n&lt;bean id=&quot;dataSource&quot; class=&quot;org.springframework.jdbc.datasource.TransactionAwareDataSourceProxy&quot;&gt;\r\n    &lt;constructor-arg index=&quot;0&quot; ref=&quot;log4jDataSource&quot; \/&gt;\r\n&lt;\/bean&gt;\r\n<\/pre>\n<p>test_data.xml<br \/>\n\u30bb\u30c3\u30c8\u30a2\u30c3\u30d7\u7528\u306e\u30c7\u30fc\u30bf\u3067\u3059\u3002<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n&lt;?xml version='1.0' encoding='UTF-8'?&gt;\r\n&lt;dataset&gt;\r\n    &lt;TODO TODO_ID=&quot;cceae402-c5b1-440f-bae2-7bee19dc17fb&quot; TODO_TITLE=&quot;one&quot; FINISHED=&quot;false&quot; CREATED_AT=&quot;2017-10-01 15:39:17.888&quot; \/&gt;\r\n    &lt;TODO TODO_ID=&quot;5dd4ba78-ff5b-423b-aa2a-a07118aeaf90&quot; TODO_TITLE=&quot;two&quot; FINISHED=&quot;false&quot; CREATED_AT=&quot;2017-10-01 15:39:19.981&quot; \/&gt;\r\n    &lt;TODO TODO_ID=&quot;e3bdb9af-3dde-40b7-b5fb-4b388567ab45&quot; TODO_TITLE=&quot;three&quot; FINISHED=&quot;false&quot; CREATED_AT=&quot;2017-10-01 15:39:28.437&quot; \/&gt;\r\n&lt;\/dataset&gt;\r\n<\/pre>\n<p>compare_data.xml<br \/>\n\u30c6\u30b9\u30c8\u30e1\u30bd\u30c3\u30c9\u5b9f\u884c\u5f8c\u306e\u691c\u8a3c\u7528\u30c7\u30fc\u30bf\u3067\u3059\u3002<br \/>\n\u30c6\u30b9\u30c8\u30e1\u30bd\u30c3\u30c9\u306e\u5185\u5bb9\u304c\u3001ID&#8221;cceae402-c5b1-440f-bae2-7bee19dc17fb&#8221;\u306e\u30c7\u30fc\u30bf\u306b\u5bfe\u3057\u3066Finished\u3092true\u306b\u3059\u308b\u5185\u5bb9\u306a\u306e\u3067\u3001<br \/>\ntest_data.xml\u306e\u5185\u5bb9\u304b\u3089ID=&#8221;cceae402-c5b1-440f-bae2-7bee19dc17fb&#8221;\u306eFINISHED\u3092&#8221;true&#8221;\u306b\u5909\u66f4\u3057\u3066\u3044\u307e\u3059\u3002<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n&lt;?xml version='1.0' encoding='UTF-8'?&gt;\r\n&lt;dataset&gt;\r\n    &lt;TODO TODO_ID=&quot;5dd4ba78-ff5b-423b-aa2a-a07118aeaf90&quot; TODO_TITLE=&quot;two&quot; FINISHED=&quot;false&quot; CREATED_AT=&quot;2017-10-01 15:39:19.981&quot; \/&gt;\r\n    &lt;TODO TODO_ID=&quot;cceae402-c5b1-440f-bae2-7bee19dc17fb&quot; TODO_TITLE=&quot;one&quot; FINISHED=&quot;true&quot; CREATED_AT=&quot;2017-10-01 15:39:17.888&quot; \/&gt;\r\n    &lt;TODO TODO_ID=&quot;e3bdb9af-3dde-40b7-b5fb-4b388567ab45&quot; TODO_TITLE=&quot;three&quot; FINISHED=&quot;false&quot; CREATED_AT=&quot;2017-10-01 15:39:28.437&quot; \/&gt;\r\n&lt;\/dataset&gt;\r\n<\/pre>\n<p>xml\u3067\u3069\u3093\u3069\u3093\u30c7\u30fc\u30bf\u3092\u66f8\u3044\u3066\u3044\u3051\u3070\u3044\u3044\u306e\u3067\u3001\u5927\u91cf\u30c7\u30fc\u30bf\u306e\u30bb\u30c3\u30c8\u30a2\u30c3\u30d7\u3068\u304b\u3082\u542b\u3081\u3066\u3084\u3063\u3071\u308aDBUnit\u306b\u3088\u308b\u30bb\u30c3\u30c8\u30a2\u30c3\u30d7\u304c\u304a\u3059\u3059\u3081\u3067\u3059\u306d\u3002<br \/>\n\u30c7\u30fc\u30bf\u306e\u691c\u8a3c\u3082\u3084\u3063\u3066\u304f\u308c\u308b\u3057\u3002<br \/>\n\u3061\u306a\u307f\u306b\u30c6\u30b9\u30c8\u30e1\u30bd\u30c3\u30c9\u5b9f\u884c\u5f8c\u306e\u30c6\u30fc\u30d6\u30eb\u30c7\u30fc\u30bf\u3068\u3001compare_data.xml\u306e\u5185\u5bb9\u304c\u7570\u306a\u3063\u3066\u3044\u305f\u5834\u5408\u3001<br \/>\nJunit\u4e0a\u3067\u30a8\u30e9\u30fc\u306b\u306a\u308a\u307e\u3059\u3002\uff08\u4f8b\u306e\u30d0\u30fc\u304c\u8d64\u304f\u306a\u308b\uff09<br \/>\n\u3061\u3087\u3063\u3068\u9762\u5012\u306a\u306e\u304c\u3001compare_data.xml\u306e\u4e2d\u306e\u30c7\u30fc\u30bf\u306e\u9806\u756a\u307e\u3067\u5408\u308f\u305b\u306a\u304d\u3083\u3044\u3051\u306a\u3044\u3053\u3068\u3067\u3059\u3002\u3002\u3002<br \/>\n\u81ea\u5206\u304c\u3084\u3063\u305f\u9650\u308a\u3067\u306f\u66f4\u65b0\u5f8c\u306e\u30c6\u30fc\u30d6\u30eb\u306e\u9806\u756a\u306b\u3061\u3083\u3093\u3068\u5408\u308f\u305b\u306a\u3044\u3068\u30a8\u30e9\u30fc\u306b\u306a\u308b\u306e\u3067\u3059\u304c\u3001<br \/>\n\u30c7\u30fc\u30bf\u306e\u6574\u5408\u6027\u3060\u3051\u898b\u3066\u304f\u308c\u308b\u3088\u3046\u306b\u8a2d\u5b9a\u3068\u304b\u3067\u304d\u306a\u3044\u306e\u3067\u3059\u304b\u306d\u30fb\u30fb\u30fb<\/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-pattern3\">Spring-Unit-Test-pattern3<\/a><\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Spring Framework\u306eUnit\u30c6\u30b9\u30c8\u5b9f\u88c5\u65b9\u6cd5\u30001-3.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":[45,27,25,28,40,24],"tags":[16,11,13,10,42,41],"class_list":["post-105","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-dbunit","category-junit","category-maven","category-mybatis","category-repository-test","category-spring","tag-dbunit","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\/105","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=105"}],"version-history":[{"count":4,"href":"https:\/\/ito-u-oti.com\/index.php?rest_route=\/wp\/v2\/posts\/105\/revisions"}],"predecessor-version":[{"id":431,"href":"https:\/\/ito-u-oti.com\/index.php?rest_route=\/wp\/v2\/posts\/105\/revisions\/431"}],"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=105"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ito-u-oti.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=105"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ito-u-oti.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=105"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}