简体   繁体   English

Mockito-不可能存根模拟对象

[英]Mockito - Impossible stubbing mocked object

I am newbie in Java world, but it is very hard understand why not can I stub method of a mocked object... 我是Java世界的新手,但很难理解为什么不能对模拟对象的方法进行存根...

@RunWith(MockitoJUnitRunner.class)
public class ChildBLLIT extends BaseInteractorIT {

  @InjectMocks
  private ChildBLL ChildBLL = Mockito.mock(ChildBLL.class);

  @Before
  public void setUp() {
    ChildBLL.engine = engineMock;
  }

  /**
   * Test of getZipStatistics method, of class ChildBLL.
   */
  @Test
  public void testGetZipStatistics() {
    final String testZipStatisticsText = "DummyZipStatistics";
    //This method will throw the null pointer exception
   when(ChildBLL.engine.getZIPStatistics()).thenReturn(testZipStatisticsText);


    ChildBLL.getZipStatistics();
    verify(ChildBLL.engine).getZIPStatistics();
  }

}

When I try to stub the getZIPStatistics() method I get always a null pointer exception, of course I get, because in the getZIPStatistics() method there is an private object, which is not mocked... it seems to me the Mockito does not mocking the private fields... and unfortunately this is from another project: 当我尝试对getZIPStatistics()方法进行存根处理时,我当然总是得到一个空指针异常,因为在getZIPStatistics()方法中有一个私有对象,该对象没有被嘲笑……在我看来,Mockito确实做到了没有嘲笑私有字段...不幸的是,这是来自另一个项目:

public class BaseIT {

  @Mock
  protected static FromOtherProject engineMock;

  @Before
  public void initMocks() {
    MockitoAnnotations.initMocks(this);
  }  
}

Here I mocked the engine variable, but then how can I mock/stub the getZIPStatistics() method? 在这里,我模拟了引擎变量,但是如何模拟/存根getZIPStatistics()方法呢? This is this method: 这是这种方法:

public class FromOtherProject {
    //...
    public final String getZIPStatistics() {
        return ZIPStatistics.toString();
    }
}

What can I do? 我能做什么?

Let's assume a simple class... 让我们假设一个简单的类...

public class Account {

    public String getPassword() {
        return "abc";
    }
}

...and simple class that contains it... ...以及包含它的简单类...

public class AccountHolder {
    private Account account;

    public String getAccountPassword() {
         return this.account.getPassword();
    }

}

So now we have a simple base class for all Account based tests... 因此,现在我们为所有基于帐户的测试提供了一个简单的基类...

public class AccountBasedTest {

   @Mock
   protected Account account;

}

...and a class that actually tests the AccountHolder... ...以及实际测试AccountHolder的类...

@RunWith(MockitoJUnitRunner.class)
public class AccountHolderTest extends AccountBasedTest {

    @InjectMocks
    private AccountHolder accountHolder;

    @Test
    public void getAccountPasswort_must_return_account_password() {
         Mockito.when( this.account.getPassword() ).thenReturn ("xyz");

         Assert.assertEquals("xyz", this.accountHolder.getAccountPassword());
    }

}

And that's all. 就这样。 The @InjectMocks, etc. annotations will also look in the superclasses, so you get your mocked account and that account will be put into your AccountHolder. @InjectMocks等注释也会在超类中查找,因此您将获得模拟帐户,该帐户将被放入AccountHolder中。 No need to call MockitoAnnotations.initMocks . 无需调用MockitoAnnotations.initMocks It shouldn't hurt, but it's not needed because you are using the MockitoJUnitRunner already, which does exactly that. 它不应该受到伤害,但是不需要,因为您已经在使用MockitoJUnitRunner ,它正是这样做的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM