Spring Boot整合Junit,@RunWith和@SpringBootTest的使用
1、在pom.xml添加junit启动器
java
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
2、编写测试类
java
/**
* main方法:
* ApplicationContext ac=new
* ClassPathXmlApplicationContext("classpath:applicationContext.xml");
* junit与spring整合:
* @RunWith(SpringJUnit4ClassRunner.class):让junit与spring环境进行整合
* @Contextconfiguartion("classpath:applicationContext.xml")
*/
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes={App.class})//App是项目的启动类名称
public class UserServiceTest {
@Autowired
private UserServiceImpl userServiceImpl;
@Test
public void testAddUser(){
this.userServiceImpl.addUser();
}
}