java - Unit testing with Mockito resets a mocked class' instance variable to null -
java - Unit testing with Mockito resets a mocked class' instance variable to null -
i have mocked message class follows (within testng unit test class):
@beforemethod protected void setup() { message = mock(message.class); }
i can assure have declared message
of type message
in unit test class. now, message
class has instance variable called category
of type string follows:
public class message { private string category; public string getcategory() { homecoming category; } public void setcategory(string category) { this.category = category; } }
okay, problem: within little test method, have set category
of mocked message
instance , utilize category
task in unit test. however, after set category
mocked
message, reverts null
again. code:
public void shouldsendmessageswithcategory() { message.setcategory("f1 racing"); // set category raceresults.addsubscriber("f1 racing", clienta); system.out.println("cat" + message.getcategory()); // message's category null raceresults.send(message); verify(clienta).receive(message); }
the console prints out null
- know it's null
. i've been trying figure out 2 days, seems i'm missing something. help appreciated.
you can utilize spy() instead of mock()
. can maintain set value. have option. can utilize doreturn("f1 racing").when(message).getcategory();
homecoming value want return.
java unit-testing mocking testng mockito
Comments
Post a Comment