{"id":123,"date":"2017-10-14T22:16:48","date_gmt":"2017-10-14T13:16:48","guid":{"rendered":"http:\/\/ito-u-oti.com\/?p=123"},"modified":"2018-05-05T19:50:54","modified_gmt":"2018-05-05T10:50:54","slug":"post-123","status":"publish","type":"post","link":"https:\/\/ito-u-oti.com\/?p=123","title":{"rendered":"Spring Framework\u306eUnit\u30c6\u30b9\u30c8\u5b9f\u88c5\u65b9\u6cd5\u30002-1.Service\u30c6\u30b9\u30c8(Junit4, spring-test)"},"content":{"rendered":"<h2 id=\"outline__1\">Spring Framework\u306eUnit\u30c6\u30b9\u30c8\u5b9f\u88c5\u65b9\u6cd5\u30002-1.Service\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\">2-1.Service\u30c6\u30b9\u30c8\u3000Junit4,Spring-test\u30e9\u30a4\u30d6\u30e9\u30ea\u3092\u4f7f\u7528\u3057\u305f\u30d1\u30bf\u30fc\u30f3<\/h2>\n<p>Service\u30af\u30e9\u30b9\u306e\u30c6\u30b9\u30c8\u3067\u3059\u3002<br \/>\nSpring-test\u30e9\u30a4\u30d6\u30e9\u30ea\u3092\u4f7f\u3046\u3053\u3068\u306b\u3088\u308aDI\u30b3\u30f3\u30c6\u30ca\u3092\u4f7f\u3046\u3053\u3068\u304c\u3067\u304d\u308b\u306e\u3067\u3001<br \/>\nRepository\u30af\u30e9\u30b9\u3068Service\u30af\u30e9\u30b9\u3092\u7e4b\u3052\u305f\u8a66\u9a13\u3092\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002<br \/>\n\u4eca\u56de\u4f7f\u7528\u3057\u3066\u3044\u308b\u30b5\u30f3\u30d7\u30eb\u306e\u4f8b\u3060\u3068\u3001Service\u30af\u30e9\u30b9\u3067Repository\u30af\u30e9\u30b9\u306eUpdate\u30e1\u30bd\u30c3\u30c9\u3092\u547c\u3073\u51fa\u3057\u3066\u3044\u308b\u306e\u3067\u3059\u304c\u3001<br \/>\nRepository\u30af\u30e9\u30b9\u3092\u30e2\u30c3\u30af\u5316\u3057\u306a\u3044\u3067\u305d\u306e\u307e\u307e\u30c6\u30fc\u30d6\u30eb\u306e\u30c7\u30fc\u30bf\u3092\u66f4\u65b0\u3059\u308b\u51e6\u7406\u304c\u8d70\u308b\u611f\u3058\u3067\u3059\u3002<\/p>\n<h3 id=\"outline__2_1\">\u3010pom.xml\u3011<\/h3>\n<p>Junit\u30e9\u30a4\u30d6\u30e9\u30ea\u3068Spring-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>TodoService.java<br \/>\n\u30c6\u30b9\u30c8\u5bfe\u8c61\u306ffinish\u30e1\u30bd\u30c3\u30c9<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npublic interface TodoService {\r\n    Collection&lt;Todo&gt; findAll();\r\n    \r\n    Todo create(Todo todo);\r\n    \r\n    Todo finish(String todoId);\r\n    \r\n    void delete(String todoId);\r\n}\r\n<\/pre>\n<p>\u4eca\u56de\u306e\u30c6\u30b9\u30c8\u30d1\u30bf\u30fc\u30f3\u306e\u5834\u5408\u3001DI\u30b3\u30f3\u30c6\u30ca\u3092\u4f7f\u3046\u306e\u3067\u30c6\u30b9\u30c8\u30e1\u30bd\u30c3\u30c9\u3067\u8a66\u9a13\u3059\u308b\u6642\u306b\u5bfe\u8c61\u3068\u306a\u308b\u306e\u306f\u3053\u306eTodoService\u30a4\u30f3\u30bf\u30fc\u30d5\u30a7\u30fc\u30b9\u306b\u306a\u308a\u307e\u3059\u3002<br \/>\n\u30a4\u30f3\u30bf\u30fc\u30d5\u30a7\u30fc\u30b9\u3092\u5b9f\u88c5\u3057\u305f\u30af\u30e9\u30b9\u3082\u3042\u308b\u306e\u3067\u3059\u304c\u3001\u3053\u3061\u3089\u3092\u76f4\u63a5\u30c6\u30b9\u30c8\u30e1\u30bd\u30c3\u30c9\u3067\u6307\u5b9a\u3057\u3066\u3082DI\u30b3\u30f3\u30c6\u30ca\u306e\u65b9\u3092\u901a\u3089\u305a\u306b\u3046\u307e\u304fRepository\u30af\u30e9\u30b9\u306b\u30a2\u30af\u30bb\u30b9\u3067\u304d\u305a\u30a8\u30e9\u30fc\u3068\u306a\u308a\u307e\u3059\u3002<br \/>\nService\u30a4\u30f3\u30bf\u30fc\u30d5\u30a7\u30fc\u30b9\u3092\u6307\u5b9a\u3057\u3066DI\u30b3\u30f3\u30c6\u30ca\u304b\u3089\u5b9f\u88c5\u30af\u30e9\u30b9\u3092\u547c\u3073\u51fa\u3057\u3066\u8a66\u9a13\u3059\u308b\u3068\u3044\u3046\u3001\u9593\u63a5\u7684\u306b\u4f7f\u7528\u3059\u308b\u30a4\u30e1\u30fc\u30b8\u3067\u3059\u304b\u306d\u3002<br \/>\n\u3061\u306a\u307f\u306b\u5b9f\u88c5\u30af\u30e9\u30b9\u306f\u4ee5\u4e0b\u306e\u30d5\u30a1\u30a4\u30eb\u3067\u3059\u3002<br \/>\n\u30c6\u30b9\u30c8\u5bfe\u8c61\u3068\u306a\u308bfinish\u30e1\u30bd\u30c3\u30c9\u306e\u90e8\u5206\u3060\u3051\u5207\u308a\u51fa\u3057\u307e\u3057\u305f\u3002<\/p>\n<p>TodoServiceImpl.java<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\n@Service\r\n@Transactional\r\npublic class TodoServiceImpl implements TodoService {\r\n\r\n    private static final long MAX_UNFINISHED_COUNT = 5;\r\n\r\n    @Inject\r\n    TodoRepository todoRepository;\r\n\r\n    @Override\r\n    public Todo finish(String todoId) {\r\n        \/\/ TODO \u81ea\u52d5\u751f\u6210\u3055\u308c\u305f\u30e1\u30bd\u30c3\u30c9\u30fb\u30b9\u30bf\u30d6\r\n        Todo todo = findOne(todoId);\r\n        if (todo.isFinished()) {\r\n            ResultMessages messages = ResultMessages.error();\r\n            messages.add(ResultMessage.fromText(&quot;&#x5B;E002]The requested Todo is already finished. (id=&quot; + todoId + &quot;)&quot;));\r\n\r\n            throw new BusinessException(messages);\r\n        }\r\n        todo.setFinished(true);\r\n        todoRepository.update(todo);\r\n        return todo;\r\n    }\r\n}\r\n<\/pre>\n<h3 id=\"outline__2_3\">\u3010\u30c6\u30b9\u30c8\u30af\u30e9\u30b9\u3011<\/h3>\n<p>TodoServiceImplTest.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 TodoServiceImplTest {\r\n\r\n    @Inject\r\n    TodoService target;\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    \/\/\u6b63\u5e38\u306b\u52d5\u4f5c\u3057\u305f\u30d1\u30bf\u30fc\u30f3\r\n    @Test\r\n    @Rollback\r\n    \/\/\u30c6\u30b9\u30c8\u30c7\u30fc\u30bf\u306e\u30bb\u30c3\u30c8\u30a2\u30c3\u30d7\u306b@sql\u30a2\u30ce\u30c6\u30fc\u30b7\u30e7\u30f3\u3092\u4f7f\u3063\u3066\u3044\u308b\r\n    @Sql(&quot;classpath:database\/test_data_testFinishOK.sql&quot;)\r\n    public void testFinishOK() throws Exception{\r\n        \/\/\u5f15\u6570\u8a2d\u5b9a\r\n        String todoId = &quot;cceae402-c5b1-440f-bae2-7bee19dc17fb&quot;;\r\n        \r\n        \/\/finish\u30e1\u30bd\u30c3\u30c9\u306e\u30c6\u30b9\u30c8\r\n        Todo todo = target.finish(todoId);\r\n        \r\n        \/\/\u7d50\u679c\u691c\u8a3c\uff08assertTodo\u30e1\u30bd\u30c3\u30c9\u306f\u30e1\u30bd\u30c3\u30c9\u306e\u5b9f\u884c\u306b\u3088\u3063\u3066\u8fd4\u3063\u3066\u304d\u305fTodo\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3092\u691c\u8a3c\u3059\u308bprivate\u30e1\u30bd\u30c3\u30c9\uff09\r\n        assertTodo(todo);\r\n        \r\n    }\r\n    \r\n    \/\/\u53d6\u5f97\u3057\u305fTodo\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u306efinished\u304c\u65e2\u306btrue\u3067\u7570\u5e38\u304c\u767a\u751f\u3057\u305f\u30d1\u30bf\u30fc\u30f3\r\n    \/\/@Test\u306eexpected\u306b\u306f\u671f\u5f85\u3059\u308bException\u30af\u30e9\u30b9\u3092\u8a2d\u5b9a\u3059\u308b\u3002\u3053\u3046\u3059\u308b\u3053\u3068\u3067Exception\u304c\u767a\u751f\u3059\u308b\u3053\u3068\u306f\u60f3\u5b9a\u901a\u308a\u3068\u3044\u3046\u3053\u3068\u3092\u5ba3\u8a00\u3057\u3066\u3044\u308b\u3002\r\n    @Test(expected = BusinessException.class)\r\n    @Rollback\r\n    \/\/\u30c6\u30b9\u30c8\u30c7\u30fc\u30bf\u306e\u30bb\u30c3\u30c8\u30a2\u30c3\u30d7\u306b@sql\u30a2\u30ce\u30c6\u30fc\u30b7\u30e7\u30f3\u3092\u4f7f\u3063\u3066\u3044\u308b\u3002\u308f\u3056\u3068Exception\u3092\u767a\u751f\u3055\u305b\u308b\u305f\u3081\u3001testFinishOK\u30e1\u30bd\u30c3\u30c9\u306e\u6642\u306e\u30bb\u30c3\u30c8\u30a2\u30c3\u30d7\u3068\u30c7\u30fc\u30bf\u3092\u5909\u3048\u3066\u3044\u308b\u3002\r\n    @Sql(&quot;classpath:database\/test_data_testFinishNG.sql&quot;)\r\n    public void testFinishNG() throws Exception{\r\n        \/\/\u5f15\u6570\u8a2d\u5b9a\r\n        String todoId = &quot;cceae402-c5b1-440f-bae2-7bee19dc17fb&quot;;\r\n        \r\n        \/\/try-catch\u6587\u306f\u306a\u304f\u3066\u3082Junit\u3068\u3057\u3066\u306f\u6b63\u5e38\u306b\u306a\u308b\u304c\u3001printStackTrace\u30e1\u30bd\u30c3\u30c9\u3067\u30a8\u30e9\u30fc\u306e\u5185\u5bb9\u3092\u8868\u793a\u3055\u305b\u3066\u3044\u308b\u3002\r\n        try {\r\n            target.finish(todoId);\r\n        }catch (BusinessException e) {\r\n            \/\/ TODO: handle exception\r\n            e.printStackTrace();\r\n            \r\n            throw e;\r\n        }\r\n    }\r\n}\r\n<\/pre>\n<p>\u60f3\u5b9a\u901a\u308a\u306b\u30c7\u30fc\u30bf\u3092\u53d6\u3063\u3066\u304d\u305f\u6642\u306e\u30d1\u30bf\u30fc\u30f3\u3068\u3001<br \/>\n\u7570\u5e38\u304c\u767a\u751f\u3057\u305f\u6642\u306e\u30d1\u30bf\u30fc\u30f3\u306e2\u30d1\u30bf\u30fc\u30f3\u3092\u8a18\u8ff0\u3057\u307e\u3057\u305f\u3002<br \/>\n\u671f\u5f85\u901a\u308a\u306e\u7570\u5e38\u304c\u767a\u751f\u3057\u305f\u5834\u5408\u306f @Test(expected = Exception\u30af\u30e9\u30b9)\u3068\u8a2d\u5b9a\u3059\u308b\u3053\u3068\u3067\u3001Junit\u306e\u30c6\u30b9\u30c8\u304c\u6210\u529f\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n<h3 id=\"outline__2_4\">\u3010\u305d\u306e\u4ed6\u8a2d\u5b9a\u3011<\/h3>\n<p>test_data_testFinishOK.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>test_data_testFinishNG.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', true, '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>\u3042\u3089\u304b\u3058\u3081finish\u30e1\u30bd\u30c3\u30c9\u5bfe\u8c61\u306e\u30c7\u30fc\u30bf\u3092true\u3068\u3057\u3066\u304a\u304f\u3053\u3068\u3067\u7570\u5e38\u3092\u767a\u751f\u3055\u305b\u308b\u3002<\/p>\n<p>\u4e00\u70b9\u6ce8\u610f\u306a\u306e\u3067\u3059\u304c\u3001BusinessException\u3068\u3044\u3046\u306e\u306fTERASOLUNA\u306e\u6a5f\u80fd\u306b\u306a\u308b\u306e\u3067\u3001<br \/>\nSpring\u306e\u5358\u4f53\u30c6\u30b9\u30c8\u306b\u3064\u3044\u3066\u898b\u306b\u6765\u305f\u65b9\u306f\u300c\u306a\u3093\u3060\u3053\u308c\uff1f\u300d\u3063\u3066\u601d\u3046\u304b\u3082\u3057\u308c\u307e\u305b\u3093\u304c\u7121\u8996\u3057\u3066\u5927\u4e08\u592b\u3067\u3059\u3002<br \/>\n\u3042\u3068ResultMessages\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3068\u304b\u3082TERASOLUNA\u306e\u6a5f\u80fd\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-pattern8\">Spring Unit Test pattern8<\/a><\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Spring Framework\u306eUnit\u30c6\u30b9\u30c8\u5b9f\u88c5\u65b9\u6cd5\u30002-1.Service\u30c6\u30b9\u30c8(Junit4 [&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,51,24,1],"tags":[11,10,42,41],"class_list":["post-123","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-junit","category-service-test","category-spring","category-1","tag-junit","tag-spring","tag-spring-test","tag-unittest"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/ito-u-oti.com\/index.php?rest_route=\/wp\/v2\/posts\/123","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=123"}],"version-history":[{"count":5,"href":"https:\/\/ito-u-oti.com\/index.php?rest_route=\/wp\/v2\/posts\/123\/revisions"}],"predecessor-version":[{"id":439,"href":"https:\/\/ito-u-oti.com\/index.php?rest_route=\/wp\/v2\/posts\/123\/revisions\/439"}],"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=123"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ito-u-oti.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=123"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ito-u-oti.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=123"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}