java - mock methods in same class -


i using mockito mock method in same class writing test. have seen other answers on (mocking method in same class), misunderstanding them, since running issues.

 class temp() {      public boolean methoda(string param) {           try {               if(methodb(param))                    return true;               return false;          } catch (exception e) {                e.printstacktrace();          }     }  } 

my test method:

 @test  public void testmethoda() {      temp temp = new temp();     temp spytemp = mockito.spy(temp);      mockito.doreturn(true).when(spytemp).methodb(mockito.any());      boolean status = temp.methoda("xyz");      assert.assertequals(true, status);  } 

i expection printed out because definition of methodb gets executed. understanding definition of methodb mocked using spytemp. not appear case.

can please explain going wrong?

first issue have use spytest object expect mockito. here not same test. spytemp wrapped mockito object temp.

another issue stub methodb(), trying run methoda(). yes in implementation of methoda() call methodb(), call @ this.methodb(), not spytemp.methodb(). here have understand mocking work when call on instance of temp. it's wrapped mockito proxy catch call , if have overriden method call new implementation instead of original. since original method called, inside know nothing mockito proxy. "overriden" method called when run spytemp.methodb()

this should work:

mockito.doreturn(true).when(spytemp).methodb(mockito.any());  boolean status = spytemp.methodb("xyz"); 

Comments

Popular posts from this blog

amazon web services - S3 Pre-signed POST validate file type? -

c# - Check Keyboard Input Winforms -