我不明白为什么会出现这种情况,因为我的测试肯定要求PowerMock为它做准备。
@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);
}
虽然我在一开始就有@PrepareForTest({AmazonSQSClientBuilder.class})
,但它显示PowerMockito.mockStatic(AmazonSQSClientBuilder.class);
行出错。
错误如下:
[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)
我错过了什么吗?
1条答案
按热度按时间kr98yfug1#
你可以试着扩展
PowerMockTestCase
。它会解决你的问题。顺便说一句,我推荐使用mockito-inline而不是Powermock来模拟静态方法或类。因为它比Powermock更容易也更流行。你可以轻松地模拟静态,而不需要多余的注解,如下所示:
参考:https://www.baeldung.com/mockito-mock-static-methods