類,這是對被模擬
@Transactional(propagation=Propagation.REQUIRED, rollbackFor=RuntimeException.class)
@Repository
public class Sample {
@Autowired
SessionFactory sessionFactory;
public Pdt loadpdtType(String planName)
{
Pdt pdt=new Pdt();
System.out.println("I am called");
Session session = sessionFactory.getCurrentSession();
Query query = session.createQuery("From Dmaster where planName=:planName order by id")
.setParameter("planName", planName);
pList = query.list();
pdt.setSI(pList.get(0).getSI());
if(planName.equalsIgnoreCase("ABCD")
pdt.setPDTType("xyz");
else
pdt.setPDTType("1234");
return pdt;
}
public Resp getSeq(String Id,String type){
Resp r=new Resp(Id,type);
return r;
}
}
我需要調用getpdtType()和getSeq()從junit的方法。我的測試方法如下
class SampleTester {
@Autowired
Sample samp;
Pdt pdt;
@Test
public void Testt() {
try {
samp=Mockito.mock(Sample.class);
Mockito.when(samp.loadpdtType("sampletext").thenCallRealMethod();
pdt=samp.loadpdtType("sampletext");
Mockito.verify(samp).loadpdtType("sampletext");
System.out.println("testresult:"+pdt.getPDTType());
} catch(Exception e) {
e.printStackTrace();
}
}
}
和輸出是
我叫
顯示java.lang.NullPointerException 在com.docs.Sample.loadpdtType(Sample.java:58) classes/:?] at com.docs.Sample $$ EnhancerByMockitoWithCGLIB $$ f5b2927b.CGLIB $ loadpdtType $ 0()[mockito-core-1.10.19.jar :?] at com.docs.Sample。$$ EnhancerByMockitoWithCGLIB $ $ f5b2927b $$ FastClassByMockitoWithCGLIB $$ 15d15c8c.invoke()[mockito-core-1.10.19.jar :?] at org.mockito.c glib.proxy.MethodProxy.invokeSuper(MethodProxy.java:216)[mockito-core-1.10.19.jar :?] at org.mockito.internal.creation.cglib.DelegatingMockitoMethodProxy.invokeSuper(DelegatingMockitoMethodProxy.java:19)[的Mockito核-1.10.19.jar:?] 在org.mockito.internal.invocation.realmethod.DefaultRealMethod.invoke(DefaultRealMethod.java:21)的Mockito核-1.10.19.jar:?] 在有機.mockito.internal.invocation.realmethod.CleanTraceRealMethod.invoke(CleanTraceRealMethod.java:30)的Mockito核-1.10.19.jar:?] 在org.mockito.internal.invocation.InvocationImpl.callRealMethod(InvocationImpl.java: 112)[mockito-core-1.10.19.jar :?] at org.mockito.internal.stubbing.answers.CallsRealMethods.answer(CallsRealMethods.java:41)[mockito-core-1.10.19.jar:?] at org.mockito.internal.stubbing.StubbedInvocationMatcher.answer(Stubb edInvocationMatcher.java:34)的Mockito核-1.10.19.jar:?] 在org.mockito.internal.handler.MockHandlerImpl.handle(MockHandlerImpl.java:91)的Mockito核-1.10.19.jar: ?] at org.mockito.internal.handle.NullResultGuardian.handle(NullResultGuardian.java:29)[mockito-core-1.10.19.jar :?] at org.mockito.internal.handler.InvocationNotifierHandler.handle(InvocationNotifierHandler的.java:38)[的Mockito核-1.10.19.jar:?] 在org.mockito.internal.creation.cglib.MethodInterceptorFilter.intercept(MethodInterceptorFilter.java:59)的Mockito核-1.10.19.jar :?] at com.docs.Sample $$ EnhancerByMockitoWithCGLIB $$ f5b2927b.loadpdtType()[mockito-core-1.10.19.jar :?] at com.documentation.SampleTester.Testt(SampleTester.java:31)[ test-classes /:?] at sun.reflect.NativeMethodAccessorImpl.inv oke0(Native Method)〜[?:1.8.0_91] at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)〜[?:1.8.0_91] at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java :43)〜[:?1.8.0_91] 在java.lang.reflect.Method.invoke(Method.java:498)〜[:?1.8.0_91] 在1 org.junit.runners.model.FrameworkMethod $。 runReflectiveCall(FrameworkMethod.java:50)[junit-4.12.jar:4.12] at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)[junit-4.12.jar:4.12] at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)的junit-4.12.jar:4.12] 在org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17) junit-4.12.jar:4.12] at o rg.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)[junit-4.12.jar:4.12] at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)[junit-4.12.jar: 4.12] at org.junit.runners。BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)[junit-4.12.jar:4.12] at org.junit.runners.ParentRunner $ 3.run(ParentRunner.java:290)[junit-4.12.jar:4.12] at org .junit.runners.ParentRunner $ 1.schedule(ParentRunner.java:71)[junit-4.12.jar:4.12] at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)[junit-4.12.jar: 4.12] at org.junit.runners.ParentRunner.access $ 000(ParentRunner.java:58)[junit-4.12.jar:4.12] at org.junit.runners.ParentRunner $ 2.evaluate(ParentRunner.java:268)[ junit-4.12.jar:4.12] at org.junit.runners.ParentRunner.run(ParentRunner.java:363)[junit-4.12.jar:4.12] at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference .run(JUnit4TestReference.java:50)[.cp/:?] at org.eclipse.jdt。 internal.junit.runner.TestExecution.run(TestExecution.java:38)[.cp/:?] at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)[.cp /:] at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)[.cp /:?] at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner .run(RemoteTestRunner.java:382)[.cp/:?] at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)[.cp/:?]
請加堆棧跟蹤並標記異常發生 – Jens
和格式化你的代碼行。編輯有預覽。 – chrylis
'samp = Mockito.mock(Sample.class); '那麼爲什麼samp中的sessionFactory是非空的? – 2016-09-19 11:33:58