未完成的存根 使用 PowerMockito 和 Mockito 时的异常



我正在尝试使用powermockito&如下所示:

@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)

我已经解决了这个问题以及这个问题。但是他们都没有帮助。有人可以让我知道我做错了什么吗?

嘲笑它后必须调用静态方法。请参阅下面的示例。

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

参考:https://github.com/powermock/powermock/wiki/mockitousage#how-to-toub-void-static-static-method-method-to-throw-exception

编辑:

所以我应该移动powermockito.donothing()。从testSetup中内部的testCreateorder,并调用来自AmqpMessagePublisher的方法?

您可以将其留在原处。问题是这个。当您嘲笑一个空白静态方法时,事情在PowerMocks API中会有些棘手。传统的doNothing().when(someObject).mockedMethod();不起作用。他们解决这个问题的方式有些怪异。他们希望您在doNothing().when(AmqpMessagePublisher.class)行之后拨打嘲笑实际静态空隙方法。这告诉PowerMock从AmqpMessagePublisher嘲笑哪种静态方法。因此,可以说您在AMQPMESSAGEPUBLISHER中有10种静态空隙方法,doNothing()不能传达其中的哪个。但是下一条直线确实如此。

相关内容

  • 没有找到相关文章

最新更新