java - Mockito:模拟 Hibernate Criteria uniqueResult 时出现 NullPointerException

标签 java unit-testing mockito

我正在学习 Mockito,我想要模拟方法 doubleCountAnimal(Session) 中使用的 Hibernate session 和 Hibernate 标准:

public class ZooKeeper {

  public int doubleCountAnimal(Session session) {
    long rowCount = (long) session.createCriteria(Animal.class)
        .setProjection(Projections.rowCount())
        .uniqueResult();
    return (int) rowCount * 2;
  }
}

这是我用 Mockito 编写的单元测试:

public class ZooKeeperMockTest {

  private static final Animal[] animals = new Animal[] {new Animal("Cat"), new Animal("Dog")};
  private Session mockedSession;
  private Criteria mockedCriteria;

  @Before
  public void setUp() {
    mockedSession = Mockito.mock(Session.class);
    mockedCriteria = Mockito.mock(Criteria.class);

    Mockito.reset(mockedSession, mockedCriteria);
    // mock session
    Mockito.when(mockedSession.createCriteria(Animal.class)).thenReturn(mockedCriteria);
    // mock criteria
    Mockito.when(mockedCriteria.setProjection(Projections.rowCount())).thenReturn(mockedCriteria);
    Mockito.when(mockedCriteria.uniqueResult()).thenReturn((Object) (animals.length * 1L));
  }

  @Test
  public void testDoubleCountAnimal() {
    ZooKeeper zooKeeper = new ZooKeeper();
    assertEquals(animals.length * 2, zooKeeper.doubleCountAnimal(mockedSession));
  }
}

但是,当我运行测试时,我在以下行中遇到异常:

        .uniqueResult();

我不明白为什么会出现此错误。我认为上述方法 uniqueResult 的模拟在测试之前就已经完成了。有人可以帮忙吗?

Running io.mincongh.zoo.mockito.ZooKeeperMockTest
Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 0.065 sec <<< FAILURE!
testDoubleCountAnimal(io.mincongh.zoo.mockito.ZooKeeperMockTest)  Time elapsed: 0.064 sec  <<< ERROR!
java.lang.NullPointerException
    at io.mincongh.zoo.ZooKeeper.doubleCountAnimal(ZooKeeper.java:14)
    at io.mincongh.zoo.mockito.ZooKeeperMockTest.testDoubleCountAnimal(ZooKeeperMockTest.java:42)
    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.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.apache.maven.surefire.junit4.JUnit4Provider.execute(JUnit4Provider.java:252)
    at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:141)
    at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:112)
    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.apache.maven.surefire.util.ReflectionUtils.invokeMethodWithArray(ReflectionUtils.java:189)
    at org.apache.maven.surefire.booter.ProviderFactory$ProviderProxy.invoke(ProviderFactory.java:165)
    at org.apache.maven.surefire.booter.ProviderFactory.invokeProvider(ProviderFactory.java:85)
    at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:115)
    at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:75)


Results :

Tests in error: 
  testDoubleCountAnimal(io.mincongh.zoo.mockito.ZooKeeperMockTest)

我的问题与模拟框架有关,请不要将其作为简单NullPointerException的重复项来关闭。

最佳答案

感谢Jim Garrison在评论中的帮助下,我发现了错误。

The error appears to be that the mocked Criteria#setProjection() is returning null instead of this to allow method chaining. I haven't worked with Mockito in years but isn't there some way of overriding the mocking to return this?

答案是肯定的,有一种方法可以在单元测试中使用 Mockito#anyObject 来实现:

Mockito.when(mockedCriteria.setProjection((Projection) Mockito.anyObject()))
    .thenReturn(mockedCriteria);

关于java - Mockito:模拟 Hibernate Criteria uniqueResult 时出现 NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38707862/

相关文章:

Java减去字符串中的字符代码的值

unit-testing - Moq - 如何对方法中引用的更改进行单元测试

intellij-idea - 如何编写一个健壮的结构搜索模板来报告 Mockito times(1)/Times(1) 在 IntelliJ IDEA 中传递给验证?

java - 在 Mock 对象上调用方法是调用真实方法而不是模拟实现

java - 在 Java 继承中隐藏字段

javascript - AngularJS - 复选框和下拉列表

java - 从数组列表中删除 [ 和 ]

java - Ant 是否可以在构建结束时列出失败的测试?

rest - 无法使用golang中的gorilla mux从url读取变量

java.lang.AssertionError : in Mockito. 如何解决?