0

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.!

  • your `revwalk` could be null , try printing its value before `revwalk.parseCommit(commitId);` – sanjeevRm Jul 16 '21 at 06:27
  • Hi, @sanjeevRm, Can't try to printing the value as this is a whole machine that we need to run,Unless and until we have a strong reason for this. Also Its only shows when we try to write the Unit test for this, Otherwise its working fine when I try to run this. – SATYAM MITTAL 17103094 Jul 16 '21 at 06:57
  • When you look at `ObjectIdOwnerMap.java:131` what is causing the NPE? – tgdavies Jul 16 '21 at 10:05
  • hi @tgdavies Nothing much on line :131, some bytecode ` while(this.tblIdx < table.length) { V v = table[this.tblIdx++]; if (v != null) { return this.found(v); } } }` – SATYAM MITTAL 17103094 Jul 18 '21 at 17:20
  • @sanjeevRm, also my whole code is working fine, i.e I am able to convert my string to revcommit type. So seems like revwalk would not be null. – SATYAM MITTAL 17103094 Jul 18 '21 at 17:21
  • So presumably `table` is null. You may have to mock a bit deeper. – tgdavies Jul 18 '21 at 22:07
  • To debug something like this, put a breakpoint where you get the NPE, and then look back up the stack trace to see what other calls (and possibly other objects) you need to mock. – tgdavies Jul 18 '21 at 23:49

0 Answers0