Junit回顾
public JUnitTest{
@Before//方法先于@Test标注的方法执行,一般用于资源申请
public void init(){
}
@Test
public void TestMethod(){
}
@After//方法后于@Test标注的方法执行,一般用于资源释放
public void destroy(){
}
}
Spring集成Junit开发步骤
1.导入spring集成Junit坐标
<!-- https://mvnrepository.com/artifact/org.springframework/spring-test -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.3.15</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/junit/junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<scope>test</scope>
</dependency>
2、使用@RunWith使用;
使用@ContextConfiguration指定配置类或配置文件;
使用@Autowired注入需要测试的对象;
并编写测试方法。
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(SpringConfig.class)
public SpringJUnitTest{
@Autowired
private User user;
@Test
public void TestMethod(){
}
}