Spring Boot整合Junit
Junit启动器
xml
复制代码
<!--junit启动器 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
编写业务代码
dao
java
复制代码
@Repository
public class UserDaoImpl {
public void saveUser(){
System.out.println("insert into users.....");
}
}
service
java
复制代码
@Service
public class UserServiceImpl {
@Autowired
private UserDaoImpl userDaoImpl;
public void addUser(){
this.userDaoImpl.saveUser();
}
}
app
java
复制代码
@SpringBootApplication
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
}
整合Junit
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})
public class UserServiceTest {
@Autowired
private UserServiceImpl userServiceImpl;
@Test
public void testAddUser(){
this.userServiceImpl.addUser();
}
}