簡體   English   中英

單元測試對於在 function 中使用 this 關鍵字的方法

[英]Unit Test For methods using this this keyword in function

我想知道如何為涉及 this 關鍵字的方法編寫單元測試用例。

DispenseRuleLogic 包含這兩個方法,其中一個使用此關鍵字調用另一個 function。 所以我想知道如何為調用另一個內置方法的行創建存根。
我已經包含了代碼、測試用例和錯誤。 Class 名稱:DispenseRuleLogic


這是我要為其編寫測試用例的方法。

@Test
public void testGetAccessRuleSelect() throws Exception {
    // given
    boolean expected = true;
    Vector<AllocationCode> allocationCodeList = new Vector<>();
    AllocationCode allocationCodeMock = mock(AllocationCode.class);
    allocationCodeMock.setCompanyId(comId);
    allocationCodeList.add(allocationCodeMock);

    DispenseRuleLogic dispenseRuleLogic = new DispenseRuleLogic(dao);
    PowerMockito.whenNew(DispenseRuleLogic.class).withAnyArguments().thenReturn(dispenseRuleLogic);
    PowerMockito.when(DispenseRuleLogic.class, "getAllocationAccessRulePin", any(AllocationAccessRule.class)).thenReturn(true);
    //DispenseRuleLogic is the name of the class for which case is being written 
    
    // when
    boolean actual_result = dispenseRuleLogic.getAccessRuleSelect(comId);

    // then
    assertEquals(expected, actual_result);
}

我試過的:
 @Test public void testGetAccessRuleSelect() throws Exception { // given boolean expected = true; Vector<AllocationCode> allocationCodeList = new Vector<>(); AllocationCode allocationCodeMock = mock(AllocationCode.class); allocationCodeMock.setCompanyId(comId); allocationCodeList.add(allocationCodeMock); DispenseRuleLogic dispenseRuleLogic = new DispenseRuleLogic(dao); PowerMockito.whenNew(DispenseRuleLogic.class).withAnyArguments().thenReturn(dispenseRuleLogic); PowerMockito.when(DispenseRuleLogic.class, "getAllocationAccessRulePin", any(AllocationAccessRule.class)).thenReturn(true); //DispenseRuleLogic is the name of the class for which case is being written // when boolean actual_result = dispenseRuleLogic.getAccessRuleSelect(comId); // then assertEquals(expected, actual_result); }

我得到的錯誤是:
 java.lang.IllegalArgumentException: object is not an instance of declaring class 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.powermock.reflect.internal.WhiteboxImpl.performMethodInvocation(WhiteboxImpl.java:1899) at org.powermock.reflect.internal.WhiteboxImpl.doInvokeMethod(WhiteboxImpl.java:801) at org.powermock.reflect.internal.WhiteboxImpl.invokeMethod(WhiteboxImpl.java:781) at org.powermock.reflect.Whitebox.invokeMethod(Whitebox.java:466) at org.powermock.api.mockito.PowerMockito.when(PowerMockito.java:474) at apex.web.action.account.DispenseRuleLogicTest.testGetAccessRuleSelect(DispenseRuleLogicTest.java:815) 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.internal.runners.TestMethod.invoke(TestMethod.java:68) at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.runTestMethod(PowerMockJUnit44RunnerDelegateImpl.java:316) at org.junit.internal.runners.MethodRoadie$2.run(MethodRoadie.java:89) at org.junit.internal.runners.MethodRoadie.runBeforesThenTestThenAfters(MethodRoadie.java:97) at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.executeTest(PowerMockJUnit44RunnerDelegateImpl.java:300) at org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl$PowerMockJUnit47MethodRunner.executeTestInSuper(PowerMockJUnit47RunnerDelegateImpl.java:131) at org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl$PowerMockJUnit47MethodRunner.access$100(PowerMockJUnit47RunnerDelegateImpl.java:59) at org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl$PowerMockJUnit47MethodRunner$TestExecutorStatement.evaluate(PowerMockJUnit47RunnerDelegateImpl.java:147) at org.mockito.internal.junit.JUnitRule$1.evaluate(JUnitRule.java:16) at org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl$PowerMockJUnit47MethodRunner.evaluateStatement(PowerMockJUnit47RunnerDelegateImpl.java:107) 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:288) at org.junit.internal.runners.MethodRoadie.runTest(MethodRoadie.java:87) at org.junit.internal.runners.MethodRoadie.run(MethodRoadie.java:50) at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.invokeTestMethod(PowerMockJUnit44RunnerDelegateImpl.java:208) at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.runMethods(PowerMockJUnit44RunnerDelegateImpl.java:147) at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$1.run(PowerMockJUnit44RunnerDelegateImpl.java:121) 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:123) at org.powermock.modules.junit4.common.internal.impl.JUnit4TestSuiteChunkerImpl.run(JUnit4TestSuiteChunkerImpl.java:121) 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:93) at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:40) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:529) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:756) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:452) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:210)

如果評論中提到的重構是不可能的,那么一種替代方法是在測試 class 中為您的DispenseRuleLogic使用spy

@Mock
DispenseRuleLogic dispenseRuleLogic;

然后在你的測試方法中:

 dispenseRuleLogic =spy(DispenseRuleLogic.class);
 ..

when(dispenseRuleLogic.getAllocationCodeList(..)).thenReturn(allocationCodeList); //returning code list.
           when(dispenseRuleLogic.getAllocationAccessRulePin(..)).thenReturn(Booleam.TRUE); // retuning true when test calls this method.
            ..
            dispenseRuleLogic.getAccessRuleSelect(..) //test actual method

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM