springboots使用的版本是2.0.1,注意不同版本可能有差异,并不一定通用
添加Junit的起步依赖:如果是使用idea快速创建SpringBoot项目,那么pom文件中已经有了测试的起步依赖
<!--测试的起步依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
编写测试类:
@RunWith(SpringRunner.class)
@SpringBootTest(classes = MySpringBootApplication.class)
public class MapperTest {
@Autowired
private UserMapper userMapper;
@Test
public void test() {
List<User> users = userMapper.queryUserList();
System.out.println(users);
}
}
SpringRunner继承自SpringJUnit4ClassRunner,使用哪一个Spring提供的测试引擎都可以
public final class SpringRunner extends SpringJUnit4ClassRunner
@SpringBootTest的属性指定的是引导类的字节码对象
来源:https://www.cnblogs.com/roadlandscape/p/12370731.html