简体   繁体   中英

Mockito.donothing() also check inside the method?

So,I want to call the donothing() method while writing the unit test for my perticular method, what I understand of using donothing is that "it tells Mockito to do nothing when a method in a mock object is called".

But in my case when I want to tells mockito to do nothing while calling the void method, it also checks the information that written inside the method too. Why this is happen, Is donothing method also checks inside the method or it just simply skip the testing for it.?

Eg

this is the method in that I want to call 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();
    }

This is the test method :

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());
    }

When I run this method it show me the error,

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)

So why this is testing inside the deleteTemporaryRemoteBranch method, even I call donothing() in it.?

I am new to unit testing.Any suggestion!!

Also any suggestion for this issue:

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)

As you mentioned, doNothing does nothing, but in the example that you have put you are not using the mocked class, you are calling another class.

You should change

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

for

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

In other way, that test does not make much sense since by default the mocked methods do not do

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM