簡體   English   中英

無法使用InjectMocks-Mockito實例化模擬對象

[英]Cannot instantiate mock objects using InjectMocks-Mockito

我對TDD和Mockito還是陌生的,我試圖將模擬注入到一個類中以執行單元測試,該類根據一些驗證在一個方法中實例化其依賴關系,但出現錯誤

測試類別/方法

//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)

首先,您的服務不使用您要注入的模擬,因為它在您調用該方法時會創建一個新模擬。 回購應該是服務構造函數的參數。

其次,用於驗證是否已調用模擬方法的正確語法不是

verify(mock.method())

verify(mock).method()

(錯誤消息明確指出)。

所以,線

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

必須替換為

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM