gpt4 book ai didi

Java Mockito 卡在单例方法的 doReturn 处

转载 作者:太空宇宙 更新时间:2023-11-04 11:01:01 27 4
gpt4 key购买 nike

我有一个单例类来帮助我从控制台读取输入:

public class IOHelper {
public org.slf4j.Logger logger = Logger.logger;

//JLine
public ConsoleReader cr;

private static IOHelper instance;

private IOHelper(){
{
try {
cr = new ConsoleReader();
} catch (IOException e) {
e.printStackTrace();
}
}
}

public static synchronized IOHelper getInstance(){
if (instance == null){
instance = new IOHelper();
}

return instance;
}

我想测试的代码将其引用为:

String in = IOHelper.getInstance().cr.readLine();

然后是我的测试类:

class Test {

private static NetworkCommunicator networkCommunicator;
private static IOHelper ioHelper;

@BeforeAll
static void setUpClass() throws Throwable {

ioHelper = spy(IOHelper.getInstance());
doReturn("1").when(ioHelper).cr.readLine();

networkCommunicator = spy(NetworkCommunicator.class);

doNothing().when(networkCommunicator).connectToServer();
doNothing().when(networkCommunicator).connectToOtherServer();
}

我的测试卡在了 doReturn("1").when(ioHelper).cr.readLine(); 行,就好像它实际执行了 cr.readline(); 部分一样。我的堆栈跟踪指向在 FileInputStream 上找到的方法 private native int read0() throws IOException; 。评论表明,如果没有可用的输入,它会阻塞。我想替换控制台上的方法 readLine(),因此当我的 CLI 要求输入时,我的测试可以“伪造”该输入。

编辑:2个有趣线程的调用堆栈:

"main@1" prio=5 tid=0x1 nid=NA runnable
java.lang.Thread.State: RUNNABLE
blocks NonBlockingInputStreamThread@1437
at java.io.FileInputStream.read0(FileInputStream.java:-1)
at java.io.FileInputStream.read(FileInputStream.java:207)
at jline.internal.NonBlockingInputStream.read(NonBlockingInputStream.java:166)
- locked <0x67d> (a jline.internal.NonBlockingInputStream)
at jline.internal.NonBlockingInputStream.read(NonBlockingInputStream.java:135)
at jline.internal.NonBlockingInputStream.read(NonBlockingInputStream.java:243)
at jline.internal.InputStreamReader.read(InputStreamReader.java:257)
at jline.internal.InputStreamReader.read(InputStreamReader.java:194)
at jline.console.ConsoleReader.readCharacter(ConsoleReader.java:2147)
at jline.console.ConsoleReader.readCharacter(ConsoleReader.java:2137)
at jline.console.ConsoleReader.readBinding(ConsoleReader.java:2222)
at jline.console.ConsoleReader.readLine(ConsoleReader.java:2463)
at jline.console.ConsoleReader.readLine(ConsoleReader.java:2374)
at jline.console.ConsoleReader.readLine(ConsoleReader.java:2362)
at jline.console.ConsoleReader.readLine(ConsoleReader.java:2350)
at com.mypkg.Test.setUpClass(Test.java:43)
at sun.reflect.NativeMethodAccessorImpl.invoke0(NativeMethodAccessorImpl.java:-1)
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.platform.commons.util.ReflectionUtils.invokeMethod(ReflectionUtils.java:389)
at org.junit.jupiter.engine.execution.ExecutableInvoker.invoke(ExecutableInvoker.java:115)
at org.junit.jupiter.engine.descriptor.ClassTestDescriptor.lambda$invokeBeforeAllMethods$5(ClassTestDescriptor.java:228)
at org.junit.jupiter.engine.descriptor.ClassTestDescriptor$$Lambda$162.715378067.execute(Unknown Source:-1)
at org.junit.jupiter.engine.execution.ThrowableCollector.execute(ThrowableCollector.java:40)
at org.junit.jupiter.engine.descriptor.ClassTestDescriptor.invokeBeforeAllMethods(ClassTestDescriptor.java:227)
at org.junit.jupiter.engine.descriptor.ClassTestDescriptor.before(ClassTestDescriptor.java:151)
at org.junit.jupiter.engine.descriptor.ClassTestDescriptor.before(ClassTestDescriptor.java:61)
at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor.lambda$execute$3(HierarchicalTestExecutor.java:80)
at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor$$Lambda$134.398690014.execute(Unknown Source:-1)
at org.junit.platform.engine.support.hierarchical.SingleTestExecutor.executeSafely(SingleTestExecutor.java:66)
at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor.execute(HierarchicalTestExecutor.java:77)
at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor.lambda$null$2(HierarchicalTestExecutor.java:92)
at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor$$Lambda$137.1353170030.accept(Unknown Source:-1)
at java.util.stream.ForEachOps$ForEachOp$OfRef.accept(ForEachOps.java:184)
at java.util.stream.ReferencePipeline$2$1.accept(ReferencePipeline.java:175)
at java.util.Iterator.forEachRemaining(Iterator.java:116)
at java.util.Spliterators$IteratorSpliterator.forEachRemaining(Spliterators.java:1801)
at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:481)
at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:471)
at java.util.stream.ForEachOps$ForEachOp.evaluateSequential(ForEachOps.java:151)
at java.util.stream.ForEachOps$ForEachOp$OfRef.evaluateSequential(ForEachOps.java:174)
at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
at java.util.stream.ReferencePipeline.forEach(ReferencePipeline.java:418)
at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor.lambda$execute$3(HierarchicalTestExecutor.java:92)
at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor$$Lambda$134.398690014.execute(Unknown Source:-1)
at org.junit.platform.engine.support.hierarchical.SingleTestExecutor.executeSafely(SingleTestExecutor.java:66)
at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor.execute(HierarchicalTestExecutor.java:77)
at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor.execute(HierarchicalTestExecutor.java:51)
at org.junit.platform.engine.support.hierarchical.HierarchicalTestEngine.execute(HierarchicalTestEngine.java:43)
at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:170)
at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:154)
at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:90)
at com.intellij.junit5.JUnit5IdeaTestRunner.startRunnerWithArgs(JUnit5IdeaTestRunner.java:62)
at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)

