1

I am using mockito to write some unit tests for an application which is already tested with integration testing but we need also to develop unit tests.

This is the code for the testing :

public class TestResourceB {

@Mock
ResourceB b;
@Mock
ResourceC c;

@Before
public void setup() {
    MockitoAnnotations.initMocks(this);
    TestObjects.InitializeObjects();
}

@Test
public void testMethodResourceA() {
    when(b.callFuncA()).thenCallRealMethod();
    when(b.callFuncB()).thenReturn(TestObjects.mockedListA);
    when(b.callFuncC((B)anyVararg())).thenCallRealMethod();
    when(c.callFuncB()).thenReturn(TestObjects.mockedListB);
    when(c.callFuncA()).thenCallRealMethod
    String output = b.callFuncA();
}
}

This is the class ResourceB

public class ResourceB {

ResourceC c = new ResourceC();

public String callFuncA(){
    /*Calling callFuncB and doing some other stuff*/
    String test = callFuncC(arg1);
}

public List<A> callFuncB() {
    /*returns the mocked list A*/
}

public String callFuncC(B arg1) {
String test2 = c.callFuncA();   // It crashes here
/*doing some other stuff*/
}
}

This is the class ResourceC

public class ResourceC {
public String callFuncA() {
    /*Calling callFuncB and doing some other stuff*/
    return testString;
}

public List<B> callFuncB() {
/*return a List*/
}
}

The problem that I have is that in method callFuncC in class ResourceB when the line

String test2 = c.callFuncA();

is called I get a NullPointerException

Any idea why this could be happening ?

1 Answer 1

6

There is several issues in your code, the first one being that you are mocking the class you want to test, doing that you will only test the mock of ResourceB or you'll have to stub the code and force chosen method to call real code (thenCallRealMethod). The main idea is to never mock the class you are testing.

That is also why you have an NPE, because a mock don't need internal fields instance. As it should'nt need to.

Here's a correct way, there might be variations but that one is the most straightforward. So basically you'd want to test the interactions between ResourceB and ResourceC, as this is a unit test of ResourceB you'd want to mock ResourceC. The thing is mocks are per instance so you have to pass the mocked one to ResourceB.

It can be injected via constructor injection, you then need to modify the ResourceB :

public class ResourceB {
  ResourceC c;

  public ResourceB() { c = new resourceC(); } // normal behavior

  @VisibleForTesting // guava annotation (in, order to expalain why there is this constructor)
  ResourceB(ResourceC c_override) { c = c_override; } // constructor for the test, note the package visibility

  // ...
}

And in the test you'll write it this way :

public class TestResourceB {
  ResourceB tested_b;
  @Mock ResourceC mocked_c;

  @Before
  public void init_tested_and_mocks() {
    MockitoAnnotations.initMocks(this);
    tested_b = new ResourceB(mocked_)
  }

  @Test
  public void ensure_result_from_ResourceC_is_returned() {
    // given 
    when(mocked_c.callFuncA()).thenReturn("result that should be returned");

    // when
    String output = tested_b.callFuncA();

    // then
    assertThat(output).isEqualTo("result that should be returned");
  }
}

By the way here's a few things to add :

  • As you are using JUnit 4.x, I'm using more meaningful/descriptive method names.
  • I'm using the Behavior Driven Development keywords (given, when, then) to help drive the test scenario.
  • Also I use AssertJ lib to write meaningful assertions.
3
  • The reason that I am mocking ResourceB class is that I want to mock a database interaction which takes place inside callFuncB() in ResourceB. Is it possible to mock the result of this function without mocking class ResourceB ?
    – SteveSt
    Jul 15, 2013 at 13:02
  • 1
    @Stefanos I strongly advise you to not do that. In mockito we have the spy but well they are for very particular cases. And most of the time spies lead to code not properly tested, it can lead to a false sense of security. If you wish to test something with a database you should write an integration test and it should connect with the real database, or at least something like H2. Even using embedded database is somehow risky as you can't really rely on it's implementation to mimick the database.
    – bric3
    Jul 15, 2013 at 13:57
  • Another fantastic and highly pedagogical answer from Brice. If I could upvote this many times over, I would. Jul 16, 2013 at 7:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.