1

I am mocking a class with a final method in it, specifically the RevTag class from jGit.

I need to stub the calling of two methods and I do it like this (forgive the goofy names):

package ut.com.isroot.stash.plugin;

import org.eclipse.jgit.lib.PersonIdent;
import org.eclipse.jgit.revwalk.RevTag;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;

import static org.powermock.api.mockito.PowerMockito.mock;
import static org.powermock.api.mockito.PowerMockito.when;

@RunWith(PowerMockRunner.class)
@PrepareForTest
public class TagProcessingStackOverflow {

    @Test
    public void testAnnotatedTagMunging() throws Exception {
        RevTag obj = mock(RevTag.class);
        PersonIdent ident = mock(PersonIdent.class);

        when(obj.getTaggerIdent()).thenReturn(ident);
        when(obj.getFullMessage()).thenReturn("lok'tar ogar \n");
    }
}

The first call to when() causes a NPE, however. Why is it executing the method? The method is a final method, but I thought PowerMockito could handle this.

Here's my relevant pom stuff:

    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.11</version>
        <exclusions>
            <exclusion>
                <artifactId>hamcrest-core</artifactId>
                <groupId>org.hamcrest</groupId>
            </exclusion>
        </exclusions>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.mockito</groupId>
        <artifactId>mockito-all</artifactId>
        <version>1.9.5</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.powermock</groupId>
        <artifactId>powermock-module-junit4</artifactId>
        <version>1.5.2</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.powermock</groupId>
        <artifactId>powermock-api-mockito</artifactId>
        <version>1.5.2</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.hamcrest</groupId>
        <artifactId>hamcrest-all</artifactId>
        <version>1.3</version>
    </dependency>
Scott James Walter
  • 427
  • 1
  • 6
  • 20
  • Maybe that is true; but on the other hand: first assumptions and gut feelings about "root causes" can be dead wrong. It would be better if you would create a minimal viable example and post that; or at least: post your current test, and the production code it is working with. You know, the simply reason for a Stackoverflow Exception is simply an infinite recursion; so, yep, you might also want to post your stack trace. – GhostCat Sep 13 '16 at 17:18
  • I would start by replacing mockito-all and hamcrest-all with their respective -core counterparts. Those dependencies package all of their required dependencies where Maven dependency resolution can't get at them. [Very strange errors](http://stackoverflow.com/questions/7869711/getting-nosuchmethoderror-org-hamcrest-matcher-describemismatch-when-running) can happen with the -all versions. Try swapping them out and see if maybe you get a better error. – user944849 Sep 13 '16 at 17:25
  • @GhostCat I added a full, simplified unit test of what I am experiencing to reproduce my error. – Scott James Walter Sep 13 '16 at 17:44
  • @user944849 I tried swapping those out and re-attempted my newly added simply unit test (above) and I get the same issue. – Scott James Walter Sep 13 '16 at 17:45
  • Why are you preparing a class for test but then immediately mocking it out? In my experience, I prepare a class for test if it has static methods I want to get at, but I don't normally mix the two of them together. – Makoto Sep 13 '16 at 17:45
  • @Makoto if I don't prepare RevTag.class, I get an NPE when I try to stub `getTaggerIdent()` (check the example above). I was under the impression that I had to prepare it, and it didn't really make sense to me why I was getting an NPE while trying to stub a method. – Scott James Walter Sep 13 '16 at 17:48

0 Answers0