public void testFunction() throws Exception {
ABC mock1= mock(ABC.class);
when(mock1.getValue()).thenReturn("string1");
whenNew(ABC.class).withNoArguments().thenReturn(mock1);
Student student= setHardCodeValuesToStudentObject();
Method method = Person.class.getDeclaredMethod("conversion",
Student.class);
method.setAccessible(true);
String actualConvertedValue = (String)method.invoke(new Person(),
student);
ClassLoader classLoader = this.getClass().getClassLoader();
InputStream inputStream =
classLoader.getResourceAsStream("expectedConvertedValue.json");
String expectedConvertedValue= "";
try {
expectedConvertedValue= IOUtils.toString(inputStream,
StandardCharsets.UTF-8);
}catch (Exception e) {
System.out.print(e.printStackTrace());
}
assertEquals(expectedConvertedValue,actualConvertedValue );
}
This is my test case for a method in which one value is getting converted into other format. So I am checking the expectedConvertedValue and actualConvertedValue.
setHardCodeValuesToStudentObject() is setting some hardCoded values to student object such as (name="jack", age="12" and so on.. ) and inside this method one mock is there and stubbing is happened on that mock. I need to mock a method because inside setHardCodeValuesToStudentObject(), the hardcoded value of getValue() is auto generated. It gets incremented when some other line of code is added before that line. So I have to mock that value.
When I am trying to mock a method inside this (setHardCodeValuesToStudentObject()) method, then its throwing this----->
org.mockito.exceptions.misusing.UnfinishedStubbingException:
Unfinished stubbing detected here:
-> at
com.print.view.ModelTest.setUpTransactionAnnotation(ModelTest.java:653)
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