I am testing the method optDate
.
public static Date optDate(JSONObject json, String key) {
if(!json.isNull(key)) {
try {
DateTime dt = JSONUtils.dateFormatter.withZoneUTC().parseDateTime(json.optString(key));
Date date = dt.toDate();
return date;
} catch(Exception e) {
}
}
return null;
}
When I run the test I get the follow error. Someone told me to "you should check if those two method calls isNull()
and optString()
are called on the object that you provide to the method under test." But I dont know what he means and how to do that.
org.mockito.exceptions.misusing.UnfinishedStubbingException:
Unfinished stubbing detected here:
-> at com.buildmetric.inspect.JSONUtilsTest.optDateShouldReturnValueOnValidString(JSONUtilsTest.java:85)
E.g. thenReturn() may be missing.
Examples of correct stubbing:
when(mock.isOk()).thenReturn(true);
when(mock.isOk()).thenThrow(exception);
doThrow(exception).when(mock).someVoidMethod();
Hints:
1. missing thenReturn()
2. you are trying to stub a final method, you naughty developer!
3: you are stubbing the behaviour of another mock inside before 'thenReturn' instruction if completed
This is my test code.
@Mock
JSONObject JSONMock = Mockito.mock(JSONObject.class);
JSONUtils mJSONUtils = Mockito.mock(JSONUtils.class);
@Test
public void optDateShouldReturnValueOnValidString() {
String dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'";
DateTimeFormatter dateFormatter = DateTimeFormat.forPattern(dateFormat);
DateTime dt = JSONUtilsTest.dateFormatter.withZoneUTC().parseDateTime("2011-11-02T02:50:12.208Z");
Date date = dt.toDate();
doReturn(date).when(mJSONUtils).optDate(JSONMock,"possessionDate");
Date result = JSONUtils.optDate(JSONMock, "possessionDate");
Assert.assertEquals("2011-11-02",result);
// Assert.assertNull(result);
}
Aucun commentaire:
Enregistrer un commentaire