SpringBoot+TestNG+service层单元测试-参数化DataProvider
首先把被测试的service放在这里,每个方法是干什么的其实通过方法名是可以猜出来的
package com.newcrud.service.impl;
import com.newcrud.entity.Blog;
import com.newcrud.mapper.BlogMapper;
import com.newcrud.service.BlogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class BlogServiceImpl implements BlogService {
@Autowired
BlogMapper blogMapper;
@Autowired
@Override
public List<Blog> getBlogs() {
return blogMapper.getBlogs();
}
@Override
public List<Blog> getBlogByUserId(Integer userid) {
return blogMapper.getBlogByUserId(userid);
}
@Override
public boolean insertBlog(Blog blog) {
return blogMapper.insertBlog(blog);
}
@Override
public boolean updateBlog(Blog blog) {
return blogMapper.updateBlog(blog);
}
@Override
public boolean deleteBlogById(Integer id) {
return blogMapper.deleteBlogById(id);
}
}
简单使用:一对一+多次执行
package com.newcrud.service.impl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.testng.AbstractTestNGSpringContextTests;
import org.testng.Assert;
import org.testng.annotations.*;
import static org.testng.Assert.*;
@SpringBootTest
public class BlogServiceImplTest extends AbstractTestNGSpringContextTests {
@Autowired
BlogServiceImpl blogService;
@DataProvider(name = "zhangsan")
public Object[][] getData(){
return new Object[][]{
{1},{2},{3}
};
}
@Test(dataProvider = "zhangsan")
public void testGetBlogByUserId(int id) {
System.out.println(id);
System.out.println(blogService.getBlogByUserId(id));
assertEquals(1,id);
}
}
结果
根据方法名提供不同的参数
package com.newcrud.service.impl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.testng.AbstractTestNGSpringContextTests;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import java.lang.reflect.Method;
import static org.testng.Assert.*;
@SpringBootTest
public class BlogServiceImplTest extends AbstractTestNGSpringContextTests {
@Autowired
BlogServiceImpl blogService;
@DataProvider(name = "zhangsan")
public Object[][] getData(Method method){
Object[][] data = null;
String name = method.getName();
if (name.equals("testGetBlogByUserId")){
data = new Object[][]{
{1},{2},{3}
};
}else if (name.equals("testGetBlogByUserId2")){
data = new Object[][]{
{77},{88},{99}
};
}
return data;
}
@Test(dataProvider = "zhangsan")
public void testGetBlogByUserId(int id) {
System.out.println(id);
System.out.println(blogService.getBlogByUserId(id));
}
@Test(dataProvider = "zhangsan")
public void testGetBlogByUserId2(int id) {
System.out.println(id);
System.out.println(blogService.getBlogByUserId(id));
}
}
结果
提供者和消费者不在同一个类
数据提供者
package com.newcrud.dataProvider;
import org.testng.annotations.DataProvider;
public class MyDataProvider {
@DataProvider(name = "lisi")
public static Object[][] getData(){
Object[][] data = new Object[][]{
{33},{44},{55}
};
return data;
}
}
数据消费者
package com.newcrud.service.impl;
import com.newcrud.dataProvider.MyDataProvider;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.testng.AbstractTestNGSpringContextTests;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import java.lang.reflect.Method;
import static org.testng.Assert.*;
@SpringBootTest
public class BlogServiceImplTest extends AbstractTestNGSpringContextTests {
@Autowired
BlogServiceImpl blogService;
@DataProvider(name = "zhangsan")
public Object[][] getData(Method method){
Object[][] data = null;
String name = method.getName();
if (name.equals("testGetBlogByUserId")){
data = new Object[][]{
{1},{2},{3}
};
}else if (name.equals("testGetBlogByUserId2")){
data = new Object[][]{
{77},{88},{99}
};
}
return data;
}
@Test(dataProvider = "zhangsan")
public void testGetBlogByUserId(int id) {
System.out.println(id);
System.out.println(blogService.getBlogByUserId(id));
}
@Test(dataProvider = "lisi",dataProviderClass = MyDataProvider.class)
public void testGetBlogByUserId2(int id) {
System.out.println(id);
System.out.println(blogService.getBlogByUserId(id));
}
}
结果 做完上面的,是不是想象空间更大了,因为你可以单独写一个专门的数据提供类,再根据method去针对性的提供数据,这样也做到了数据和逻辑的分离。
还原数据
比如说我们测试新增或者删除功能的时候,我们测试完成后还想还原数据怎么办
这是我原来的数据
package com.newcrud.service.impl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.testng.AbstractTestNGSpringContextTests;
import org.springframework.test.context.testng.AbstractTransactionalTestNGSpringContextTests;
import org.springframework.transaction.annotation.Transactional;
import org.testng.annotations.Test;
import static org.testng.Assert.*;
@SpringBootTest
public class BlogServiceImplTest extends AbstractTransactionalTestNGSpringContextTests {
@Autowired
BlogServiceImpl blogService;
@Test
public void testDeleteBlogById() {
blogService.deleteBlogById(9);
}
@Test
public void testDeleteBlogById2() {
blogService.deleteBlogById(10);
}
}
结果:测试用例执行成功了,数据还没被删除,而且日志里有这么一段
o.s.t.c.transaction.TransactionContext : Rolled back transaction for test: [DefaultTestContext@22e357dc testClass = BlogServiceImplTest, testInstance = com.newcrud.service.impl.BlogServiceImplTest@40cb8df7, testMethod = testDeleteBlogById2@BlogServiceImplTest。。。。。。
说明操作完成后被回滚了
|