找到你要的答案

Q:PowerMockito and Mockito conflict

Q:PowerMockito和Mockito的冲突

I need to built unit tests (with junit) for a legacy system. The method that I need to test, makes use of a static method and I need to check if it's called. So, I'll need to use PowerMockito (for "regular" mocking, we use mockito).

But, when I include PowerMockito statements inside the test, Mockito fails with an org.mockito.exceptions.misusing.UnfinishedStubbingException. If I comment the lines PowerMockito.mockStatic(Application.class), PowerMockito.doNothing().when(Application.class) and PowerMockito.verifyStatic(), the UnfinishedStubbingExceptiondoes does not occur, but this way, I'm not able to check if my IllegalArgumentException occured.

The method under test looks like:

public class ClientMB {

   public void loadClient(Client client) {
      try {
         if (client == null) {
            throw new IllegalArgumentException("Client is mandatory!");
         }

         setClient(clientService.findById(client.getId()));
      } catch (Exception ex) {
         Application.handleException(ex);
      }
   }
}

And the test looks like:

@PrepareForTest({ Application.class })
@RunWith(PowerMockRunner.class)
public class ClientMBTest {

   @Test
   public final void testLoadClient() {
      ClientService mockedClientService = Mockito.mock(ClientService.class);
      Mockito.when(mockedClientService.findById(42L)).thenReturn(new Client());

      PowerMockito.mockStatic(Application.class);
      PowerMockito.doNothing().when(Application.class);

      ClientMB cmb = new ClientMB(mockedClientService);
      mb.loadClient(null);

      PowerMockito.verifyStatic();
   }
}

I imported PowerMokito using the latest version.

<dependency>
    <groupId>org.powermock</groupId>
    <artifactId>powermock-module-junit4</artifactId>
    <version>1.6.2</version>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>org.powermock</groupId>
    <artifactId>powermock-api-mockito</artifactId>
    <version>1.6.2</version>
    <scope>test</scope>
</dependency>

What I'm doing wrong? Any advice is welcome.

我需要建立单元测试(Unit)为遗留系统。我需要测试的方法,使用静态方法,我需要检查它是否被调用。所以,我要用PowerMockito(“正规”的嘲讽,我们用mockito)。

但是,当我有PowerMockito的陈述里面的试验,Mockito因org.mockito.exceptions.misusing.unfinishedstubbingexception。如果我的评论线powermockito。mockstatic(应用类),PowerMockito。donothing()。当(应用类)和PowerMockito。verifystatic(),的unfinishedstubbingexceptiondoes不会发生,但这种方式,我不能看我时发生。

被测试的方法看起来像:

public class ClientMB {

   public void loadClient(Client client) {
      try {
         if (client == null) {
            throw new IllegalArgumentException("Client is mandatory!");
         }

         setClient(clientService.findById(client.getId()));
      } catch (Exception ex) {
         Application.handleException(ex);
      }
   }
}

测试看起来像:

@PrepareForTest({ Application.class })
@RunWith(PowerMockRunner.class)
public class ClientMBTest {

   @Test
   public final void testLoadClient() {
      ClientService mockedClientService = Mockito.mock(ClientService.class);
      Mockito.when(mockedClientService.findById(42L)).thenReturn(new Client());

      PowerMockito.mockStatic(Application.class);
      PowerMockito.doNothing().when(Application.class);

      ClientMB cmb = new ClientMB(mockedClientService);
      mb.loadClient(null);

      PowerMockito.verifyStatic();
   }
}

我进口的PowerMokito使用的是最新版本。

<dependency>
    <groupId>org.powermock</groupId>
    <artifactId>powermock-module-junit4</artifactId>
    <version>1.6.2</version>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>org.powermock</groupId>
    <artifactId>powermock-api-mockito</artifactId>
    <version>1.6.2</version>
    <scope>test</scope>
</dependency>

我做错了什么?欢迎任何建议。

answer1: 回答1:
PowerMockito.doNothing().when(Application.class);

That's a stubbing command, but because you don't make a method call after the when(...), it's unfinished.

PowerMockito.doNothing().when(Application.class);
Application.someApplicationMethod();

You need to use this syntax because the normal doVerb().when(foo) syntax will provide an instance, and Java often issues a warning when trying to call a static method based on an instance instead of a class name.

If you want to stub all of Application's methods, you can do so by passing another argument into mockStatic:

PowerMockito.mockStatic(Application.class, RETURNS_SMART_NULLS);
PowerMockito.doNothing().when(Application.class);

这是一个存根的命令,而是因为你不做一个方法调用后的当(…),它是未完成的。

PowerMockito.doNothing().when(Application.class);
Application.someApplicationMethod();

你需要使用这个语法,因为正常的doverb()。当(富)语法将提供了一个实例,和java经常发出警告,当试图调用一个静态方法基于实例而不是类的名字。

如果你想短所有应用的方法,你可以通过另一种说法为mockstatic:

PowerMockito.mockStatic(Application.class, RETURNS_SMART_NULLS);
junit  mockito  powermockito