简体   繁体   中英

Writing Unit test with Mockito with Jgit

I am new to Mockito/PowerMockito(as it is a static method), while testing my code. I am not able to write test for this method as it contain fileList along with Jgit method, Can anyone show how can I perform testing for this perticular method.

public static String addAndCommitUntrackedChanges(final File gitFile, final String branchName,
                                                      final String commitMessage, List<String> filesList)
            throws IOException, GitAPIException {
        final Git openedRepo = Git.open(gitFile);
        openedRepo.checkout().setCreateBranch(true).setName(branchName).call();

        AddCommand add = openedRepo.add();
       for (final String file: filesList) {
          Path filepath = Paths.get(file); //file absolute Path
          final Path repoBasePath = Paths.get("/", "tmp", "PackageName"); //file base common path
          final Path relative = repoBasePath.relativize(filepath); //Remove the repoBasePath from filpath
           add.addFilepattern(relative.toString());
       }

        add.call();
        // Create a new commit.
        RevCommit commit = openedRepo.commit()
                .setMessage(commitMessage)
                .call();

        //Return the Latest Commit_Id
        return ObjectId.toString(commit.getId());
    }

Thanks in advance !

You should avoid static methods. How are you going to test clients of addAndCommitUntrackedChanges if you can't mock it?

To make addAndCommitUntrackedChanges more testable, introduce a GitWrapper interface:

interface GitWrapper {
  Git open(File f);
}

with an implementation:

class DefaultGitWrapper implements GitWrapper {
  public Git open(File f) {
    return Git.open(f);
  }
}

and change the signature of your method to:

public static String addAndCommitUntrackedChanges(
  GitWrapper gitWrapper,
  final File gitFile,
  final String branchName,
  final String commitMessage,
  List<String> filesList)

and use GitWrapper instead of the static instance of Git .

In the particular case of Git the wrapper isn't needed, because you can just pass an instance of Git , which can be mocked normally, but when you really have a third party class which only provides static methods it's a necessary solution.

Then you can mock the things you need to mock to write a unit test, which would look something like (this is uncompiled code):

class TestAddAndCommitUntrackedChanges {
 @Mock
 GitWrapper gitWrapper;
 @Mock
 Git git;
 @Mock
 CheckoutCommand checkoutCommand;
 @Mock
 AddCommand addCommand;

 @Test
 public void testBehaviour() {
    List<String> files = List.of("/tmp/PackageName/foo", "/tmp/PackageName/bar");
    File gitFile = new File("gitFile");
    when(gitWrapper.open(gitFile)).thenReturn(git);
    when(git.checkout()).thenReturn(checkoutCommand);
    when(checkoutCommand.setName("theBranch"))
      .thenReturn(checkoutCommand);
    when(git.add()).thenReturn(addCommand);
    assertEquals(
      "thecommitid",
      addAndCommitUntrackedChanges(
         gitWrapper, gitFile, "theBranch",
         "the commit message", files)
    );
    verify(checkoutCommand).call();
    verify(addCommand).addFilePattern("foo");
    verify(addCommand).addFilePattern("bar");
    verify(addCommand).call();
 }
}

You'll also need to mock and verify the CommitCommand .

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