我正在尝试使用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

编辑:


  所以我应该移动PowerMockito.doNothing()。when(AmqpMessagePublisher.class);从testSetup的testCreateOrder内部,并调用从中调用AmqpMessagePublisher的方法?


您可以将其保留在原处。问题是这样的。当您模拟void静态方法时,PowerMocks API会有些棘手。传统的doNothing().when(someObject).mockedMethod();不起作用。他们解决这个问题的方式有点怪异。他们希望您在doNothing().when(AmqpMessagePublisher.class)行之后调用要模拟的实际static void方法。这告诉PowerMock从AmqpMessagePublisher模拟哪个静态方法。举例来说,假设您在AmqpMessagePublisher中有10个静态void方法,doNothing()不会传达要模拟的方法。但是下一条即时线可以。

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

10-10 08:01