Mockito.donothing() 也检查里面的方法?

Mockito.donothing() also check inside the method?

所以,我想在为我的特定方法编写单元测试时调用 donothing() 方法,我对使用 donothing 的理解是“当方法在模拟对象被调用。

但在我的例子中,当我想告诉 mockito 在调用 void 方法时什么都不做,它也会检查写入方法内部的信息。 为什么会发生这种情况,donothing 方法是否也在方法内部进行检查,或者它只是简单地跳过对它的测试。?

例如

这是我要调用的方法 donothing:

 public void deleteTemporaryRemoteBranch(final File gitFile, final String branchName) throws IOException,
            GitAPIException {
        final Git openedRepo = Git.open(gitFile);
        //delete branch 'branchName' locally
        openedRepo.branchDelete().setBranchNames(branchName).setForce(true).call();

        //delete branch 'branchName' on remote 'origin'
        RefSpec refSpec = new RefSpec()
                .setSource(null)
                .setDestination(String.format(BRANCH_HEAD_REF, branchName));
        openedRepo.push().setRefSpecs(refSpec).setRemote(LambdaEnv.ORIGIN).call();
    }

这是测试方法:

private static final String REPO_LOCATION = "/tmp/JGitClientTest12-repo/";
    private static final File REPO_FILE = new File(REPO_LOCATION);

    @Test
    public void clone23_happyCase2() throws Exception {
     Mockito.doNothing().when(jgitAccessor).deleteTemporaryRemoteBranch(Mockito.any(),Mockito.anyString());
     classUnit.deleteTemporaryRemoteBranch(REPO_FILE,Mockito.anyString());
    }

当我运行这个方法时它告诉我错误,

java.lang.IllegalArgumentException: Invalid refspec refs/heads/

    at org.eclipse.jgit.transport.RefSpec.checkValid(RefSpec.java:539)
    at org.eclipse.jgit.transport.RefSpec.setDestination(RefSpec.java:337)

那么为什么这是在 deleteTemporaryRemoteBranch 方法内部测试,即使我在其中调用 donothing()。?

我是新单位testing.Any建议!!

还有关于这个问题的任何建议:

java.lang.IllegalArgumentException: Invalid refspec refs/heads/

    at org.eclipse.jgit.transport.RefSpec.checkValid(RefSpec.java:539)
    at org.eclipse.jgit.transport.RefSpec.setDestination(RefSpec.java:337)

正如您提到的,doNothing 什么都不做,但是在您所举的示例中,您没有使用模拟的 class,您正在调用另一个 class.

你应该改变

   classUnit.deleteTemporaryRemoteBranch(REPO_FILE,Mockito.anyString());

为了

   jgitAccessor.deleteTemporaryRemoteBranch(REPO_FILE,Mockito.anyString());

换句话说,该测试没有多大意义,因为默认情况下模拟方法不做

https://www.javadoc.io/doc/org.mockito/mockito-core/2.8.9/org/mockito/Mockito.html#doNothing()