gpt4 book ai didi

java - 内部类无法访问模拟的外部类方法

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:46:54 24 4
gpt4 key购买 nike

下面是要测试的示例类:

public class TestOuterClass {
private final InnerClass innerObj;

boolean status;
Object nullObj = null;

public TestOuterClass(boolean status) {
this.innerObj = new InnerClass();
this.status = status;
}

public class InnerClass {
public boolean doInner() {
return outerMethod();
}
}

public void mockOuterMethod() {
if (innerObj.doInner()) {
return;
}
throw new RuntimeException();
}

boolean outerMethod() {
nullObj.toString();
return status;
}
}

下面是示例测试代码:

@RunWith(PowerMockRunner.class)
@PrepareForTest(TestOuterClass.class)
public class TestMockOuterClass {
@Test
public void testMockOuterMethod() {
TestOuterClass outerObj = spy(new TestOuterClass(false));
doReturn(true).when(outerObj).outerMethod();
outerObj.mockOuterMethod();
}

@Test
public void testOuterMethod() {
TestOuterClass outerObj = spy(new TestOuterClass(false));
doReturn(true).when(outerObj).outerMethod();
assertTrue(outerObj.outerMethod());
}
}

请注意,第一个测试失败并出现以下异常堆栈,第二个测试成功:

java.lang.NullPointerException
at org.apache.samza.runtime.TestOuterClass.outerMethod(TestOuterClass.java:31)
at org.apache.samza.runtime.TestOuterClass$InnerClass.doInner(TestOuterClass.java:19)
at org.apache.samza.runtime.TestOuterClass.mockOuterMethod(TestOuterClass.java:24)
at org.apache.samza.runtime.TestMockOuterClass.testMockOuterMethod(TestMockOuterClass.java:22)
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.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.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:51)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)

似乎当我为类 TestOuterClass 创建一个 spy 对象并直接模拟和测试 outerMethod 时,它起作用了。但是,当从 InnerClass.doInner() 方法触发 outerMethod 时,模拟不起作用。修复第一个测试的推荐方法是什么?

最佳答案

让我们逐点分析这个例子。

在测试用例中,您围绕 TestOuterClass 的一个实例创建了一个 spy 。在创建实例(在构造函数中)时,您创建了一个 InnerClass 的对象作为其字段。

现在您模拟 outerMethod 的行为,使其始终返回 true

现在,当 outerObj.mockOuterMethod() 被调用时发生的是 real 方法被调用,因为它没有被模拟。您可以在您提供的堆栈跟踪中看到它:

at org.apache.samza.runtime.TestOuterClass.outerMethod(TestOuterClass.java:31)
at org.apache.samza.runtime.TestOuterClass$InnerClass.doInner(TestOuterClass.java:19)
at org.apache.samza.runtime.TestOuterClass.mockOuterMethod(TestOuterClass.java:24)

稍后,它继续调用真正的 doInner 方法 - 这里可能会让人感到困惑 - 真正的 outerMethod

您可能会说:“嘿,我 mock 了那个东西”。但是你看 - 在这个方法中:

public boolean doInner() {
return outerMethod();
}

您实际上指的是 this.outherMethod()。问题是,从 InnerClass 的角度来看,this 指的是实际的 TestOuterClass 实例 - 不是 spy 。

当您创建一个实例的 spy 时,就会创建一种代理。当您调用一个被模拟的方法时,甚至不会到达真正的对象。 但是当您调用未被模拟的方法时 - 它会“代理”对真实对象的调用。

所以您的问题是:如何解决?

您可以添加 TestOuterClass 的另一个构造函数,而不是创建 InnerClass 的实例,而是注入(inject)它:

public TestOuterClass(boolean status, InnerClass innerObj) {
this.innerObj = innerObj;
this.status = status;
}

然后,在测试中,您可以创建将 InnerClass 的模拟传递给构造函数:

TestOuterClass.InnerClass innerObjMock = Mockito.mock(TestOuterClass.InnerClass.class);
TestOuterClass outerObj = spy(new TestOuterClass(false, innerObjMock));

使用此设置,您可以模拟 innerObjMock 的行为。

如果这不是您要找的东西,请告诉我。

关于java - 内部类无法访问模拟的外部类方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51773084/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com