使用此关键字在功能中使用此关键字的方法测试

发布于 2025-01-25 18:19:16 字数 6415 浏览 4 评论 0原文

我想知道如何为涉及此关键字的方法编写单元测试用例。

dispenSeruleLogic包含这两种方法,其中一个使用此关键字调用另一个函数。所以我想知道如何为呼叫另一种内置方法的行创建一个存根。
我包括了代码,测试案例和错误。 班级名称:dispenSerulogic


Here is the method for which I want the test case to be written.

public boolean getAccessRuleSelect(String companyId) {
    boolean pinFlag = false;
    AllocationCode ac = new AllocationCode();
    ac.setCompanyId(companyId);
    Vector alCode = this.getAllocationCodeList(ac);
    AllocationAccessRule aar = new AllocationAccessRule();
    for (int i = 0; i < alCode.size(); i++) {
        aar.setAllocationCodeId(((AllocationCode) alCode.get(i))
                .getAllocationCodeId());

   //here this refers to DispenseRuleLogic
        if (this.getAllocationAccessRulePin(aar)) {
            pinFlag = true;
            break;
        }
    }
    return pinFlag;
}

What I have tried:
@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);
}

Error i am getting is:
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)

I wanted to know how to write unit test cases for methods involving this keyword.

DispenseRuleLogic contains these two methods in which one is calling another function using this keyword. 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


Here is the method for which I want the test case to be written.

public boolean getAccessRuleSelect(String companyId) {
    boolean pinFlag = false;
    AllocationCode ac = new AllocationCode();
    ac.setCompanyId(companyId);
    Vector alCode = this.getAllocationCodeList(ac);
    AllocationAccessRule aar = new AllocationAccessRule();
    for (int i = 0; i < alCode.size(); i++) {
        aar.setAllocationCodeId(((AllocationCode) alCode.get(i))
                .getAllocationCodeId());

   //here this refers to DispenseRuleLogic
        if (this.getAllocationAccessRulePin(aar)) {
            pinFlag = true;
            break;
        }
    }
    return pinFlag;
}

What I have tried:

@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);
}

Error i am getting is:

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)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

帅的被狗咬 2025-02-01 18:19:16

如果无法在评论中提到的那样无法进行重构,那么您的spy在您的测试类中使用spy dispenserulelogic :

@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

If refactoring is not possible as mentioned in comments then one alternative is to use spy for your DispenseRuleLogic in your test class:

@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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文