Skip to content

Commit

Permalink
Add validation to MockMethodDispatcher that this class is only ever l…
Browse files Browse the repository at this point in the history
…oaded by the bootstrap class loader.

This is normally assured by Mockito but other testing frameworks that work with instrumentation can interfer with this. This might be difficult to discover for those frameworks as seen with Robolectric. This explicit error should help to discover such discrepencies.
  • Loading branch information
raphw committed Aug 22, 2020
1 parent 3d1405f commit 42a154f
Showing 1 changed file with 16 additions and 0 deletions.
Expand Up @@ -11,6 +11,22 @@

public abstract class MockMethodDispatcher {

static {
ClassLoader classLoader = MockMethodDispatcher.class.getClassLoader();
if (classLoader != null) {
// Do not use Mockito classes in here as this is executed on the boot loader.
throw new IllegalStateException(
MockMethodDispatcher.class.getName()
+ " is not loaded by the bootstrap class loader but by "
+ classLoader.toString()
+ ".\n\nThis causes the inline mock maker to not work as expected. "
+ "Please contact the maintainer of this class loader implementation "
+ "to assure that this class is never loaded by another class loader. "
+ "The bootstrap class loader must always be queried first for this "
+ "class for Mockito's inline mock maker to function correctly.");
}
}

private static final ConcurrentMap<String, MockMethodDispatcher> DISPATCHERS =
new ConcurrentHashMap<>();

Expand Down

0 comments on commit 42a154f

Please sign in to comment.