Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save hackugyo/7244152 to your computer and use it in GitHub Desktop.
Save hackugyo/7244152 to your computer and use it in GitHub Desktop.
Mockito 1.9.5 cannot mock package protected classes. refs: http://code.google.com/p/mockito/issues/detail?id=127#c18
public void testMockCreating() {
try {
PublicClass instance = mock(PublicClass.class);
} catch (IllegalAccessError e) {
fail();
}
try {
PublicButInheritedStaticPackageProtectedClass instance;
instance = mock(PublicButInheritedStaticPackageProtectedClass.class);
} catch (IllegalAccessError e) {
fail();
} catch (UnsupportedOperationException e) {
fail();
}
try {
PublicButInheritedPackageProtectedClass instance;
// instance = mock(PublicButInheritedPackageProtectedClass.class);
instance = Mockito.spy(new PublicButInheritedPackageProtectedClass());
Mockito.doReturn(Integer.valueOf(3)).when(instance).getPublicInt();
assertEquals(Integer.valueOf(3), instance.getPublicInt());
Mockito.doReturn(Integer.valueOf(3)).when(instance).getIntProtectedMethod();
assertEquals(Integer.valueOf(3), instance.getIntProtectedMethod());
Mockito.doReturn(Integer.valueOf(3)).when(instance).getIntPackageProtectedMethod();
assertEquals(Integer.valueOf(3), instance.getIntPackageProtectedMethod());
} catch (IllegalAccessError e) {
fail();
} catch (UnsupportedOperationException e) {
fail();
}
try {
StaticPackageProtectedClass instance = mock(StaticPackageProtectedClass.class);
fail();
} catch (IllegalAccessError e) {
fail();
} catch (UnsupportedOperationException e) {
// Static Package Protected class CANNOT be mocked!!
}
try {
PackageProtectedClass instance = mock(PackageProtectedClass.class);
fail();
} catch (IllegalAccessError e) {
fail();
} catch (UnsupportedOperationException e) {
// Package Protected class CANNOT be mocked!!
}
}
public class PublicClass {
// @formatter:off
public Integer getPublicInt() { return 2; }
protected Integer getIntProtectedMethod() { return 2; }
Integer getIntPackageProtectedMethod() { return 2; }
// @formatter:on
}
// @formatter:off
public class PublicButInheritedStaticPackageProtectedClass extends StaticPackageProtectedClass {}
public class PublicButInheritedPackageProtectedClass extends PackageProtectedClass {}
// @formatter:on
static class StaticPackageProtectedClass {
// @formatter:off
public Integer getPublicInt() { return 2; }
protected Integer getIntProtectedMethod() { return 2; }
Integer getIntPackageProtectedMethod() { return 2; }
// @formatter:on
}
class PackageProtectedClass {
// @formatter:off
public Integer getPublicInt() { return 2; }
protected Integer getIntProtectedMethod() { return 2; }
Integer getIntPackageProtectedMethod() { return 2; }
// @formatter:on
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment