无法使用InjectMocks-Mockito实例化模拟对象

时间:2015-12-23 18:24:08

标签: java unit-testing junit tdd mockito

我是TDD和mockito的新手,我正在尝试将mocks注入类中以执行单元测试,该类根据某些验证在方法中实例化其依赖项,我收到错误

测试类/方法

//Its interface 
public interface UserService {

    public Debt getCustomerDebt(String id);
}

//validator method

public static boolean isValidId(String id){
    if(id != null && !id.isEmpty() && !id.trim().equals("")){
        return true;
    }
    return false;
}

public class UserServiceImpl implements UserService {

    private Repository repo;
    private WSDLCustomerDebt wsdlCostumerDebt;

    public static final int USER_EXIST = 1;
    public static final int USER_DOESNOT_EXIST = 0;

    public UserServiceImpl(){

    }

    public Debt getCustomerDebt(String id) {

        if(ValidatorHelper.isValidId(id)){
            repo = new RepositoryImpl();
            int exist = repo.getCustomer(id);
            if(exist==USER_EXIST){
                wsdlCostumerDebt = new WSDLCustomerDebtImpl();
                List<Date> meses = wsdlCostumerDebt.obtenerMeses(id);
                if(meses.size()>0){
                    int totalDebt = 0;
                    for (Date mes : meses){
                        totalDebt += wsdlCostumerDebt.obtenerDeuda(mes, id);
                    }

                    return new Debt(id, BigDecimal.valueOf(totalDebt));

                }else{
                    return new Debt(id, BigDecimal.valueOf(0));
                }
            }
        }
        return null;
    }

}

模拟类repositoryimpl

public class RepositoryImpl implements Repository {

    public int getCustomer(String id) {
        int y = Integer.valueOf(1);
        return y;
    }

}

wsdl模拟了类

//Interface
public interface WSDLCustomerDebt {

    public List<Date> obtenerMeses(String customerId);

    public Integer obtenerDeuda(Date month, String customerId);

}

public class WSDLCustomerDebtImpl implements WSDLCustomerDebt {

    public List<Date> obtenerMeses(String customerId) {
        return null;
    }

    public Integer obtenerDeuda(Date month, String customerId) {
        Integer y = Integer.valueOf(11);
        return y;
    }
}

域名类债务

public class Debt {

    private String id;
    private BigDecimal debt;

    public Debt(String id, BigDecimal debt) {
        super();
        this.id = id;
        this.debt = debt;
    }

    //Getters and setters ....
}

最后测试课程

import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;

public class UserServiceImplTest {
    @Mock
    private Repository repo;

    @Mock
    private WSDLCustomerDebt wsdlCustomerDebt;

    @InjectMocks
    private UserServiceImpl userService; 

    @Before
    public void init(){
        //repo=Mockito.mock(Repository.class);      
        //when(wsdlcustomer.obtenerDeuda(D, customerId))
        MockitoAnnotations.initMocks(this);
    }

    @Test
    public void noExistingCustomer(){
        //Given:
        String id = "123";
        //When:
        Mockito.when(repo.getCustomer(id)).thenReturn(0);
        Debt debt = userService.getCustomerDebt(id);
        Mockito.verify(repo.getCustomer(Mockito.any(String.class))); 
        //Then:
        assertNull(debt);   
    }
}

这是我得到的错误,我试图避免使用构造函数或任何getter / setter并通过参数接收模拟,也许它可能是由于一个虚拟错误引起的,但此时我并没有知道我做错了什么,事实上我认为问题的出现是因为模拟类中的return语句。我正在使用mockito版本1.9.5 btw

org.mockito.exceptions.misusing.NotAMockException: 
Argument passed to verify() is of type Integer and is not a mock!
Make sure you place the parenthesis correctly!
See the examples of correct verifications:
    verify(mock).someMethod();
    verify(mock, times(10)).someMethod();
    verify(mock, atLeastOnce()).someMethod();
    at com.i2btech.poctest.UserServiceImplTest.noExistingCustomer(UserServiceImplTest.java:51)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)

1 个答案:

答案 0 :(得分:2)

首先,您的服务不使用您正在注入的模拟,因为它在您调用方法时会创建一个新模拟。 repo应该是服务构造函数的参数。

其次,验证调用mock方法的正确语法不是

verify(mock.method())

verify(mock).method()

(如错误信息所示)。

所以,行

Mockito.verify(repo.getCustomer(Mockito.any(String.class)))

必须替换为

Mockito.verify(repo).getCustomer(Mockito.any(String.class))