flxh
flxh

Reputation: 565

Verify calls to dependency in unit test?

For this following class I want to write a unit test:

public class SomeClass {

    private Dependency dependency;

    public SomeClass(Dependency dep){
        this.dependency = dep;
    }

    private String processString(String s){
        /*
        edit the string and return
         */
    }

    public void doSomething(String arg){

        String processed = processString(arg);

        dep.doSomethingElse(processed);
    }
}

At first I would stub all the methods SomeClass calls on Dependency in order to test my class in isolation. But the question I couldn't yet find an answer to is :

Should I check how SomeClass calls methods of Dependency e.g. what parameters are passed etc. ? Of course this is a pretty trivial example, but I want to know if this should be part of a unit test in general.

Edit: In my case Dependency would be third party api library which I dont control. So I would consider it important what parameters are passed to these functions however I'm not sure this should be part of a unit test.

Upvotes: 5

Views: 2570

Answers (4)

WesternGun
WesternGun

Reputation: 12728

I think so. You should capture the args. I think it's worth checking you interacted with the dependency, and also how you did it.

Upvotes: 0

Rafał Krajnik
Rafał Krajnik

Reputation: 36

I would say that if dependency is invoked then you should have at least one test case to check if it is called. If you don't want to cover this case, that means (for me) you don't need to call it anyway. This is very important when you have any conditional statements like if/else/switch. Can you imagine that just by mistake you removed this line of code

 dep.doSomethingElse(processed);

Without checking if dependency was invoked you will even not notice that you removed it.

The test can looks like:

import static org.fest.assertions.Assertions.assertThat;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
import org.mockito.Captor;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.runners.MockitoJUnitRunner;

@RunWith(MockitoJUnitRunner.class)
public class SomeClassTest {

  @Mock
  private Dependency dependency;

  @InjectMocks
  private SomeClass someClass;

  @Captor
  private ArgumentCaptor<String> argumentCaptor;

  @Test
  public void shouldCallDependency() throws Exception {
    //given
    String arg = "arg";

    //when
    someClass.doSomething(arg);

    //then
    Mockito.verify(dependency).doSomethingElse(argumentCaptor.capture());
    assertThat(argumentCaptor.getValue()).isEqualTo("processed");
  }

}

Upvotes: 2

Thomas Stets
Thomas Stets

Reputation: 3035

That really depends on the situation, and is highly opinion-based...

I would say, if the calls to the dependency just helps your method to do its job, don't test the call. Just test whether your method does its job.

If the call to the dependency is important, like when the call to the dependency is an important part of the functionality, or even the whole reason for the execution of your method, you should consider testing that the dependency is called correctly.

Ask yourself: do you really care that the dependency is called, and called correctly, or do you just care that your method under test does its job?

Or to look at it from a different perspective: Is the dependency just a part of your class, or is it an independent Object your class is interacting with?

I know, this is rather ambiguous, but I hope you get the idea.

Upvotes: 1

Sabir Khan
Sabir Khan

Reputation: 10132

Testing Dependency shouldn't be part of your unit tests of class SomeClass i.e. don't test method calls of Dependency for correctness and accuracy.

You can check parameter values passed to methods of Dependency if those values are local to class SomeClass i.e. created and managed by SomeClass otherwise perform normal input check assertions.

Upvotes: 1

Related Questions