gpt4 book ai didi

java - 使用 PowerMockito 和 Mockito 时未完成的 stub 异常

转载 作者:行者123 更新时间:2023-11-29 06:52:37 31 4
gpt4 key购买 nike

我正在尝试使用 PowerMockito 和 Mockito,如下所示:

@RunWith(PowerMockRunner.class)
@PrepareForTest({AmqpMessagePublisher.class})
public class OrderItemManagerImplTest {

@Spy
@InjectMocks
private OrderItemManager orderItemManager = new OrderItemManagerImpl();

@Mock
private OrderReleaseManager srReleaseManager;

@Spy
private srOrderTransformer srOrderTransformer;

private OrderItemEntry getOrderItemEntry() {
OrderItemEntry orderItemEntry = new OrderItemEntry();
orderItemEntry.setProcessingStartDate("2017-03-01");
return orderItemEntry;
}

@Before
public void setUp() throws Exception {
MockitoAnnotations.initMocks(this);
PowerMockito.mockStatic(AmqpMessagePublisher.class);
PowerMockito.doNothing().when(AmqpMessagePublisher.class);
}

@Test
public void testCreateOrder() throws Exception {
OrderItemEntry expected = getOrderItemEntry();
String expectedItemId = expected.getItems().get(0).getItemId();
// the below line is throwing the exception
Mockito.when(srReleaseManager.getReleaseByStoreReleaseId(expectedItemId)).thenReturn(null);
}
}

在尝试测试上面的代码时抛出异常

org.mockito.exceptions.misusing.UnfinishedStubbingException: 
Unfinished stubbing detected here:
-> at org.powermock.api.mockito.internal.PowerMockitoCore.doAnswer(PowerMockitoCore.java:36)

E.g. thenReturn() may be missing.
Examples of correct stubbing:
when(mock.isOk()).thenReturn(true);
when(mock.isOk()).thenThrow(exception);
doThrow(exception).when(mock).someVoidMethod();
Hints:
1. missing thenReturn()
2. you are trying to stub a final method, you naughty developer!
3: you are stubbing the behaviour of another mock inside before 'thenReturn' instruction if completed

at com.myntra.sr.manager.impl.jb.OrderItemManagerImplTest.testCreateOrder(OrderItemManagerImplTest.java:74)
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:66)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.runTestMethod(PowerMockJUnit44RunnerDelegateImpl.java:310)
at org.junit.internal.runners.MethodRoadie$2.run(MethodRoadie.java:86)
at org.junit.internal.runners.MethodRoadie.runBeforesThenTestThenAfters(MethodRoadie.java:94)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.executeTest(PowerMockJUnit44RunnerDelegateImpl.java:294)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl$PowerMockJUnit47MethodRunner.executeTestInSuper(PowerMockJUnit47RunnerDelegateImpl.java:127)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl$PowerMockJUnit47MethodRunner.executeTest(PowerMockJUnit47RunnerDelegateImpl.java:82)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.runBeforesThenTestThenAfters(PowerMockJUnit44RunnerDelegateImpl.java:282)
at org.junit.internal.runners.MethodRoadie.runTest(MethodRoadie.java:84)
at org.junit.internal.runners.MethodRoadie.run(MethodRoadie.java:49)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.invokeTestMethod(PowerMockJUnit44RunnerDelegateImpl.java:207)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.runMethods(PowerMockJUnit44RunnerDelegateImpl.java:146)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$1.run(PowerMockJUnit44RunnerDelegateImpl.java:120)
at org.junit.internal.runners.ClassRoadie.runUnprotected(ClassRoadie.java:34)
at org.junit.internal.runners.ClassRoadie.runProtected(ClassRoadie.java:44)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.run(PowerMockJUnit44RunnerDelegateImpl.java:122)
at org.powermock.modules.junit4.common.internal.impl.JUnit4TestSuiteChunkerImpl.run(JUnit4TestSuiteChunkerImpl.java:106)
at org.powermock.modules.junit4.common.internal.impl.AbstractCommonPowerMockRunner.run(AbstractCommonPowerMockRunner.java:53)
at org.powermock.modules.junit4.PowerMockRunner.run(PowerMockRunner.java:59)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)

我已经经历了this问题还有this问题。但他们都没有帮助。有人可以让我知道我做错了什么吗?

最佳答案

你必须在模拟之后调用静态方法。请参见下面的示例。

PowerMockito.doNothing().when(StaticService.class);
StaticService.executeMethod();

引用:https://github.com/powermock/powermock/wiki/MockitoUsage#how-to-stub-void-static-method-to-throw-exception

编辑:

So should I move PowerMockito.doNothing().when(AmqpMessagePublisher.class); inside testCreateOrder from testSetup and call the method from which AmqpMessagePublisher is getting called?

你可以把它留在原处。问题是这样的。当您模拟 void 静态方法时,PowerMocks API 中的事情会变得有点棘手。传统的 doNothing().when(someObject).mockedMethod(); 不起作用。他们解决这个问题的方式有点奇怪。他们希望您在 doNothing().when(AmqpMessagePublisher.class) 行之后立即调用被模拟的实际静态 void 方法。这告诉 PowerMock 从 AmqpMessagePublisher 模拟哪个静态方法。因此,例如假设您在 AmqpMessagePublisher 中有 10 个静态无效方法,doNothing() 没有传达要模拟其中的哪些方法。但下一行确实如此。

关于java - 使用 PowerMockito 和 Mockito 时未完成的 stub 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42726074/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com