'org.powermock.api.mockito.ClassNotPreparedException when it's declared to be prepared

I don't understand why this is showing since my test definitely asked PowerMock to prepare for it.

@PrepareForTest({AmazonSQSClientBuilder.class})
@RunWith(PowerMockRunner.class)
@PowerMockIgnore({"javax.*", "org.apache.logging.*"})
class EncodingAdapterTest {

    ...
    private AmazonSQSClientBuilder sqsBuilder;
    private AmazonSQS sqsClient;
    private AmazonSNS snsClient;
    private SendMessageResult sqsResult;
    private PublishResult snsResult;

    @BeforeEach
    void setUp() {
       
        sqsClient = PowerMockito.mock(AmazonSQS.class);
        PowerMockito.mockStatic(AmazonSQSClientBuilder.class);
        AmazonSQSClientBuilder mockBuilder = PowerMockito.mock(AmazonSQSClientBuilder.class);
        PowerMockito.when(AmazonSQSClientBuilder.standard()).thenReturn(mockBuilder);
        sqsBuilder = PowerMockito.mock(AmazonSQSClientBuilder.class);
    }

Although I have @PrepareForTest({AmazonSQSClientBuilder.class}) at the very start, it shows error for line PowerMockito.mockStatic(AmazonSQSClientBuilder.class);.

The error goes as follows:

[Ljava.lang.Object;@259661b4
The class com.amazonaws.services.sqs.AmazonSQSClientBuilder not prepared for test.
org.powermock.api.mockito.ClassNotPreparedException: 

[Ljava.lang.Object;@259661b4
The class com.amazonaws.services.sqs.AmazonSQSClientBuilder not prepared for test.
    at org.powermock.api.mockito.expectation.reporter.MockitoPowerMockReporter.classNotPrepared(MockitoPowerMockReporter.java:29)
    at org.powermock.api.mockito.internal.mockcreation.MockTypeValidatorFactory$DefaultMockTypeValidator.validate(MockTypeValidatorFactory.java:37)
    at org.powermock.api.mockito.internal.mockcreation.AbstractMockCreator.validateType(AbstractMockCreator.java:10)
    at org.powermock.api.mockito.internal.mockcreation.DefaultMockCreator.createMock(DefaultMockCreator.java:49)
    at org.powermock.api.mockito.internal.mockcreation.DefaultMockCreator.mock(DefaultMockCreator.java:40)
    at org.powermock.api.mockito.PowerMockito.mockStatic(PowerMockito.java:62)
    at package.adapters.EncodingAdapterTest.setUp(EncodingAdapterTest.java:76)

Am I missing anything??



Solution 1:[1]

You could try extending the PowerMockTestCase. It would solve your problem.

class EncodingAdapterTest extends PowerMockTestCase 

By the way, I recommend using mockito-inline instead of Powermock to mock static method or class. Because it is easier and more popular than Powermock. You can mock static easily, without superfluous annotations, like this:

try (MockedStatic<AmazonSQSClientBuilder> builder = Mockito.mockStatic(AmazonSQSClientBuilder.class)) {
        builder.when(() -> AmazonSQSClientBuilder.standard())
        .thenReturn("somthing");
}

Reference: https://www.baeldung.com/mockito-mock-static-methods

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