How do I fix `ClassNotPreparedException` when mocking static methods with PowerMockito?

I’m trying to unit test a static method in my Android image loader that calls:

Base64.encodeToString(byteArray, Base64.DEFAULT);

Since Mockito doesn’t support static methods, I switched to PowerMockito and used:

PowerMockito.mockStatic(Base64.class);

with the annotation:

@PrepareForTest({Base64.class})

However, I still get the error:

`org.powermock.api.mockito.ClassNotPreparedException`: The class `android.util.Base64` is not prepared for the test. To prepare this class, add class to the '`@PrepareForTest`' annotation.

My test class, code to be tested, and Gradle dependencies are properly set up, but mocking still fails.

How can I properly prepare and mock the Base64 class in this scenario?

I ran into the exact same problem when trying to mock android.util.Base64.

The key thing I learned is that PowerMockito cannot mock Android framework classes directly in a plain JVM test because they’re not on the standard classpath.

What I did instead was wrap the static call in a helper class:

public class Base64Wrapper {
    public static String encode(byte[] input) {
        return Base64.encodeToString(input, Base64.DEFAULT);
    }
}

Then, in my test, I mock Base64Wrapper instead of android.util.Base64:

@PrepareForTest({Base64Wrapper.class})
public class MyTest {
    @Test
    public void testEncode() {
        PowerMockito.mockStatic(Base64Wrapper.class);
        when(Base64Wrapper.encode(any())).thenReturn("mocked");
    }
}

This bypasses the ClassNotPreparedException completely. It’s a little extra code, but it makes the tests JVM-friendly and stable.

I got hit by this exception a while ago when trying to mock Android static methods.

Even with @PrepareForTest({Base64.class}), PowerMockito couldn’t handle android.util.Base64 because it’s a final Android class loaded by the Android runtime.

What worked for me:

Move the test to a Robolectric test, which can load Android classes in the JVM.

Or, like others suggested, create a wrapper around the static method and mock the wrapper.

For example, with Robolectric you can do:


@RunWith(RobolectricTestRunner.class)
@PrepareForTest({Base64.class})
public class MyTest {
    @Test
    public void testEncode() {
        PowerMockito.mockStatic(Base64.class);
        when(Base64.encodeToString(any(byte[].class), anyInt())).thenReturn("mocked");
    }
}

Without Robolectric, the wrapper approach is safer and avoids this exception entirely.

I struggled with ClassNotPreparedException when trying to mock Base64 too.

Even after adding it to @PrepareForTest, PowerMockito still complained.

The trick I found is that the class must be loaded by the JVM test runner, which Android framework classes aren’t.

My solution was simple: never mock android.util.Base64 directly. Instead, I created a small helper class:

public class MyBase64 {
    public static String encode(byte[] input) {
        return Base64.encodeToString(input, Base64.DEFAULT);
    }
}

Then, in tests:

@PrepareForTest({MyBase64.class})
PowerMockito.mockStatic(MyBase64.class);
when(MyBase64.encode(any())).thenReturn("mocked");

It made the tests run perfectly on the JVM without Robolectric and eliminated the ClassNotPreparedException.