2

I'm quite new to testing with Mockito/Powermockito so I may have missed some important details about it. Here is a part of my code under test (I use the EncryptedPreferences library to encrypt the SharedPreferences data on my Android device):

public static boolean isTargetAddress(Context context, String address) {
        EncryptedPreferences encryptedPreferences = new EncryptedPreferences.Builder(context).
                withEncryptionPassword(Constants.AES_PASSWORD).build();

        Boolean bool = encryptedPreferences.contains("test");

        return false;
}

And this is my test case for this code:

@RunWith(PowerMockRunner.class)
@PrepareForTest({EncryptedPreferences.class, EncryptedPreferences.Builder.class, Context.class})
public class BluetoothReceiverTest {
    EncryptedPreferences encPreferences = PowerMockito.mock(EncryptedPreferences.class);
    Context context = PowerMockito.mock(Context.class);

    @Test
    public void testNullTargetAddress() throws Exception {
        PowerMockito.whenNew(EncryptedPreferences.class).withAnyArguments().thenReturn(encPreferences);

        PowerMockito.doReturn(Boolean.TRUE).
                when(encPreferences).contains(Mockito.anyString());

        PowerMockito.when(encPreferences.contains(Mockito.anyString())).thenReturn(Boolean.TRUE);

        PowerMockito.when(encPreferences.contains("test")).thenReturn(Boolean.TRUE);

        Assert.assertTrue(BluetoothReceiver.isTargetAddress(context, "test"));
    }
}

I want to mock the EncryptedPreferences class to test some edge cases of my program. The problem is that I get this error:

java.lang.NullPointerException
    at com.lukanin.testappjava2.bluetooth.BluetoothReceiver.isTargetAddress(BluetoothReceiver.java:146)
    at com.lukanin.testappjava2.bluetooth.BluetoothReceiverTest.testNullTargetAddress(BluetoothReceiverTest.java:37)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.junit.internal.runners.TestMethod.invoke(TestMethod.java:68)

Line 146: Boolean bool = encryptedPreferences.contains("test");

Line 37: Assert.assertTrue(BluetoothReceiver.isTargetAddress(context, "test"));

What's wrong with my code? Why I can't mock the encryptedPreferences.contains(String str) method?

Is there any string like Mockito's MockitoAnnotations.initMocks(this); required for Powermockito in such case?

Edit 1:

I have read What is a NullPointerException, and how do I fix it? and it's helpful, but doesn't address my problem.

I MOCKED the creation of the EncryptedPreferences object by using:

EncryptedPreferences encPreferences = PowerMockito.mock(EncryptedPreferences.class);
PowerMockito.whenNew(EncryptedPreferences.class).withAnyArguments().thenReturn(encPreferences);

So then I think I can also mock the encryptedPreferences.contains(String str) method with

PowerMockito.when(encPreferences.contains(Mockito.anyString())).thenReturn(Boolean.TRUE);

But it doesn't work :(

Community
  • 1
  • 1
trinarr
  • 51
  • 6
  • Have you tried adding the class under test (the one containing `isTargetAddress`) to `@PrepareForTest`? And wouldn't you have to mock `EncryptedPreferences.Builder` via `whenNew` as well? – beatngu13 Jan 07 '17 at 22:30
  • @beatngu13 I will try to add the class under test to `@PrepareForTest`. As for the `EncryptedPreferences.Builder` constructor mocking, it doesn't make any sense :( – trinarr Jan 07 '17 at 22:42
  • As for my experience using *PowerMock(ito)* is a surrender to bad design. You should change your code under test to use *dependency injection* then you could use plain Mockito for mocking. – Timothy Truckle Jan 08 '17 at 12:03
  • I don't think that's a duplicate, pretty different question. @TimothyTruckle not per se, but definitely worth mentioning. – beatngu13 Jan 08 '17 at 12:27
  • @beatngu13 *"not per se"* I agree, But at least **I** never had the need of using PowerMock again since I strictly do TDD and have SRP/SoC + IoC in mind... – Timothy Truckle Jan 08 '17 at 13:29

0 Answers0