Spring Boot整合Junit
Junit启动器
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId></dependency>
编写业务代码
dao
@Repositorypublic class UserDaoImpl {public void saveUser(){System.out.println("insert into users.....");}}
service
@Servicepublic class UserServiceImpl {@Autowiredprivate UserDaoImpl userDaoImpl;public void addUser(){this.userDaoImpl.saveUser();}}
app
@SpringBootApplicationpublic class App {public static void main(String[] args) {SpringApplication.run(App.class, args);}}
整合Junit
@RunWith(SpringJUnit4ClassRunner.class) @SpringBootTest(classes={App.class})public class UserServiceTest {@Autowiredprivate UserServiceImpl userServiceImpl;@Testpublic void testAddUser(){this.userServiceImpl.addUser();}}