'Easy Mock - addMockedMethod of partialMockBuilder returns null instead of expected return value

I have a method(upsertMethod) which calls another method (generateRSA2048KeyPair) with in the class

public EmailDomainResponse upsertMethod(String subDomain, String domain) {
...
....
String selector = properties.getCustomDKimSelectors().get(key);
Pair<String, String> keyPair = generateRSA2048KeyPair(); // this will be a mocked method in test class
String privateKey = keyPair.getKey() // null Pointer exception since test case addMockedMethod generateRSA2048KeyPair() returns null 
jweService.encryptSymmetric(privateKey.toString(), SomeClass.class);
....
...

I want to write a unit test for this method upsertMethod using easyMock and here is how I did

@Before
    public void setup() {
       partialMock =  partialMockBuilder(SomeClass.class)
                .addMockedMethod("generateRSA2048KeyPair").createMock();
       properties = createMock(RefreshableProperties.class);
       jweService = createMock(JsonWebEncryptionService.class);
       .....
}

@Test
    public void testUpsertMethodSuccessful() throws Exception {
 Map<String, String> selectors = new HashMap<>();
selectors.put("selector1", "value");
Pair<String, String> keyPair = new Pair("publicKey", "privateKey");
expect(properties.getCustomDKimSelectors()).andReturn(selectors).anyTimes();
expect(partialMock.generateRSA2048KeyPair()).andReturn(keyPair).anyTimes();
expect(jweService.encryptSymmetric(keyPair.getValue(), SomeClass.class).andReturn("encryptedPrivateKey");
partialMock.UpsertMethod("sub","domain"); // null pointer exception since partialMock.generateRSA2048KeyPair() return null instead of keyPair
}

Why generateRSA2048KeyPair returns null in test case even when I return keyPair in testcase?



Solution 1:[1]

You forgot to call replay() before doing the test. Easymock has 2 phases: record and replay. The expectations are set during the recording phase and using them during the replay phase. You can think of it as the "Given" and the "When" of BDD.

So your code should be something like this (I also took the liberty of making it cuter).

public class SomeClassTest {
    private JsonWebEncryptionService jweService;

    private RefreshableProperties properties;

    private SomeClass partialMock;

    @Before
    public void setup() {
        properties = mock(RefreshableProperties.class);
        jweService = mock(JsonWebEncryptionService.class);

        partialMock =  partialMockBuilder(SomeClass.class)
                .addMockedMethod("generateRSA2048KeyPair")
                .withConstructor(String.class, RefreshableProperties.class, JsonWebEncryptionService.class)
                .withArgs("key", properties, jweService)
                .mock();
    }

    @Test
    public void testUpsertMethodSuccessful() {
        Map<String, String> selectors = Collections.singletonMap("selector1", "value");
        Pair<String, String> keyPair = new Pair<>("publicKey", "privateKey");
        expect(properties.getCustomDKimSelectors()).andStubReturn(selectors);
        expect(partialMock.generateRSA2048KeyPair()).andStubReturn(keyPair);
        expect(jweService.encryptSymmetric(keyPair.getKey(), SomeClass.class)).andReturn("encryptedPrivateKey");

        replay(jweService, properties, partialMock);

        partialMock.upsertMethod("sub","domain"); // null pointer exception since partialMock.generateRSA2048KeyPair() return null instead of keyPair

        verify(jweService, properties, partialMock);
    }
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Henri