-1

I have the following test set up. I don't understand how the test is passing successfully since the user is set to "Dummy."

@RunWith(MockitoJUnitRunner.class)
public class TodoServiceAbstractImplTest
{

    @InjectMocks
    TodoServiceAbstractImpl todoServiceAbstractImpl = new TodoServiceAbstractImpl();

    @Mock
    SomeRandomClass someRandomClass;

    @Mock
    TodoServiceAbstract todoServiceAbstract;

    @Test
    public void testRetrieveTodo_usingAMock(){

        todoServiceAbstractImpl.setUser("Dummy"); //Set the user to be "Dummy" already

        assertEquals(null,todoServiceAbstractImpl.getUser()); //Why is the user is still null?

    }
}

Here are the relevant classes. I created them to test Mockito as I am still learning testing in Spring Boot.

Definition of the SomeRandomClass:

public class SomeRandomClass{

    private String field;

     public SomeRandomClass(){
    }

    public SomeRandomClass(String field){
        setRandom(field);
    }

    public void setRandom(String field){
        this.field = field;
    }

    public String getRandom(){
        return field;
    }

}

Definition of the Abstract class:

public abstract class TodoServiceAbstract {

    @Autowired
    private SomeRandomClass RandomUser;

    public TodoServiceAbstract(){
        //RandomUser = new SomeRandomClass();
    }


    public void setUser(String user){
        this.RandomUser.setRandom(user);
    }

    public String getUser(){
        return RandomUser.getRandom();
    } 


    public abstract List<String> retrieveTodos(String user);


}

Definition of the Abstract Implementation

public class TodoServiceAbstractImpl extends TodoServiceAbstract{


    public List<String> retrieveTodos(String user){
        if(user == getUser()){
            return Arrays.asList("item 1", "item 2",
                "item 3");
        }

        return Arrays.asList("Random item");

    }

}
dejonathan
  • 15
  • 1
  • 7
  • 1
    Why do you expect something else than `null`? `SomeRandomClass` is mocked so it obviously doesn't actually set anything when calling `setUser`. And why should it? That's the point of a mock. – Tom Feb 14 '17 at 00:16
  • 3
    @Tom Your comment is the correct answer. If you post it as an answer, I shall upvote it. – Dawood ibn Kareem Feb 14 '17 at 00:20
  • @DavidWallace Feel free to do that yourself, I don't answer such questions. Thank you :). – Tom Feb 14 '17 at 00:21
  • @Tom yeah you right. I shouldn't be expecting anything. I'm new to the testing world and was trying to figure why my code didn't work before understanding Mock. This post helped me a lot though: http://stackoverflow.com/questions/2277039/how-do-mock-frameworks-work – dejonathan Feb 14 '17 at 16:59

2 Answers2

3

Tom answered in the comments:

Why do you expect something else than null? SomeRandomClass is mocked so it obviously doesn't actually set anything when calling setUser. And why should it? That's the point of a mock.

Remember that mocked implementations are not real, and in particular unstubbed calls will return dummy values such as null, 0, or an empty string.

Community
  • 1
  • 1
Jeff Bowman
  • 90,959
  • 16
  • 217
  • 251
1

In addition to what Tom already said in the comments, this test is testing your mocks, rather than your actual implementation. Since you mocked SomeRandomClass, your tests should verify if that method is being called. In this case you should test if SomeRandomClass.setRandom() is called when you call setUser() and likewise, you should test if SomeRandomClass.getRandom() is called when you call getUser().

For example:

@Test
public void getUser_shouldUseGetRandom() {
    when(someRandomClass.getRandom()).thenReturn("data");
    assertEquals("data", todoServiceAbstractImpl.getUser());
}

To test setUser() you can do something like:

@Test
public void setUser_shouldUseSetRandom() {
  todoServiceAbstractImpl.setUser("data");
  verify(someRandomClass).setRandom("data");
}

By mocking/stubbing you can write proper unit tests for TodoServiceAbstractImpl without having to take the behaviour of SomeRandomClass.

g00glen00b
  • 41,995
  • 13
  • 95
  • 133