[英]Unit Test For methods using this this keyword in function
I wanted to know how to write unit test cases for methods involving this keyword.我想知道如何为涉及 this 关键字的方法编写单元测试用例。
DispenseRuleLogic contains these two methods in which one is calling another function using this keyword. DispenseRuleLogic 包含这两个方法,其中一个使用此关键字调用另一个 function。 So I wanted to know how to create a stub for the line calling another inbuilt method.
所以我想知道如何为调用另一个内置方法的行创建存根。
I have included the code, the test case and the error.我已经包含了代码、测试用例和错误。 Class name: DispenseRuleLogic
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)
If refactoring is not possible as mentioned in comments then one alternative is to use spy
for your DispenseRuleLogic
in your test class:如果评论中提到的重构是不可能的,那么一种替代方法是在测试 class 中为您的
DispenseRuleLogic
使用spy
:
@Mock
DispenseRuleLogic dispenseRuleLogic;
Then in your test method:然后在你的测试方法中:
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.