1、配置环境
(1)添加测试所需 pom 依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
(2)进入测试类
在所需测试的类中 ctrl + shift + t
进入测试类
(3)添加注解
主要是三个注解:@RunWith(SpringRunner.class)、@SpringBootTest()、@Test
@RunWith(SpringRunner.class)
@SpringBootTest(classes = MapperApplication.class)
public class BrandMapperTest {
@Autowired
private BrandMapper brandMapper; //测试的接口:注入spring容器
@Test
public void testFindById() { //测试方法
...
}
}
2、开始使用
通过 @Autowired 注解,拿到了对象,便可以调用对象的方法
@Test
public void testFindById() {
Brand brand = brandMapper.selectByPrimaryKey(1);
System.out.println(brand);
}
点击开始执行: