{"id":203185,"date":"2025-05-22T10:10:18","date_gmt":"2025-05-22T02:10:18","guid":{"rendered":"https:\/\/server.hk\/cnblog\/203185\/"},"modified":"2025-05-22T10:10:18","modified_gmt":"2025-05-22T02:10:18","slug":"%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8-junit-5-%e4%b8%ba%e5%90%8c%e4%b8%80%e6%95%b0%e6%8d%ae%e9%9b%86%e6%b5%8b%e8%af%95%e4%b8%8d%e5%90%8c%e6%96%b9%e6%b3%95%ef%bc%8c%e9%81%bf%e5%85%8d%e9%87%8d%e5%a4%8d","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/203185\/","title":{"rendered":"\u5982\u4f55\u4f7f\u7528 JUnit 5 \u4e3a\u540c\u4e00\u6570\u636e\u96c6\u6d4b\u8bd5\u4e0d\u540c\u65b9\u6cd5\uff0c\u907f\u514d\u91cd\u590d\u8f93\u5165\u6d4b\u8bd5\u7528\u4f8b\uff1f"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>\u5982\u4f55\u4f7f\u7528 JUnit 5 \u4e3a\u540c\u4e00\u6570\u636e\u96c6\u6d4b\u8bd5\u4e0d\u540c\u65b9\u6cd5\uff0c\u907f\u514d\u91cd\u590d\u8f93\u5165\u6d4b\u8bd5\u7528\u4f8b\uff1f<\/h1>\n<p>\u5728IT\u884c\u4e1a\u8fd9\u4e2a\u53d1\u5c55\u66f4\u65b0\u901f\u5ea6\u5f88\u5feb\u7684\u884c\u4e1a\uff0c\u53ea\u6709\u4e0d\u505c\u6b62\u7684\u5b66\u4e60\uff0c\u624d\u4e0d\u4f1a\u88ab\u884c\u4e1a\u6240\u6dd8\u6c70\u3002\u5982\u679c\u4f60\u662f\u6587\u7ae0\u5b66\u4e60\u8005\uff0c\u90a3\u4e48\u672c\u6587\u300a\u5982\u4f55\u4f7f\u7528 JUnit 5 \u4e3a\u540c\u4e00\u6570\u636e\u96c6\u6d4b\u8bd5\u4e0d\u540c\u65b9\u6cd5\uff0c\u907f\u514d\u91cd\u590d\u8f93\u5165\u6d4b\u8bd5\u7528\u4f8b\uff1f\u300b\u5c31\u5f88\u9002\u5408\u4f60\uff01\u672c\u7bc7\u5185\u5bb9\u4e3b\u8981\u5305\u62ec##content_title##\uff0c\u5e0c\u671b\u5bf9\u5927\u5bb6\u7684\u77e5\u8bc6\u79ef\u7d2f\u6709\u6240\u5e2e\u52a9\uff0c\u52a9\u529b\u5b9e\u6218\u5f00\u53d1\uff01<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241031\/173037800067237910e41bd.jpg\" class=\"aligncenter\"><\/p>\n<h3>\u5982\u4f55\u4f7f\u7528junit 5\u4e3a\u540c\u4e00\u6570\u636e\u6837\u4f8b\u6d4b\u8bd5\u4e0d\u540c\u65b9\u6cd5\uff1f<\/h3>\n<p><strong>\u95ee\u9898\uff1a<\/strong><\/p>\n<p>\u5e0c\u671b\u4e3a\u540c\u4e00\u6570\u636e\u96c6\u6d4b\u8bd5\u4e0d\u540c\u65b9\u6cd5\uff0c\u4f46\u4e0d\u60f3\u91cd\u590d\u8f93\u5165\u6d4b\u8bd5\u7528\u4f8b\u3002<\/p>\n<p><strong>\u89e3\u51b3\u65b9\u6848\uff1a<\/strong><\/p>\n<p>\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528\u6ce8\u89e3 @beforeeach \u5728\u6bcf\u6b21\u6d4b\u8bd5\u65b9\u6cd5\u4e4b\u524d\u521d\u59cb\u5316\u6d4b\u8bd5\u6570\u636e\u6765\u5b9e\u73b0\u6b64\u76ee\u6807\u3002<\/p>\n<p><strong>\u4fee\u6539\u540e\u7684\u6d4b\u8bd5\u7c7b\uff1a<\/strong><\/p>\n<pre>import algorithm.sort_.MySortAlgorithm_Main;\nimport org.junit.jupiter.api.*;\n\n@TestInstance(TestInstance.Lifecycle.PER_CLASS)\npublic class Sort_Test3 {\n\n    private int[] testData;\n    private int[] runTest_Data;\n\n    @BeforeAll\n    void print(){\n        System.out.println();\n        System.out.println(\"--------------\u7a0b\u5e8f\u8fd0\u884c\u7ed3\u675f--------------\");\n    }\n\n    @BeforeEach\n    void init_(){\n        this.runTest_Data = init_All();\n        System.out.println(\"run...\");\n    }\n\n    @AfterEach\n    void space() {\n        System.out.println();\n    }\n\n    private int[] init_All(){\n        final int[] testData;\n        System.out.println(\"before run...\");\n\n        \/\/ \u968f\u673a\u751f\u6210\u6d4b\u8bd5\u6570\u636e\n        int n = (int) (Math.random() * 1000);\n        System.out.println(n);\n        testData = new int[n];\n        for (int i = 0; i &lt; n; i++) {\n            testData[i] = (int) (Math.random() * 1000);\n        }\n        return testData;\n    }\n\n    @Test\n    \/\/\u5192\u6ce1\u6392\u5e8f\n    public void test_mpSort() {\n        int[] mp = MySortAlgorithm_Main.mp_sort(runTest_Data);\n        System.out.println(\"\u5192\u6ce1\u6392\u5e8f\u7ed3\u679c:\");\n    }\n\n    @Test\n        \/\/ \u9009\u62e9\u6392\u5e8f\n    void test_choseSort() {\n        int[] cos = MySortAlgorithm_Main.chose_sort(runTest_Data);\n        System.out.println(\"\u9009\u62e9\u6392\u5e8f\u7ed3\u679c\uff1a\");\n    }\n\n    @Test\n        \/\/ \u63d2\u5165\u6392\u5e8f\n    void test_insertSort() {\n        int[] ins = MySortAlgorithm_Main.insert_sort(runTest_Data);\n        System.out.println(\"\u63d2\u5165\u6392\u5e8f\u7ed3\u679c\");\n    }\n}<\/pre>\n<p><strong>\u89e3\u91ca\uff1a<\/strong><\/p>\n<ul>\n<li>\u4f7f\u7528 @testinstance(testinstance.lifecycle.per_class) \u6ce8\u89e3\uff0c\u4f7f\u6d4b\u8bd5\u7528\u4f8b\u5728\u6574\u4e2a\u6d4b\u8bd5\u7c7b\u4e2d\u5171\u4eab\u4e00\u4e2a\u5b9e\u4f8b\u3002<\/li>\n<li>\u5728 @beforeeach \u4e2d\uff0c\u4f7f\u7528 init_all() \u65b9\u6cd5\u751f\u6210\u6d4b\u8bd5\u6570\u636e\u5e76\u5c06\u5176\u5b58\u50a8\u5728 runtest_data \u3002<\/li>\n<li>\u6bcf\u79cd\u6392\u5e8f\u7b97\u6cd5\u7684\u6d4b\u8bd5\u65b9\u6cd5\u90fd\u4f7f\u7528 runtest_data \u800c\u4e0d\u662f testdata \u4f5c\u4e3a\u8f93\u5165\u6570\u636e\u3002<\/li>\n<li>\u8fd9\u6837\u5c31\u5b9e\u73b0\u4e86\u4e3a\u6240\u6709\u6d4b\u8bd5\u65b9\u6cd5\u5171\u4eab\u76f8\u540c\u6d4b\u8bd5\u6570\u636e\u7684\u76ee\u7684\uff0c\u5e76\u4e14\u53ea\u5728\u6bcf\u4e2a\u6d4b\u8bd5\u65b9\u6cd5\u4e4b\u524d\u521d\u59cb\u5316\u4e00\u6b21\u6d4b\u8bd5\u6570\u636e\u3002<\/li>\n<\/ul>\n<p>\u6587\u4e2d\u5173\u4e8e\u7684\u77e5\u8bc6\u4ecb\u7ecd\uff0c\u5e0c\u671b\u5bf9\u4f60\u7684\u5b66\u4e60\u6709\u6240\u5e2e\u52a9\uff01\u82e5\u662f\u53d7\u76ca\u532a\u6d45\uff0c\u90a3\u5c31\u52a8\u52a8\u9f20\u6807\u6536\u85cf\u8fd9\u7bc7\u300a\u5982\u4f55\u4f7f\u7528 JUnit 5 \u4e3a\u540c\u4e00\u6570\u636e\u96c6\u6d4b\u8bd5\u4e0d\u540c\u65b9\u6cd5\uff0c\u907f\u514d\u91cd\u590d\u8f93\u5165\u6d4b\u8bd5\u7528\u4f8b\uff1f\u300b\u6587\u7ae0\u5427\uff0c\u4e5f\u53ef\u5173\u6ce8\u516c\u4f17\u53f7\u4e86\u89e3\u76f8\u5173\u6280\u672f\u6587\u7ae0\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5982\u4f55\u4f7f\u7528 JUnit 5 \u4e3a\u540c\u4e00&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4925],"tags":[],"class_list":["post-203185","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/203185","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=203185"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/203185\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=203185"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=203185"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=203185"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}