0

I'm working on testing a class. This class calls on a service (let's say its called client, and we want to call client.put())

put() should return a response, but at least in the test, the response is null

I don't know if I just didn't setup the mock correctly and wanted to sanity check with you guys here

public class ATest {
    @Mock
    private ServiceProto.PutItemsResponse res;
    ...(private variables)...
    @Before
    public void setUp() throws Exception {
        client = mock(Client.class);
        clientFactory = mock(ClientFactory.class);
        when(clientFactory.get(any())).thenReturn(client);
        ...(initializing private vars for constructor as mock variables, example below...)
        captionConverter = mock(CaptionToCTItemConverter.class);
       when(privateVar.convert(any(obj.class))).thenReturn(Item.newBuilder().build());
     
        classAToTest = spy(new ClassAToTest(private variables);
    }
    @Test
    public void putItem() {
        long id = 4710582L;
        AObject aObject = testUtils.getObject();
        doReturn(res).when(client).putItems(any(ServiceProto.PutItemsRequest.class));
        System.out.println("result is "+ res);
        try {
             classAToTest.putMethod(aObject);
        }
        catch (NullPointerException e) {
        }
        verify(creativeToolsClient, Mockito.times(1)).putItems(any(IngestionServiceProto.PutItemsRequest.class));
    }

}

And this is the method being tested

public void putMethod(AObject aObject) {
    final String id = Long.toString(aObject.getId());
    ServiceProto.PutItemsResponse putItemsResponse = null;
    Exception putItemsFailure = null;
    putItemsResponse =
            client.putItems(ServiceProto.PutItemsRequest.newBuilder()
                    .putItems(
                            id,
                            ServiceProto.PutItemsRequest.Item.newBuilder()).build())
                    .build());

    if (putItemsResponse == null) {
        logger.warning("PutItems request has failed: "+
                (putItemsFailure == null ? "null" : putItemsFailure.getMessage()));
    }
}

and when I run it it gives the warning

The putItems method works for other people. Did I set up mock incorrectly?

Manny
  • 35
  • 6
  • You need to initialise Mockito so that `res` is actually set to a Mock. See https://stackoverflow.com/questions/40961057/how-to-use-mockito-with-junit5 – tgdavies Jul 06 '22 at 05:12

1 Answers1

0

res variable is not initialized. To mock objects with @Mock annotation use

@ExtendWith(MockitoExtension.class)
public class ATest {
...
Roman
  • 181
  • 7
  • oh I see, but what would I need to import? I can't find it, and it says it can't recognize the module. I already have org.junit.Test – Manny Jul 06 '22 at 06:22
  • the test still runs though, even with the "cannot resolve symbol" – Manny Jul 06 '22 at 06:23
  • MockitoExtension is located in https://mvnrepository.com/artifact/org.mockito/mockito-junit-jupiter. If you don't want to add additional dependencies, mock via static method :( Static method mocking will work 100% – Roman Jul 06 '22 at 06:37
  • im not using maven – Manny Jul 06 '22 at 14:23
  • Then, I guess, static method usage is your choice :) Besides, you already use it before test. – Roman Jul 06 '22 at 15:48
  • I have import org.junit.Before; import org.junit.Test; import org.mockito.Mock; import org.mockito.Mockito; import static org.mockito.Mockito.*; – Manny Jul 06 '22 at 19:41
  • I cant find the right static import? – Manny Jul 06 '22 at 19:42
  • It's the same static method you use before test `@Before public void setUp() throws Exception { client = mock(Client.class); clientFactory = mock(ClientFactory.class);` Static method you need is in wildcard `import static org.mockito.Mockito.*` – Roman Jul 07 '22 at 04:49