0

The code snippet retrieves the entity based on parameters.

public void updateNotification(String status, Entity entity ) {
        Entity entity1 = null;
        try {
            switch (status) {
            case "AX":
                entity1 = this.Repository.findByTypeAndParams(
                        1, entity.getParam1(), entity.getParam2(),
                        entity.getParam3());
                if (entity1!= null) {
                    entity1.setCurrentStatusKey("SET");
                    updateEntity(entity1);
                } else {
                    LOGGER.debug("");
                }
                break;

Test case for the above code :

@RunWith(SpringJUnit4ClassRunner.class)
public class ServiceTest {
    @InjectMocks
    CVService cVServiceMock;

    @Mock
    RepositoryMock repositoryMock;

     @Test
            public void testUpdateOut() {
                Entity entity1 = new Entity ();
                entity1.setType(2);
                Mockito.when(repositoryMock.findByTypeAndParams(any(Integer.class), any(String.class),
                        any(String.class), any(String.class))).thenReturn(entity1);
                cVServiceMock.updateNotification("AX", entity1);
            }

The entity1 is always null instead of mocked entity when executed from the test case, what am i doing wrong here?

Karthik Suresh
  • 367
  • 7
  • 23
  • 1
    Consider adding a [mre] or at least the complete test (including the mock creation and other annotations) and the relevant parts of the class under test (method signature, relevant fields, constructors). – second Nov 07 '19 at 09:41
  • 1
    Can you show how the notificationServiceMock is initialized? It would be better to paste all classes both service and test. – Alexey Nov 07 '19 at 09:42
  • And please show also how `RepositoryMock` is created. – Benoit Nov 07 '19 at 09:43

1 Answers1

0

As said in another StackOverflow MariuszS' answer about Mocking static methods with Mockito:

Use PowerMockito on top of Mockito.

[...]

More information:

PowerMockito adds more features and it allows the mocking of private methods, static methods, and more.

With this, you should maybe mock an instance of Repository:

// In your test method
PowerMockito.mockStatic(Repository.class);

Don't forget to add annotation to your test class:

@RunWith(PowerMockRunner.class)
@PowerMockRunnerDelegate(SpringRunner.class) // Since you are using SpringBoot
@PrepareForTest(Repository.class)
@PowerMockIgnore({"javax.management.*", "javax.net.ssl.*", "javax.security.*"}) // Avoid certain classloading related issues, not mandatory

In my example, Mockito.when() is replaced by PowerMockito.doReturn(...).when(...). It could looks like the following:

@Test
public void testUpdateOut() {
    Entity entity1 = new Entity ();
    entity1.setType(2);
    PowerMockito.mockStatic(Repository.class);

    PowerMockito.doReturn(entity1).when(
        Repository.class,
        "findByTypeAndParams", // The name of the method
        Mockito.anyInt(), // The arguments
        Mockito.anyString(),
        Mockito.anyString(),
        Mockito.anyString()
    );

    // Your test
    notificationServiceMock.updateNotification("AX", entity1);

    // And at the end of the test:
    PowerMockito.verifyStatic(); // It will verify that all statics were called one time
}

Note that I replace your any(Integer.class) and any(String.class) with, respectively, anyInt() and anyString().

I hope this will help!

Paul Rey
  • 1,270
  • 1
  • 15
  • 26
  • I don't think that `Repository.findByTypeAndParams` is a static method. Its just that the OP didn't use the Java Conventions for naming his fields. – second Nov 07 '19 at 12:29
  • @second Maybe you're right, in this case, the `mockStatic()` and `verifyStatic()` is useless. I'll wait for his answer. – Paul Rey Nov 07 '19 at 16:26