"NonBlockingInputStreamThread@1437" daemon prio=5 tid=0xf nid=NA waiting
java.lang.Thread.State: WAITING
waiting for main@1 to release lock on <0x67d> (a jline.internal.NonBlockingInputStream)
at java.lang.Object.wait(Object.java:-1)
at jline.internal.NonBlockingInputStream.run(NonBlockingInputStream.java:275)
at java.lang.Thread.run(Thread.java:745)

扩展这个问题:我有一些方法要求用户进行多个输入(例如更新一些设置)。我是否正确地认为最好的方法是将设置重构为采用参数并仅测试这个新方法的方法?有没有一种解决方案,当任何方法尝试从 ConsoleReader 读取时,我可以将一系列字符串传递给测试以按下?我想过使用 Robot 但如果读取不是由测试而不是底层逻辑完成,我如何确保它以正确的顺序传递击键?

最佳答案

看来你在 mock 错误的事情。您想要使用 ConsoleReader 的返回值。所以有几个选择:

<小时/>

在您的 IOHelper 类上使用一些 getConsoleReader() 方法,然后您可以模拟该方法 - 您需要确保 IOHelper 类也通过此方法访问它。例如

private final IOHelper mySpy = spy(IOHelper.getInstance());

@Before
public void setup() {
final ConsoleReader mockCR = mock(ConsoleReader.class);
// Any mockery on your mockCR you need.
// doReturn(...).when(mockCR).readLine();, etc.
doReturn(mockCR).when(mySpy).getConsoleReader();
}
<小时/>

将 cr 字段修改为模拟字段。例如

private final IOHelper ioHelper= IOHelper.getInstance();

@Before
public void setup() {
final ConsoleReader mockCR = mock(ConsoleReader.class);
// Any mockery on your mockCR you need.
// doReturn(...).when(mockCR).readLine();, etc.
ioHelper.cr = mockCR;
}

尽管如此,我还是对这种情况提出警告;我看不出有什么理由让您的 ConsoleReader 成为 public (或不是 final),而这只是一个要求。即使它是私有(private)的,您始终可以使用一些辅助库来扰乱该字段。 spring 和 apache-commons-lang3 都提供这种类型的实用程序。

<小时/>

使用Powermock来摆弄ConsoleReader的构造函数:

@RunWith(PowerMockRunner.class)
@PrepareForTest(IOHelper.class)
public class IOHelperTest {
@BeforeClass
public static void setup() {
final ConsoleReader mockCR = mock(ConsoleReader.class);
// Any mockery on your mockCR you need.
// doReturn(...).when(mockCR).readLine();, etc.

PowerMock.whenNew(ConsoleReader.class).thenReturn(mockCR);
}
}
<小时/>

最后,您可以修改您的 IOHelper 类,以将 ConsoleReader 作为构造函数参数,并简单地提供上述所有方法中的 mockCR(并使其不是 private)。

关于Java Mockito 卡在单例方法的 doReturn 处,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46889859/

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