2016-09-19 49 views
0

示例代碼單元測試的彈簧的非 - 使用Java控制器方法

類,這是對被模擬

@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 $$ f5b29​​27b.CGLIB $ loadpdtType $ 0()[mockito-core-1.10.19.jar :?] at com.docs.Sample。$$ EnhancerByMockitoWithCGLIB $ $ f5b29​​27b $$ 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 $$ f5b29​​27b.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/:?]

+0

請加堆棧跟蹤並標記異常發生 – Jens

+0

和格式化你的代碼行。編輯有預覽。 – chrylis

+0

'samp = Mockito.mock(Sample.class); '那麼爲什麼samp中的sessionFactory是非空的? – 2016-09-19 11:33:58

回答

0

這裏有很多東西。

  1. 你不需要下面一行:

    Mockito.when(samp.loadpdtType( 「sampletext」)thenCallRealMethod();

    ,因爲你正在測試的。 Sample因此它顯然會調用loadpdtType方法

  2. 您需要在中設置SessionFactory的模擬類並模擬所有被調用的方法sessionFactory in loadpdtType

你需要的東西象下面這樣:

class SampleTester { 

@Mock SessionFactory mockSessionFactory; 
@Mock Session mockSession; 

@InjectMock 
Sample samp; 

Pdt pdt; 

@Test 
public void Testt()  { 
    try { 
     samp=Mockito.mock(Sample.class);     

     pdt=samp.loadpdtType("sampletext"); 
     when(mockSessionFactory. getCurrentSession()).thenReturn(mockSession); 
// mock other method which are called on Session 

     Mockito.verify(samp).loadpdtType("sampletext"); 
     System.out.println("testresult:"+pdt.getPDTType()); 
    } catch(Exception e) { 
     e.printStackTrace(); 
    } 
    } 
} 
+0

謝謝你的回答。我嘗試了會話模擬代碼,但它顯示**方法getSession()未定義類型SessionFactory ** – KarthikaSrinivasan

+0

session = Mockito.mock(Session.class); sessionFactory = Mockito.mock(SessionFactory.class); \t \t \t \t \t \t Mockito.when(sessionFactory.getCurrentSession())。thenReturn(session); – KarthikaSrinivasan

+0

我用上面的代碼來模擬sessionfactory,但它仍然沒有工作 – KarthikaSrinivasan

0

萬歲!謝謝大家,我解決了我自己的問題。我讓它變得更加困難和困難。配置SessionFactory的bean並embeddedWebApplicationContext在MyApp.class的和它的工作...

@RunWith(SpringJUnit4ClassRunner.class)來

@SpringApplicationConfiguration(類= MyApp.class的)

@WebAppConfiguration

@ IntegrationTest( 「server.port:0」)

公共類SampleTester

{
@Autowired

Sample samp; 

@Test 
public void Testt()  
{   
    Pdt pdt=samp.loadpdtType("sampletext"); 
    Resp r=samp.getSeq("ABC","XYZ"); 
// All assertions for Pdt, Resp follows 

} 

}