整合jUnit4
1.引入依赖
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>5.2.6.RELEASE</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.13.2</version> </dependency>
2.创建测试类,使用注解方式完成
package spring; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import spring.service.AccountService; @RunWith(SpringJUnit4ClassRunner.class)//单元测试框架 @ContextConfiguration("classpath:spring.xml")//加载配置文件 public class TestJunit4 { @Autowired AccountService accountService; @Test public void test(){ accountService.transfer(); } }
整合jUnit5
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>5.2.6.RELEASE</version> </dependency> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-engine</artifactId> <version>5.5.2</version> </dependency>
package spring; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit.jupiter.SpringExtension; import spring.service.AccountService; @ExtendWith(SpringExtension.class) @ContextConfiguration("classpath:spring.xml") public class TestJunit5 { @Autowired AccountService accountService; @Test public void test(){ accountService.transfer(); } }
标签:spring,springframework,整合,import,test,org,jUnit4,jUnit5,junit From: https://www.cnblogs.com/ixtao/p/17149865.html