Stack Overflow
Stack Overflow

Reputation: 13

Setting values through mockito not working

Consider the following class:

public class FacadeImplTest {
/*Class to test*/
private FacadeImpl facade;
@Mock
private Test test;
@Mock
private Test1 test1;
 @Before
public void setUp() throws Exception {

    MockitoAnnotations.initMocks(this);
    facade = new FacadeImpl();

    test.setID("1234");


}
@Test
public void testOrder() throws Exception {
// Have methods to test
}

Here Test class is a pojo class which has its getters and setters for id variable. The test.setID("1234") should set the id in the Test class, but it is not setting and returning null. Am I missing something here?

But when I try instantiating the Test class and then setting the value it is working fine.

Upvotes: 1

Views: 3064

Answers (1)

Mattias
Mattias

Reputation: 1110

Yes, you would need to mock what happens when the get value returns:

when(test.get("1234")).thenReturn("1234");

Calling set on a mock does not do anything. The mock does not have any concept of the fields being contained, it just knows what is called and what it returns, ie. you need to specify specific return values for specific inputs.

Upvotes: 2

Related Questions