と私はモックHibernateセッションをしたいと方法doubleCountAnimal(Session)
に使用されるような基準を休止:Mockito:Hibernateの基準をあざけるときNullPointerExceptionが、私はMockitoを学んでいるuniqueResult
public class ZooKeeper {
public int doubleCountAnimal(Session session) {
long rowCount = (long) session.createCriteria(Animal.class)
.setProjection(Projections.rowCount())
.uniqueResult();
return (int) rowCount * 2;
}
}
そして、ここでMockitoと書かれた私のユニットテストは、です:
.uniqueResult();
:私は、テストを実行すると
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
の模擬試験がすでに行われていると思う。誰かが助けることができますか?
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がの重複としてそれを閉じないでください。
あなたは、静的メソッドをテストするためPowermockを必要としています。 – garnulf
メソッドをstaticからnon-staticに変更しましたが、同じエラーが発生します。更新@garnulf –
を参照してください。[NullPointerExceptionとは何ですか?それを修正するにはどうすればいいですか?](http://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do -i-fix-it) – ManoDestra