目标:在spring Boot项目中使用Junit进行单元测试UserService的方法
分析:
1.添加启动器依赖spring-boot-starter-test
2.编写测试类测试
=====
1.添加启动器依赖spring-boot-starter-test
<!--添加测试依赖启动器--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> </dependency>
2.编写测试类测试
package com.cc8w.home.service; import com.cc8w.com.cc8w.entity.User; import com.cc8w.home.mapper.UserMapper; 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.SpringRunner; @RunWith(SpringRunner.class) @SpringBootTest public class UserServiceTest { @Autowired private UserService userService; @Test public void queryById(){ userService.queryById(1); } @Test public void saveUser(){ userService.saveUser(new User()); } }