java - Mockito method returns null -


i looked in forum couldn't find why happen

i have

public class aimpl implements a{     @autowired    b bimpl;     protected void dosomething(){       string s = "test";       string d = b.permutestring(s);       system.out.println(s.substring(1));    }  }  public class bimpl implements b{     public string permuatestring(s){      return s;    } } 

in test have:

@injectmocks aimpl aimpl;  @mock bimpl bimpl;  @test public void testcode(){ testcode(){    aimpl.dosomething(); } } 

the method permuatestring bimpl returns null. need know result of permutestring() in order continue execution of dosomething. can't null

why it?

i using mockito

by annotating bimpl field @mock, you're saying instance should mock. however, you're not telling mockito result mock should give when invoked arguments, mockito uses default behaviour of returning null.

the fact bimpl real class real behaviour irrelevant. field not contain instance of class; contains instance of mock.

in test method, before call aimpl.dosomething(), tell mockito behaviour expect use:

when(bimpl.permutestring("test")).thenreturn("someothervalue"); 

Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

javascript - jQuery .height() return 0 when visible but non-0 when hidden -