2016-09-15 131 views
1

我正在爲通過接口在內部調用另一個方法的方法編寫JUnit測試用例。我使用Mockito來模擬界面,但由於某種原因它給了我NPE。我調試通過,但無法得到任何線索來解決它。 getAllVendors()方法拋出通過接口傳入的異常。使用Mockito測試接口方法

MUT

public void prepare() throws AccountServiceException, ManagerException { 
vendors = getVendorManager().getAllVendors(); 

microsites = new ArrayList<VendorMicrositeTO>(); 
microsites.add(new VendorMicrositeTO("http://www.docusign.com", "docuSign")); 

clientUser = createClientUserObject(); 

} 

的JUnit

@Test 
public void testPrepare() throws Exception { 
    AccountAction accountAction = new AccountAction(); 
    Map<String, Object> actionMap = new HashMap<>(); 
    actionMap.put("application", "ESignatureIntegrationAction"); 

    ActionContext.setContext(new ActionContext(actionMap)); 
    String beanName = Constants.VENDOR_MANAGER_SPRING_BEAN; 
    PowerMockito.mockStatic(AppContext.class); 
    PowerMockito.when(AppContext.containsBean(beanName)).thenReturn(true); 

    IVendorDto iVendorDto = new VendorDto(); 
    iVendorDto.setActive(true); 
    iVendorDto.setCreatedBy("9/15/2016"); 
    iVendorDto.setName("CorpESignClientUser"); 
    iVendorDto.setCreatedBy("SYSTEM"); 

    List<IVendorDto> vendorList = new ArrayList<>(); 
    vendorList.add(iVendorDto); 

    IVendorManager iManager = Mockito.mock((IVendorManager.class)); 
    Mockito.when(iManager.getAllVendors()).thenReturn(vendorList); 

    accountAction.setVendors(vendorList); 
    accountAction.prepare(); 
} 

堆棧跟蹤

java.lang.NullPointerException 
at com.mercuryinsurance.esignature.ui.webapp.action.AccountAction.prepare(AccountAction.java:65) 
at test.com.mercuryinsurance.esignature.ui.webapp.action.TestAccountAction.testPrepare(TestAccountAction.java:58) 
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) 
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
at java.lang.reflect.Method.invoke(Method.java:606) 
at org.junit.internal.runners.TestMethod.invoke(TestMethod.java:66) 
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.runTestMethod(PowerMockJUnit44RunnerDelegateImpl.java:310) 
at org.junit.internal.runners.MethodRoadie$2.run(MethodRoadie.java:86) 
at org.junit.internal.runners.MethodRoadie.runBeforesThenTestThenAfters(MethodRoadie.java:94) 
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.executeTest(PowerMockJUnit44RunnerDelegateImpl.java:294) 
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl$PowerMockJUnit47MethodRunner.executeTestInSuper(PowerMockJUnit47RunnerDelegateImpl.java:127) 
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl$PowerMockJUnit47MethodRunner.executeTest(PowerMockJUnit47RunnerDelegateImpl.java:82) 
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.runBeforesThenTestThenAfters(PowerMockJUnit44RunnerDelegateImpl.java:282) 
at org.junit.internal.runners.MethodRoadie.runTest(MethodRoadie.java:84) 
at org.junit.internal.runners.MethodRoadie.run(MethodRoadie.java:49) 
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.invokeTestMethod(PowerMockJUnit44RunnerDelegateImpl.java:207) 
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.runMethods(PowerMockJUnit44RunnerDelegateImpl.java:146) 
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$1.run(PowerMockJUnit44RunnerDelegateImpl.java:120) 
at org.junit.internal.runners.ClassRoadie.runUnprotected(ClassRoadie.java:34) 
at org.junit.internal.runners.ClassRoadie.runProtected(ClassRoadie.java:44) 
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.run(PowerMockJUnit44RunnerDelegateImpl.java:122) 
at org.powermock.modules.junit4.common.internal.impl.JUnit4TestSuiteChunkerImpl.run(JUnit4TestSuiteChunkerImpl.java:106) 
at org.powermock.modules.junit4.common.internal.impl.AbstractCommonPowerMockRunner.run(AbstractCommonPowerMockRunner.java:53) 
at org.powermock.modules.junit4.PowerMockRunner.run(PowerMockRunner.java:59) 
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50) 
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) 
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459) 
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675) 
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382) 
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192) 

感謝,提前

回答

1

看來你忘了在您的測試中添加一行:

accountAction.setVendorManager(iManager); 
+0

對不起,但沒有安裝人員的IManager。 – Jaykumar

+0

@Jaykumar:本着解決方案的精神,這是正確的。不知何故,您需要提供'getVendorManager'將提供的值,否則它將愉快地返回'null'。 – Makoto

+0

我得到的vendorManager和調試時經歷getVendorManager,但由於某種原因,它不進入getAllVendors() – Jaykumar

0

你的問題基本上是你寫的代碼很難測試。 首先,要了解如何提高您在該地區的技能,請啓動here並花一些時間觀看視頻。沒有大片,但仍值得你花時間。

你會在那裏學習的關鍵點:你必須小心你的代碼使用的對象來自何處。含義:也許你的代碼不應該調用getVendorManager()...但是用注入這個類的對象呢?因爲那樣你可以很容易地嘲笑。

+0

我會遵循指導原則,但我現在應該做什麼。我無法停止測試,也無法更改代碼。 – Jaykumar

+0

我想你會得到其他答案背後的人給你一個工作解決方案;-) – GhostCat

+0

正確,你不是那個幸運的人:) – Jaykumar