springboot单元测试和集成测试

初学springboot单元测试和继承测试

项目位置

单元测试

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
@Slf4j
@SpringBootTest
/*@Rollback(value = false)
@Transactional
@EnableTransactionManagement*/
public class UserControlTests {

private IUserService userService;
private UserDo userDo;


@BeforeClass //只执行一次
public void setUp() {
log.info("beforClass");
}


@BeforeMethod //每次测试方法执行前运行
public void before() throws Exception{
log.info("beforeMethod");
userDo = new UserDo();
userDo.setUserName("li");
userDo.setPassword("123456");
userService = PowerMockito.mock(UserServiceImpl.class);//不会真正调用方法创建的对象的方法
//模拟restful 每次方法前执行
RestAssuredMockMvc.standaloneSetup(new UserControl(userService));
}

@After
public void after() throws Exception {
log.info("测试结束");
}

@Test(dataProvider = "insertData")
public void insertUser(Integer result, String userName, String password) throws Exception {
log.info("进入测试方法注册用户");
UserDo userDo = new UserDo();
userDo.setPassword(password);
userDo.setUserName(userName);
//模拟返回数据
PowerMockito.when(userService.addUser(userDo)).thenReturn(1); //
//进行测试
RestAssuredMockMvc.given()
.contentType(ContentType.JSON)
.body(userDo)
.log()
.all()
.when()
.post("/signUp")
.then()
.statusCode(200)
.contentType(ContentType.JSON)
.body("code",org.hamcrest.Matchers.equalTo(1000));

}

//构造测试数据
@DataProvider(name = "insertData")
public Object[][] insertData(){
return new Object[][] {
{1,"li","li"},
{0,"chun","chun"}
};
}
}

继承测试

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59

@SpringBootTest
@Slf4j
public class UserControlIntegrations extends AbstractTestNGSpringContextTests {//必须继承此类

@Autowired //注入上下文真实环境
private WebApplicationContext context;

@BeforeClass
public void setUp(){
//模拟http请求和web环境
RestAssuredMockMvc.webAppContextSetup(context);
}

@Test(dataProvider = "insertData")
public void testSignUp(String expected ,String userName, String password){

UserDo userDo = new UserDo();
userDo.setPassword(password);
userDo.setUserName(userName);
RestAssuredMockMvc.given()
.contentType(ContentType.JSON)
.body(userDo)
.log()
.all()
.when()
.post("/signUp")
.then()
.statusCode(200)
.contentType(ContentType.JSON);
}

@Test(dataProvider = "insertData", dependsOnMethods = "testSignUp")
public void testLogin(String expected ,String userName, String password){

UserDo userDo = new UserDo();
userDo.setPassword(password);
userDo.setUserName(userName);
RestAssuredMockMvc.given()
.contentType(ContentType.JSON)
.body(userDo)
.log()
.all()
.when()
.post("/login")
.then()
.statusCode(200)
.contentType(ContentType.JSON);
}

@DataProvider(name = "insertData")
public Object[][] insertData(){
return new Object[][] {
{"成功","li","li"},
{"失败","chun","chun"}
};
}

}

总结

单元测试

1只是仅仅模拟数据,不会调用数据库,mock创建的对象都不会真正执行
2用PowerMockito 创建对象和模拟返回的数据

集成测试

1真实模拟环境
2会真正执行所有操作,包括数据库操作
3dependsOnMethods 的作用

集成测试和单元测试区别

单元测试如何实现数据库操作