1

I have this mock:

@Mock
ObjectMetadata metaMock;

I then use it in a test with the following line of code:

when(client.getObjectMetadata(bucket, key)).thenReturn(metaMock);

My test returns a NullPointerException at this line in my method under test:

ObjectMetadata meta = client.getObjectMetadata(bucket, key);

As best as I can tell this should work. Am I doing anything wrong here? the only thing I can think is that metaMock may need to have something assigned to it. Is this the case?

pac
  • 491
  • 1
  • 7
  • 30
  • You need to show us more of the code, but it looks like the client has not been initialized. – Adriaan Koster Feb 05 '18 at 12:12
  • 1
    I don't think the standard NPE answer suffices here. An answer which is specific to this question would address how to initialise a `@Mock` object. For example: run the test with `@RunWith(MockitoJUnitRunner.class)` or initialise all mocked class members in a `@Before` method like so: `MockitoAnnotations.initMocks(this);`. – glytching Feb 05 '18 at 12:13
  • can you show more code. from the above code mocking is redundant – zealvault Feb 05 '18 at 12:14

0 Answers0