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>