mercredi 23 décembre 2015

Cannot instantiate mock objects using InjectMocks-Mockito

Hey i'm new to TDD and mockito aswell, I'm trying to inject mocks into a class to perform a unit test, the class instantiate its dependences inside a method depending on some validations, i got an error

test class/method

//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;
}

}

mocked class repositoryimpl

public class RepositoryImpl implements Repository {



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

}

wsdl mocked class

//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) {
    // TODO Auto-generated method stub
    return null;
}

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

domain class debt

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 ....
    }

finally test class

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);   
        }
}

this is the error im getting, maybe it could be caused by a dummy error but at this point i do not know what im doing wrong, in fact i think the problem occurs because of the return statement in mocked classes.

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)

Aucun commentaire:

Enregistrer un commentaire