Java Mockito 卡在单例方法的 doReturn 上

Java Mockito stuck at doReturn of singleton method

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

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:

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 要求输入时,我的测试可以 "fake" 该输入。

编辑: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(ClassTestDescriptor.java:228)
      at org.junit.jupiter.engine.descriptor.ClassTestDescriptor$$Lambda2.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(HierarchicalTestExecutor.java:80)
      at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor$$Lambda4.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(HierarchicalTestExecutor.java:92)
      at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor$$Lambda7.1353170030.accept(Unknown Source:-1)
      at java.util.stream.ForEachOps$ForEachOp$OfRef.accept(ForEachOps.java:184)
      at java.util.stream.ReferencePipeline.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(HierarchicalTestExecutor.java:92)
      at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor$$Lambda4.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 但是如果读取不是由测试而不是底层逻辑完成的,我如何确保它以正确的顺序传递击键?

看来你在嘲笑错误的东西。你想玩 ConsoleReader 的 returns。所以有几个选项:


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

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 字段修改为 mock。例如

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),这只是一项要求。你总是可以使用一些帮助库来弄乱这个领域,即使它是私有的。 spring 和 apache-commons-lang3 都提供这种实用程序。


使用 Powermock fiddle 和 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 class 以将 ConsoleReader 作为构造函数参数,并简单地提供上述所有方法中的 mockCR (不要 private)。