未完成的存根使用PowerMockito&的Mockito

时间:2017-03-10 19:14:06

标签: java mocking mockito powermockito

我正在尝试使用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个问题。但他们都没有帮助。有人能让我知道我做错了吗?

1 个答案:

答案 0 :(得分:2)

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

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)行之后调用被模拟的实际静态void方法。这告诉PowerMock从AmqpMessagePublisher模拟哪个静态方法。例如,假设您在AmqpMessagePublisher中有10个静态void方法,doNothing()不会传达其中哪些是模拟的。但下一个直接线路确实如此。