Greetings,
I've been attempting to mock the static method java.lang.Package.getPackage(String). I've successfully used PowerMock to mock other static methods so I followed the same pattern to mock java.lang.Package.getPackage() and it didn't work. I my unit test, the line PowerMock.verify(Package.class); fails to verify with a message saying "Package.getPackage(<any>): expected: 1, actual: 0". So it's clear it's not getting mocked. When I replace the call to Package.getPackage(String) with a class I specifically created, PackageGetter.getPackage(), this gets mocked successfully. So is there something about Package being in the java.lang package which prevents it from being mocked? NOTE: The Manifest object below is a method I created, not the JVM Manifest object.
@RunWith(PowerMockRunner.class)
@PrepareForTest(Package.class)
public class ManifestTest
{
@Before
public void before() {
MockitoAnnotations.initMocks(this);
}
@Test
public void getImplementationTitle()
{
// PowrMock, mock static
PowerMock.mockStatic(Package.class);
// // Mock class which uses the static
// Package p = Mockito.mock(Package.class);
// Mockito.when(p.getImplementationTitle()).thenReturn("getImplementationTitle");
// Easymock, expect
EasyMock.expect(Package.getPackage(EasyMock.anyString())).andReturn(null);
// PowerMock, replay
PowerMock.replay(Package.class);
// Run test which uses static method
Manifest m = new Manifest(this.getClass());
String s = m.getImplementationTitle();
// PowerMock, verify
PowerMock.verify(Package.class);
// Assert
Assert.assertEquals("getImplementationTitle", s);
}
}