Issue
I have this method to validate Unit. I send field values and inside it method I create a new model and then return that model via interface.
public void validate(String unitNumber, Integer unitTypeId, String notes) {
if(!TextUtils.isEmpty(unitNumber)) {
Unit unit = new Unit();
unit.setUnitNumber(unitNumber);
unit.setFlatTypeId(unitTypeId);
unit.setNotes(notes);
view.displayUnitValid(unit);
} else {
view.displayUnitNotValid();
}
}
Now I want do do unit testing of this method with the following code.
@Test public void shouldValidateSinceUnitNumberIsValid() {
// Given
String unitNumber = "1";
// When
presenter.validate(unitNumber, null, null);
// Then
Mockito.verify(view).displayUnitValid(new Unit());
}
I am getting the following error message:
Argument(s) are different! Wanted:
view.displayUnitValid(
com.rwar.domain.customers.Unit@57cf54e1
);
-> at com.rwar.presentation.work_orders.AddUnitPresenterTest.shouldValidateSinceUnitNumberIsValid(AddUnitPresenterTest.java:73)
Obvisouly there is a problem since I am creating a new Unit instance here:
Mockito.verify(view).displayUnitValid(new Unit());
And inside validate() method I create another instance of Unit.
How I can fix this?
Solution
Here is the working solution that might be useful to someone else:
@Test public void shouldValidateSinceUnitNumberIsValid() {
// Given
String unitNumber = "1";
// When
presenter.validate(unitNumber, null, null);
// Then use ArgumentCaptor to get unit value from newly created object inside validate() method
ArgumentCaptor<Unit> argument = ArgumentCaptor.forClass(Unit.class);
Mockito.verify(view).displayUnitValid(argument.capture());
// Compare captured argument of Unit number with local unitNumber
assertEquals(argument.getValue().getUnitNumber(), unitNumber);
}
Answered By - Zookey
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.