When we should use Mockito on Unit test?

As I mentioned, I would like to tell you something about Mockito on unit tests. You should know about where use unit test, because I will use that context for this post.

What Mockito is?

I changed my thinking about Mockito object and this step allows me to use it only in a good place for it. I think about Mockito objects only like data source (on "given" section) and communicate with other system (on "then" section). Why? Because I think when you use a lot of Mockito objects, you don't have a single responsibility in testing code. You use mock or stub for a lot of objects, because you think that part of the code isn't currently important. I don't think that it doesn't important if you have on that method. Maybe it should be another function in another place. When you think like me, You will use it only for a really important place for it.

What can I stubbing?

As I mentioned You can stubbing data sources. This is a good place for it, because You should prepare input data for tests without physical data (speed is important). I think about it like parameters for functions, but from the other side. I stubbing database and other systems since I would like to get data.

Another place for stubbing is sending data to other systems. You should check it if your function sends it. It should be on "then" section when you verify your code.

Example of these places:

private final taskName = "taskName";
private TaskService taskService;
@Mock
private TaskRespository taskRepository;
@Before
public void prepareTaskService(){
    taskService = new TaskService(taskRepository);
}

@Test
public void shouldDoesnotAddTaskIfExistsWithTheSameName () {
    // given
    Task task = new Task(taskName);
    when(taskRepository.findByName(taskName)).thenReturn(task);
    // when
    taskService.add(task);
    // then
    verify(taskRepository, never()).save(any(Task.class));
}

@Test
public void shouldAddTaskIfDoesNotExistsWithTheSameName () {
    // given
    Task task = new Task(taskName);
    when(taskRepository.findByName(taskName)).thenReturn(null);
    // when
    taskService.add(task);
    // then
    verify(taskRepository).save(task);
}

What I did wrong?

During learning about testing and Mockito I did a lot of bad code. I know about it and I show you that. Please don't repeat my mistakes.

Stubbing entity/dto/structure - I am stubbing entity where I think this is easier. This is problematic, because in your functions you sometimes change this entity, but your stub is not changed. Sometimes my test worked because I have stubbed object. Debugging that code is very hard.

Stubbing all services on our code - I think this is wrong too, because some services is important on ours code. I know we should only testing our function, but if you stub service which return max from the array, you will be a problem with data integrity. Sometimes we should think about what we stubbing and what we should stubbing. I think we shouldn't stubbing data processing, because this is an important place in our code.

Limit use Mockito

I think Mockito, mocking and stubbing is very helpful. I am grateful that team of programmers working for creating it. Programmers like to use something too much and I have seen Mockito is not exceptional. I would like to repair test world, so I will be happy If you use Mockito only in that place where you should use it, because test code is less sensitive to bug and more readable.

Comments

Popular posts from this blog

Why TDD is bad practice?

How correctly imitate dependencies?

Software development using Angular 5 with Spring Boot