代码之家  ›  专栏  ›  技术社区  ›  MiketheCalamity

Java-单元测试与未来的未来

  •  0
  • MiketheCalamity  · 技术社区  · 6 年前

    在我的代码中,我有以下内容:

    // for the test:
    // executor is an ExecutorService that will run in the current thread
    // compute is an IgniteCompute (can be mocked)
    
    String other = "blah";
    IgniteFuture<String> future = compute.callAsync(callable).chainAsync(i -> myCreate(i, other), executor);
    

    方法 myCreate 是类中的私有方法,我希望确保对其进行单元测试。我试着嘲笑 IgniteCompute 但结果是 callAsync chainAsync 两者都被模拟,导致in-my方法未被调用。关于我如何才能得到真正的 我的创建 在运行上述行的测试中运行的方法?

    2 回复  |  直到 6 年前
        1
  •  2
  •   Denis Mekhanikov    6 年前

    您可以在嵌入式内存模式下开始点火进行测试。要启动节点,请使用 Ignition.start(...) 方法。

    Ignite节点非常轻,启动时不需要太多资源。我认为,它比模拟点火计算更容易、更透明。

        2
  •  0
  •   Tobb    6 年前

    当测试异步内容时,通常必须捕获发送到Async的参数,然后在测试中执行它,然后验证发送到Async的方法的结果。所以像这样:

    final ArgumentCaptor<IgniteClosure> closureCaptor = ArgumentCaptor.forClass(IgniteClosure.class);
    when(computeMock.callAsync(any(IgniteCallable.class))).thenReturn(futureMock);
    when(futureMock.chainAsync(any(IgniteClosure.class), any(Executor.class))).then(invocation -> null);
    
    //invoke method under test
    
    verify(futureMock).chainAsync(closureCaptor.capture(), any(Executor.class));
    
    closureCaptor.getValue().apply(executorMock);
    //verify return value, other invocations, etc.