Mockito是一个流行的单元测试框架,我理解主要用来构造复杂的对象结合junit进行单元测试,尤其在全流程测试的情况下,需要很多外部的依赖,依赖其他服务返回的数据,环境又一下子搭建不起来,此时来使用mockito进行单元测试太合适不过了。
如果要对http REST服务进行整体测试,使用wiremock框架会更加的方便,更加的接近实际情况,这里
重点写一个关于mockito的例子方便大家理解。
package com.example.demo.dao;
import com.example.demo.domain.User;
import org.springframework.stereotype.Repository;
@Repository
public class UserDao {
public User getUserByName(String name) {
return new User(name,"18");
}
}
package com.example.demo.service;
import com.example.demo.dao.UserDao;
import com.example.demo.domain.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class UserService {
@Autowired
private UserDao userDao;
public User getUser(String name){
return userDao.getUser