{"id":318,"date":"2022-04-09T09:07:11","date_gmt":"2022-04-09T01:07:11","guid":{"rendered":"https:\/\/blog.lijiajia.top\/?p=318"},"modified":"2026-06-19T21:37:59","modified_gmt":"2026-06-19T13:37:59","slug":"spring5%e6%a1%86%e6%9e%b6%e5%ad%a6%e4%b9%a0%ef%bc%9a%e6%96%b0%e5%8a%9f%e8%83%bd-%e6%95%b4%e5%90%88junit%e4%ba%8c%e5%8d%81%e5%85%ad","status":"publish","type":"post","link":"https:\/\/blog.ttwow.top\/?p=318","title":{"rendered":"Spring5\u6846\u67b6\u5b66\u4e60\uff1a\u65b0\u529f\u80fd-\u6574\u5408Junit(\u4e8c\u5341\u516d)"},"content":{"rendered":"<h2>\u6574\u5408Junit4<\/h2>\n<h3>\u7b2c\u4e00\u6b65\uff1a\u5f15\u5165Spring\u76f8\u5173\u9488\u5bf9\u6d4b\u8bd5\u4f9d\u8d56<\/h3>\n<pre><code class=\"language-markdown\">spring-test-5.2.6.RELEASE.jar\nhamcrest-core-1.3.jar\njunit-4.13.1.jar<\/code><\/pre>\n<h3>\u7b2c\u4e8c\u6b65\uff1a\u521b\u5efa\u6d4b\u8bd5\u7c7b\uff0c\u4f7f\u7528\u6ce8\u89e3\u65b9\u5f0f\u5b8c\u6210\uff1a<\/h3>\n<p>@Test \u4f7f\u7528 import org.junit.Test;<\/p>\n<pre><code class=\"language-java\">import com.spring5.service.TransferService;\nimport org.junit.Test;\nimport org.junit.runner.RunWith;\nimport org.springframework.beans.factory.annotation.Autowired;\nimport org.springframework.test.context.ContextConfiguration;\nimport org.springframework.test.context.junit4.SpringJUnit4ClassRunner;\n\n@RunWith(SpringJUnit4ClassRunner.class) \/\/\u5355\u5143\u6d4b\u8bd5\u6846\u67b6 SpringJUnit4ClassRunner @ContextConfiguration\u5728spring-test\u5305\n@ContextConfiguration(&quot;classpath:bean1.xml&quot;)\/\/\u52a0\u8f7d\u914d\u7f6e\u6587\u4ef6\npublic class Junit4Test {\n\n    @Autowired\n    private TransferService transferService;\n\n    @Test\n    public void test1(){\n        transferService.transfer();\n    }\n}<\/code><\/pre>\n<h2>\u6574\u5408Junit5<\/h2>\n<h3>\u7b2c\u4e00\u6b65\uff1a\u5f15\u5165Junit5\u7684jar\u5305<\/h3>\n<p><div class='fancybox-wrapper lazyload-container-unload' data-fancybox='post-images' href='https:\/\/bucket-blog-img.oss-cn-shanghai.aliyuncs.com\/wp-content\/uploads\/2022\/04\/image-1649467541727.png'><img class=\"lazyload lazyload-style-11\" src=\"data:image\/svg+xml;base64,PCEtLUFyZ29uTG9hZGluZy0tPgo8c3ZnIHdpZHRoPSIxIiBoZWlnaHQ9IjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgc3Ryb2tlPSIjZmZmZmZmMDAiPjxnPjwvZz4KPC9zdmc+\"  decoding=\"async\" data-original=\"https:\/\/bucket-blog-img.oss-cn-shanghai.aliyuncs.com\/wp-content\/uploads\/2022\/04\/image-1649467541727.png\" src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB\/AAffA0nNPuCLAAAAAElFTkSuQmCC\" alt=\"file\" \/><\/div><\/p>\n<h3>\u7b2c\u4e8c\u6b65\uff1a\u521b\u5efa\u6d4b\u8bd5\u7c7b\uff0c\u4f7f\u7528\u6ce8\u89e3\u65b9\u5f0f\u5b8c\u6210<\/h3>\n<p>@Test \u4f7f\u7528 org.junit.jupiter.api.Test;<\/p>\n<pre><code class=\"language-java\">\nimport com.spring5.service.TransferService;\nimport org.junit.jupiter.api.Test;\nimport org.junit.jupiter.api.extension.ExtendWith;\nimport org.springframework.beans.factory.annotation.Autowired;\nimport org.springframework.test.context.ContextConfiguration;\nimport org.springframework.test.context.junit.jupiter.SpringExtension;\n\n@ExtendWith(SpringExtension.class)\n@ContextConfiguration(&quot;classpath:bean1.xml&quot;)\npublic class Junit5Test {\n\n    @Autowired\n    private TransferService transferService;\n\n    @Test\n    public void test1(){\n        transferService.transfer();\n    }\n}<\/code><\/pre>\n<h3>\u4f7f\u7528\u590d\u5408\u6ce8\u89e3\uff0c\u4ee3\u66ff\u4e0a\u9762\u4e24\u4e2a\u6ce8\u89e3<\/h3>\n<pre><code class=\"language-java\">import com.spring5.service.TransferService;\nimport org.junit.jupiter.api.Test;\nimport org.springframework.beans.factory.annotation.Autowired;\nimport org.springframework.test.context.junit.jupiter.SpringJUnitConfig;\n\n\/\/@ExtendWith(SpringExtension.class)\n\/\/@ContextConfiguration(&quot;classpath:bean1.xml&quot;)\n@SpringJUnitConfig(locations = &quot;classpath:bean1.xml&quot;)\npublic class Junit5Test {\n\n    @Autowired\n    private TransferService transferService;\n\n    @Test\n    public void test1(){\n        transferService.transfer();\n    }\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u6574\u5408Junit4 \u7b2c\u4e00\u6b65\uff1a\u5f15\u5165Spring\u76f8\u5173\u9488\u5bf9\u6d4b\u8bd5\u4f9d\u8d56 spring-test-5.2.6.RELEASE [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[125],"tags":[47,68],"class_list":["post-318","post","type-post","status-publish","format-standard","hentry","category-java-backend","tag-junit","tag-spring"],"_links":{"self":[{"href":"https:\/\/blog.ttwow.top\/index.php?rest_route=\/wp\/v2\/posts\/318","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.ttwow.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.ttwow.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.ttwow.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.ttwow.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=318"}],"version-history":[{"count":1,"href":"https:\/\/blog.ttwow.top\/index.php?rest_route=\/wp\/v2\/posts\/318\/revisions"}],"predecessor-version":[{"id":2137,"href":"https:\/\/blog.ttwow.top\/index.php?rest_route=\/wp\/v2\/posts\/318\/revisions\/2137"}],"wp:attachment":[{"href":"https:\/\/blog.ttwow.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=318"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.ttwow.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=318"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.ttwow.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=318"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}