Reputation: 1
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 dependencies 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) {
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 I'm getting and I'm trying to avoid using a constructor or any getter/setter and to receive the mocks via parameters, maybe it could be caused by a dummy error but at this point I do not know what I'm doing wrong, in fact i think the problem occurs because of the return statement in mocked classes. I'm using mockito version 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)
Upvotes: 0
Views: 2276
Reputation: 692081
First of all, your service doesn't use the mock you're injecting, since it creates a new one when you call the method. The repo should be an argument of the service constructor.
Second, the proper syntax to verify that a method of a mock has been called is not
verify(mock.method())
but
verify(mock).method()
(as the error message clearly says).
So, the line
Mockito.verify(repo.getCustomer(Mockito.any(String.class)))
must be replaced by
Mockito.verify(repo).getCustomer(Mockito.any(String.class))
Upvotes: 2