springboot使用mockito做单元测试

发布时间:2024-04-28 09:01

1.在pom.xml中添加mockito的依赖

         
			org.mockito
			mockito-all
			2.0.2-beta
		

2.测试类

public class Test1 {

	@InjectMocks
	UsersServiceImpl usersServiceImpl;

	@Mock
	UserMapper mapper;

	@Before
	public void setUp() {
		MockitoAnnotations.initMocks(this);
	}

	@Test
	public void testGet() {
		List list = new ArrayList();
		User user = new User();
		Date time = new Date();
		Timestamp timestamp = new Timestamp(time.getTime());
		user.setId("500151");
		user.setAge(18);
		user.setName("aaa");
		user.setBirthday(timestamp);
		user.setStats(1);
		user.setOther(new BigDecimal(1));
		list.add(user);
		when(mapper.selectUsers()).thenReturn(list);
		List relist = usersServiceImpl.selectUser();
		System.out.println(relist);

	}

}

3.service和service的实现类

@Service
@Transactional
public class UsersServiceImpl implements UsersService {
	@Autowired
	private UserMapper usermapper;
	@Override
	public List selectUser() {
		return this.usermapper.selectUsers();
	}
public interface UsersService {
	List selectUser();
}

4.dao层

@Mapper
public interface UserMapper {
	List selectUsers();
}

5.最后的效果
在这里插入图片描述
在这里插入图片描述
先贴代码,后续更新解释

ItVuer - 免责声明 - 关于我们 - 联系我们

本网站信息来源于互联网,如有侵权请联系:561261067@qq.com

桂ICP备16001015号