{"id":1231,"date":"2021-03-31T09:23:55","date_gmt":"2021-03-31T00:23:55","guid":{"rendered":"https:\/\/www.opentone.co.jp\/ot-lab\/?p=1231"},"modified":"2025-05-12T10:06:14","modified_gmt":"2025-05-12T01:06:14","slug":"junit","status":"publish","type":"post","link":"https:\/\/www.opentone.co.jp\/ot-lab\/all\/web-system\/junit","title":{"rendered":"JUnit\u3067\u30e1\u30bd\u30c3\u30c9\u306e\u623b\u308a\u5024\u3092\u6307\u5b9a\u3001\u547c\u3073\u51fa\u3057\u56de\u6570\/\u5f15\u6570\u3092\u691c\u8a3c"},"content":{"rendered":"\n<p>\u3053\u3093\u306b\u3061\u306f\u3002I.T\u3067\u3059\u3002<\/p>\n\n\n\n<p>\u4eca\u56de\u306f\u3001JUnit\u3067\u30e2\u30c3\u30af(JMockit)\u3092\u4f7f\u7528\u3057\u3001\u4e0b\u8a18\u3092\u5b9f\u73fe\u3057\u305f\u3044\u3068\u601d\u3044\u307e\u3059\u3002<br>\u30fb\u691c\u8a3c\u30e1\u30bd\u30c3\u30c9\u306e\u623b\u308a\u5024\u3092\u6307\u5b9a\uff08\u8907\u6570\u56de\u547c\u3073\u51fa\u3057\u306b\u5bfe\u5fdc\uff09<br>\u30fb\u691c\u8a3c\u30e1\u30bd\u30c3\u30c9\u304c\u547c\u3073\u51fa\u3057\u56de\u6570\u3092\u691c\u8a3c<br>\u30fb\u691c\u8a3c\u30e1\u30bd\u30c3\u30c9\u306e\u5f15\u6570\u3092\u691c\u8a3c\uff08\u8907\u6570\u56de\u547c\u3073\u51fa\u3057\u306b\u5bfe\u5fdc\uff09<\/p>\n\n\n\n<p>\u30e2\u30c3\u30af(JMockit)\u306b\u3064\u3044\u3066\u306f<br>\u4e0b\u8a18\u306e\u8a18\u4e8b\u3067\u3082\u89e3\u8aac\u3057\u3066\u304a\u308a\u307e\u3059\u306e\u3067\u3001\u305c\u3072\u3054\u89a7\u304f\u3060\u3055\u3044\u3002<br><a href=\"http:\/\/labs.opentone.co.jp\/?p=6391\" target=\"_blank\" rel=\"noreferrer noopener\">\u5916\u90e8\u30b5\u30fc\u30d0\u3078\u306eHTTP\u30ea\u30af\u30a8\u30b9\u30c8\u3092JUnit\uff08\u30e2\u30c3\u30af\uff09\u3067\u30c6\u30b9\u30c8\u3057\u3088\u3046<\/a><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u691c\u8a3c\u74b0\u5883<\/h3>\n\n\n\n<ul class=\"wp-block-list\"><li>Eclipse 4.4.1<\/li><li>Java 1.7<\/li><li>JUnit 4.12<\/li><li>JMockit 1.17<\/li><li>httpclient 4.4.1<\/li><\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">Eclipse\u52d5\u4f5c\u78ba\u8a8d\u624b\u9806<\/h3>\n\n\n\n<p>Eclipse\u30d7\u30ed\u30b8\u30a7\u30af\u30c8\u3092\u6dfb\u4ed8\u3057\u3066\u304a\u308a\u307e\u3059\u306e\u3067<br>\u52d5\u4f5c\u3092\u78ba\u8a8d\u3057\u306a\u304c\u3089\u3054\u89a7\u3044\u305f\u3060\u3051\u308c\u3070\u5e78\u3044\u3067\u3059\u3002<br><a href=\"http:\/\/labs.opentone.co.jp\/wp-content\/uploads\/2015\/11\/opentone_labs_201511.zip\">opentone_labs_201511.zip<\/a><\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>\u300c\u30d5\u30a1\u30a4\u30eb\u300d-\u300c\u30a4\u30f3\u30dd\u30fc\u30c8\u300d-\u300c\u65e2\u5b58\u30d7\u30ed\u30b8\u30a7\u30af\u30c8\u3092\u30ef\u30fc\u30af\u30b9\u30da\u30fc\u30b9\u3078\u300d\u3092\u9078\u629e<\/li><li>\u300c\u30a2\u30fc\u30ab\u30a4\u30d6\u30fb\u30d5\u30a1\u30a4\u30eb\u306e\u9078\u629e\u300d\u3067opentone_labs_201511.zip\u3092\u9078\u629e\u3057\u30a4\u30f3\u30dd\u30fc\u30c8<\/li><li>\u30d7\u30ed\u30b8\u30a7\u30af\u30c8\u3092\u53f3\u30af\u30ea\u30c3\u30af\u3057\u3001\u300c\u5b9f\u884c\u300d-\u300cJUnit \u30c6\u30b9\u30c8\u300d<\/li><li>JUnit\u304c\u5168\u3066\u7dd1\uff08\u6210\u529f\uff09\u3057\u3066\u3044\u308b\u3053\u3068\u3092\u78ba\u8a8d<\/li><\/ol>\n\n\n\n<h3 class=\"wp-block-heading\">\u30c6\u30b9\u30c8\u5bfe\u8c61\u6a5f\u80fd<\/h3>\n\n\n\n<p>\u4e0b\u8a18\u304c\u30c6\u30b9\u30c8\u5bfe\u8c61\u6a5f\u80fd\u3067\u3059\u3002<br>\u5f15\u6570\u306eURL\u306b\u5bfe\u3057\u3066GET\u3067HTTP\u30ea\u30af\u30a8\u30b9\u30c8\u3092\u884c\u3044\u3001HTTP\u30b9\u30c6\u30fc\u30bf\u30b9\u3092\u8fd4\u5374\u3057\u3066\u3044\u307e\u3059\u3002<br>1\u56de\u76ee\u306e\u30ea\u30af\u30a8\u30b9\u30c8\u306eHTTP\u30b9\u30c6\u30fc\u30bf\u30b9\u304c504 Gateway timeout\u306e\u5834\u5408\u306e\u307f\u30012\u56de\u76ee\u3092\u30ea\u30af\u30a8\u30b9\u30c8\u3057\u3066\u3044\u307e\u3059<\/p>\n\n\n\n<p>\u30fbHttpServiceImpl.java\uff08\u30c6\u30b9\u30c8\u5bfe\u8c61\u6a5f\u80fd\uff09<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">[java]\npublic class HttpServiceImpl implements HttpService {\n    \/**\n     * \u30b3\u30f3\u30b9\u30c8\u30e9\u30af\u30bf\n     *\/\n    public HttpServiceImpl() {\n    }\n\n    @Override\n    public int requestUrl(String url) throws IOException {\n        int statusCode = request(url);\n\n        \/\/ Gateway timeout\u306e\u5834\u5408\u306f\u518d\u5b9f\u884c\n        if (statusCode == HttpStatus.SC_GATEWAY_TIMEOUT) {\n            statusCode = request(url);\n        }\n\n        \/\/ HTTP\u30b9\u30c6\u30fc\u30bf\u30b9\u3092\u8fd4\u5374\n        return statusCode;\n\n    }\n\n    private int request(String url) throws IOException {\n        CloseableHttpClient httpClient = null;\n        CloseableHttpResponse response = null;\n        int statusCode;\n\n        try {\n            \/\/ CloseableHttpClient\u3092\u751f\u6210\n            httpClient = HttpClients.createDefault();\n\n            \/\/ GET\u3067HTTP\u30ea\u30af\u30a8\u30b9\u30c8\u3092\u4f5c\u6210\n            HttpGet request = new HttpGet(url);\n\n            \/\/ HTTP\u30ea\u30af\u30a8\u30b9\u30c8\u3092\u884c\u3044\u3001HTTP\u30ec\u30b9\u30dd\u30f3\u30b9\u3092\u53d6\u5f97\n            response = httpClient.execute(request);\n\n            \/\/ HTTP\u30ec\u30b9\u30dd\u30f3\u30b9\u3088\u308aHTTP\u30b9\u30c6\u30fc\u30bf\u30b9\u3092\u53d6\u5f97\n            statusCode = response.getStatusLine().getStatusCode();\n\n        } finally {\n            if (httpClient != null) {\n                \/\/ CloseableHttpClient\u3092close\n                httpClient.close();\n            }\n\n            if (response != null) {\n                \/\/ CloseableHttpResponse\u3092close\n                response.close();\n            }\n        }\n        \/\/ HTTP\u30b9\u30c6\u30fc\u30bf\u30b9\u3092\u8fd4\u5374\n        return statusCode;\n    }\n}\n[\/java]<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u30c6\u30b9\u30c8\u30b3\u30fc\u30c9<\/h3>\n\n\n\n<p>\u30c6\u30b9\u30c8\u5bfe\u8c61\u6a5f\u80fd\u306b\u304a\u3044\u3066HTTP\u30ea\u30af\u30a8\u30b9\u30c8\u3092\u884c\u3063\u3066\u3044\u308bhttpClient.execute\u30e1\u30bd\u30c3\u30c9\u306b\u3064\u3044\u3066<br>\u4e0b\u8a183\u70b9\u306e\u691c\u8a3c\u3092\u3057\u305f\u3044\u3068\u601d\u3044\u307e\u3059\uff08\u203bHttpClient\u306b\u3064\u3044\u3066\u306f<a href=\"http:\/\/hc.apache.org\/httpcomponents-client-ga\/httpclient\/apidocs\/org\/apache\/http\/client\/HttpClient.html\" target=\"_blank\" rel=\"noreferrer noopener\">\u3053\u3061\u3089<\/a>\u3092\u53c2\u7167)<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>1\u56de\u76ee\u306e\u30ea\u30af\u30a8\u30b9\u30c8\u304c504 Gateway timeout\u3068\u306a\u3063\u305f\u5834\u5408\u3001httpClient.execute\u304c2\u56de\u547c\u3070\u308c\u3066\u3044\u308b\u3053\u3068<\/li><li>2\u56de\u3068\u3082GET\u3067\u30ea\u30af\u30a8\u30b9\u30c8\u3055\u308c\u3066\u3044\u308b\u3053\u3068<\/li><li>2\u56de\u3068\u3082\u5f15\u6570\u306eURL\uff08\u2605dummy\u2605\uff09\u304c\u6307\u5b9a\u3055\u308c\u3066\u3044\u308b\u3053\u3068<\/li><\/ol>\n\n\n\n<p>\u300cnew NonStrictExpectations() \u300d\u306e\u7b87\u6240\u3067\u691c\u8a3c\u30e1\u30bd\u30c3\u30c9\u306e\u623b\u308a\u5024\u3092\u6307\u5b9a\u3057\u307e\u3059\u3002<br>result\u306b\u8907\u6570\u56de\u6307\u5b9a\u3059\u308b\u3053\u3068\u3067\u30011\u56de\u76ee\u306fHTTP\u30b9\u30c6\u30fc\u30bf\u30b9\uff1d504 Gateway timeout\u30012\u56de\u76ee\u306f200 OK\u304c\u8fd4\u5374\u30fb\u30fb\u30fb\u306e\u3088\u3046\u306b\u8a2d\u5b9a\u3067\u304d\u307e\u3059\u3002<\/p>\n\n\n\n<p>\u300cnew Verifications()\u300d\u306e\u7b87\u6240\u3067\u691c\u8a3c\u30e1\u30bd\u30c3\u30c9\u304c\u547c\u3073\u51fa\u3055\u308c\u305f\u56de\u6570\u3068\u5f15\u6570\u3092\u691c\u8a3c\u3057\u307e\u3059\u3002<br>\u56de\u6570\u306f\u3001\u300ctimes = 2\u300d\u3067\u691c\u8a3c\u3057\u3066\u3044\u307e\u3059\u3002<br>\u5f15\u6570\u306f\u3001httpClient.execute\u306e\u5f15\u6570\u306b\u6307\u5b9a\u3055\u308c\u305f\u5185\u5bb9\u304cactualHttpUriRequestList\u306b\u30ea\u30b9\u30c8\u3067\u683c\u7d0d\u3055\u308c\u308b\u306e\u3067\u3001\u305d\u308c\u3092\u691c\u8a3c\u3057\u3066\u3044\u307e\u3059\u3002<\/p>\n\n\n\n<p>\u30fbUnusedMockedHttpServiceTest.java<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">[java]\n    @Test\n    public void \u30e2\u30c3\u30af\u306e\u547c\u3070\u308c\u305f\u56de\u6570\u3068\u30e2\u30c3\u30af\u306b\u6e21\u3055\u308c\u305f\u30d1\u30e9\u30e1\u30fc\u30bf\u3092\u691c\u8a3c() throws Exception {\n        \/\/ 1\u56de\u76ee\u306fHTTP\u30b9\u30c6\u30fc\u30bf\u30b9504 Gateway timeout\u30012\u56de\u76ee\u306f200 OK\u304c\u8fd4\u5374\u3055\u308c\u308b\u3088\u3046\u30e2\u30c3\u30af\u306b\u8a2d\u5b9a\n        new NonStrictExpectations() {\n            {\n                \/\/ \u30e2\u30c3\u30af\u306e\u5bfe\u8c61\u30e1\u30bd\u30c3\u30c9\u3092\u6307\u5b9a\n                mockHttpClient.execute((HttpUriRequest) any);\n\n                \/\/ CloseableHttpClient#execute\u306e\u623b\u308a\u5024\u3092\u751f\u6210\n                result = new HttpResponseImplDummy(HttpStatus.SC_GATEWAY_TIMEOUT);\n                result = new HttpResponseImplDummy(HttpStatus.SC_OK);\n            }\n        };\n\n        \/\/ \u30e2\u30c3\u30af\u3092\u4f7f\u7528\u3059\u308b\u306e\u3067\u3001\u5b9f\u969b\u306b\u306f \u2605dummy\u2605 \u306b\u30ea\u30af\u30a8\u30b9\u30c8\u3055\u308c\u306a\u3044\n        int actual = httpService.requestUrl(\"\u2605dummy\u2605\");\n\n        \/\/ HTTP\u30b9\u30c6\u30fc\u30bf\u30b9\u304c200 OK\u3067\u3042\u308b\u3053\u3068\n        assertThat(actual, is(HttpStatus.SC_OK));\n\n        \/\/ \u30e2\u30c3\u30af\u304c\u671f\u5f85\u901a\u308a\u306b\u547c\u3070\u308c\u305f\u304b\u691c\u8a3c\n        new Verifications() {\n            {\n                \/\/ \u547c\u3070\u308c\u305f\u6642\u306e\u5f15\u6570\u3092\u53d6\u5f97\n                List&lt;HttpUriRequest&gt; actualHttpUriRequestList = new ArrayList&lt;&gt;();\n                mockHttpClient.execute(withCapture(actualHttpUriRequestList));\n\n                \/\/ \u5bfe\u8c61\u30e1\u30bd\u30c3\u30c9\u304c\u547c\u3070\u308c\u305f\u56de\u6570\u304c2\u56de\u3067\u3042\u308b\u3053\u3068\n                times = 2;\n\n                \/\/ 1\u56de\u76ee\uff1aGET\u3067\u30ea\u30af\u30a8\u30b9\u30c8\u3055\u308c\u3066\u3044\u308b\u3053\u3068\n                assertThat(actualHttpUriRequestList.get(0), instanceOf(HttpGet.class));\n\n                \/\/ 1\u56de\u76ee\uff1aURI\u304c\u671f\u5f85\u901a\u308a\u3067\u3042\u308b\u3053\u3068\n                assertThat(actualHttpUriRequestList.get(0).getURI().toString(), is(\"\u2605dummy\u2605\"));\n\n                \/\/ 2\u56de\u76ee\uff1aGET\u3067\u30ea\u30af\u30a8\u30b9\u30c8\u3055\u308c\u3066\u3044\u308b\u3053\u3068\n                assertThat(actualHttpUriRequestList.get(1), instanceOf(HttpGet.class));\n\n                \/\/ 2\u56de\u76ee\uff1aURI\u304c\u671f\u5f85\u901a\u308a\u3067\u3042\u308b\u3053\u3068\n                assertThat(actualHttpUriRequestList.get(1).getURI().toString(), is(\"\u2605dummy\u2605\"));\n\n            }\n        };\n    }\n[\/java]<\/pre>\n\n\n\n<p>\u30e2\u30c3\u30af(JMockit)\u6d3b\u7528\u306b\u3088\u308a<br>\u30fb\u691c\u8a3c\u30e1\u30bd\u30c3\u30c9\u306e\u623b\u308a\u5024\u3092\u6307\u5b9a\uff08\u8907\u6570\u56de\u547c\u3073\u51fa\u3057\u306b\u5bfe\u5fdc\uff09<br>\u30fb\u691c\u8a3c\u30e1\u30bd\u30c3\u30c9\u304c\u547c\u3073\u51fa\u3057\u56de\u6570\u3092\u691c\u8a3c<br>\u30fb\u691c\u8a3c\u30e1\u30bd\u30c3\u30c9\u306e\u5f15\u6570\u3092\u691c\u8a3c\uff08\u8907\u6570\u56de\u547c\u3073\u51fa\u3057\u306b\u5bfe\u5fdc\uff09<br>\u304c\u5b9f\u73fe\u3067\u304d\u307e\u3057\u305f\u3002<\/p>\n<div class=\"opent-_2\" style=\"margin-top: 10px;margin-right: 10px;margin-bottom: 10px;margin-left: 10px;\" id=\"opent-2557140593\"><a href=\"https:\/\/www.opentone.co.jp\/backlog\/\" aria-label=\"twitter_backlogskt_new\"><img src=\"https:\/\/www.opentone.co.jp\/ot-lab\/wp-content\/uploads\/sites\/11\/2023\/02\/twitter_backlogskt_new.jpg\" alt=\"\"  srcset=\"https:\/\/www.opentone.co.jp\/ot-lab\/wp-content\/uploads\/sites\/11\/2023\/02\/twitter_backlogskt_new.jpg 1024w, https:\/\/www.opentone.co.jp\/ot-lab\/wp-content\/uploads\/sites\/11\/2023\/02\/twitter_backlogskt_new-300x157.jpg 300w, https:\/\/www.opentone.co.jp\/ot-lab\/wp-content\/uploads\/sites\/11\/2023\/02\/twitter_backlogskt_new-768x401.jpg 768w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" width=\"384\" height=\"200\"   \/><\/a><\/div>","protected":false},"excerpt":{"rendered":"<p>\u3053\u3093\u306b\u3061\u306f\u3002I.T\u3067\u3059\u3002 \u4eca\u56de\u306f\u3001JUnit\u3067\u30e2\u30c3\u30af(JMockit)\u3092\u4f7f\u7528\u3057\u3001\u4e0b\u8a18\u3092\u5b9f\u73fe\u3057\u305f\u3044\u3068\u601d<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"_uag_custom_page_level_css":"","_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[28],"tags":[11],"class_list":["post-1231","post","type-post","status-publish","format-standard","hentry","category-web-system","tag-java",""],"acf":[],"aioseo_notices":[],"uagb_featured_image_src":{"full":false,"thumbnail":false,"medium":false,"medium_large":false,"large":false,"1536x1536":false,"2048x2048":false,"engage-mag-carousel-img":false,"engage-mag-carousel-img-landscape":false,"engage-mag-carousel-large-img":false,"engage-mag-carousel-large-img-landscape":false,"engage-mag-large-thumb":false,"engage-mag-small-thumb":false},"uagb_author_info":{"display_name":"ah106rx4o4","author_link":"https:\/\/www.opentone.co.jp\/ot-lab\/author\/ah106rx4o4"},"uagb_comment_info":0,"uagb_excerpt":"\u3053\u3093\u306b\u3061\u306f\u3002I.T\u3067\u3059\u3002 \u4eca\u56de\u306f\u3001JUnit\u3067\u30e2\u30c3\u30af(JMockit)\u3092\u4f7f\u7528\u3057\u3001\u4e0b\u8a18\u3092\u5b9f\u73fe\u3057\u305f\u3044\u3068\u601d","_links":{"self":[{"href":"https:\/\/www.opentone.co.jp\/ot-lab\/wp-json\/wp\/v2\/posts\/1231","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.opentone.co.jp\/ot-lab\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.opentone.co.jp\/ot-lab\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.opentone.co.jp\/ot-lab\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.opentone.co.jp\/ot-lab\/wp-json\/wp\/v2\/comments?post=1231"}],"version-history":[{"count":1,"href":"https:\/\/www.opentone.co.jp\/ot-lab\/wp-json\/wp\/v2\/posts\/1231\/revisions"}],"predecessor-version":[{"id":1232,"href":"https:\/\/www.opentone.co.jp\/ot-lab\/wp-json\/wp\/v2\/posts\/1231\/revisions\/1232"}],"wp:attachment":[{"href":"https:\/\/www.opentone.co.jp\/ot-lab\/wp-json\/wp\/v2\/media?parent=1231"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.opentone.co.jp\/ot-lab\/wp-json\/wp\/v2\/categories?post=1231"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.opentone.co.jp\/ot-lab\/wp-json\/wp\/v2\/tags?post=1231"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}