1
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
Sangeet
  • 145
  • 3
  • 9
  • Did you try to use ArgumentCaptor? https://static.javadoc.io/org.mockito/mockito-core/2.6.9/org/mockito/ArgumentCaptor.html https://stackoverflow.com/questions/12295891/how-to-use-argumentcaptor-for-stubbing – Octavian R. Mar 17 '19 at 12:29
  • How can I use Argument Captor in this situation? setHardCodeValuesToStudentObject() is setting some values and inside this method, one more method is being called which is setting values to a list. So I want one value from that list. – Sangeet Mar 17 '19 at 12:34
  • 1
    If you want help, then post all the code that you want to test it because, from your test class, is hard to figure out what exactly you want to test. – Octavian R. Mar 17 '19 at 19:30

0 Answers0