SpringBoot整合junit

主要分为4步

  1. 添加依赖
  2. 创建测试类
  3. 在测试类上添加注解
  4. 在测试类注入测试对象

1:导入依赖包

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>

2:创建测试类

SpringBoot整合junit-LMLPHP

3:在测试类上添加注解并注入测试对象

测试类上注解:

@RunWith(SpringRunner.class)
@SpringBootTest(classes = Springbootdemo1Application.class)
package com.offcn.springbootdemo1;

import com.offcn.springbootdemo1.mapper.UUserMapper;
import com.offcn.springbootdemo1.pojo.UUser;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner; import javax.annotation.Resource;
import java.util.List; //添加整合junit注解
@RunWith(SpringRunner.class)
@SpringBootTest(classes = Springbootdemo1Application.class)
public class AppTest {
   //注入Mapper对象,用来获取List<UUser>集合
@Resource
private UUserMapper userMapper;
@Test
public void TestMethod(){
List<UUser> uUsers = userMapper.selectUUser();
for (UUser uUser : uUsers) {
System.out.println(uUser);
}
}
}
05-11 19:44