Springboot--Junit
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
package com.bjsxt.dao; import org.springframework.stereotype.Repository; @Repository public class UserDaoImpl { public void saveUser(){ System.out.println("insert into users....."); } }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
package com.bjsxt.service; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import com.bjsxt.dao.UserDaoImpl; @Service public class UserServiceImpl { @Autowired private UserDaoImpl userDaoImpl; public void addUser(){ this.userDaoImpl.saveUser(); } }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
package com.bjsxt; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class App { public static void main(String[] args) { SpringApplication.run(App.class, args); } }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
package com.bjsxt.test; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import com.bjsxt.App; import com.bjsxt.service.UserServiceImpl; /** * SpringBoot测试类 *@RunWith:启动器 *SpringJUnit4ClassRunner.class:让junit与spring环境进行整合 * *@SpringBootTest(classes={App.class}) 1,当前类为springBoot的测试类 *@SpringBootTest(classes={App.class}) 2,加载SpringBoot启动类。启动springBoot * *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(); } }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.10.RELEASE</version> </parent> <groupId>com.bjsxt</groupId> <artifactId>19-spring-boot-test</artifactId> <version>0.0.1-SNAPSHOT</version> <properties> <java.version>1.7</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- 添加junit环境的jar包 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> </dependency> </dependencies> </project>