I have a this piece of code using Jgit that I want to write the Unit test.
public void pushToMainline(final File gitFile, String commitID, final String repoUri)
throws IOException, GitAPIException {
final Git openedRepo = Git.open(gitFile);
openedRepo.checkout().setName(Constant.MAINLINE).call();
//Change String CommitId to RevCommit type
Repository repository = openedRepo.getRepository();
RevWalk revwalk = new RevWalk(repository);
ObjectId commitId = repository.resolve(commitID);
RevCommit commit = revwalk.parseCommit(commitId);
openedRepo.cherryPick().include(commit).call();
openedRepo.push()
.setRemote(repoUri)
.add(Constant.MAINLINE)
.setForce(true)
.call();
}
And this is the test class
public static final String TEST_COMMIT_ID = RandomStringUtils.randomAlphabetic(40);
@Test
public void pushToMainline_happyCase() throws Exception {
Git git = Git.open(REPO_FILE);
git.branchCreate().setName("refs/heads/z342ddfd").call();
System.out.println("Test is221");
git.add().addFilepattern(".").call();
RevCommit fixingA = git.commit().setMessage("fixed a").call();
git.checkout().setName("refs/heads/mainline").call();
System.out.println("Test is222");
CherryPickResult pickResult = git.cherryPick().include(fixingA)
.setNoCommit(false).call();
System.out.println("Test is223");
Assert.assertEquals(CherryPickResult.CherryPickStatus.OK, pickResult.getStatus());
when(mockGit.push()).thenReturn(mockPushCommand);
when(mockPushCommand.call()).thenReturn(mockPushResult);
System.out.println("Test is224");
jgitAccessor.pushToMainline(REPO_FILE, TEST_COMMIT_ID, REPO_LOCATION);
System.out.println("Test is221");
}
But When I try to run the above test, getting the NullPointerException in this line RevCommit commit = revwalk.parseCommit(commitId);
i.e present in the above pushToMainline()
method.
This is the small stackTrace:
Test is221
Test is222
Test is223
Test is224
java.lang.NullPointerException
at org.eclipse.jgit.lib.ObjectIdOwnerMap.get(ObjectIdOwnerMap.java:131)
at org.eclipse.jgit.revwalk.RevWalk.parseAny(RevWalk.java:857)
at org.eclipse.jgit.revwalk.RevWalk.parseCommit(RevWalk.java:772)
Any Suggestion Why I am getting this exception.!