From 42a154f235c245740b8292e01782b009843e0090 Mon Sep 17 00:00:00 2001 From: Rafael Winterhalter Date: Sat, 22 Aug 2020 21:45:11 +0200 Subject: [PATCH 1/2] Add validation to MockMethodDispatcher that this class is only ever loaded 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. --- .../bytebuddy/inject/MockMethodDispatcher.java | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/main/java/org/mockito/internal/creation/bytebuddy/inject/MockMethodDispatcher.java b/src/main/java/org/mockito/internal/creation/bytebuddy/inject/MockMethodDispatcher.java index 3be8501574..8bc489e7dd 100644 --- a/src/main/java/org/mockito/internal/creation/bytebuddy/inject/MockMethodDispatcher.java +++ b/src/main/java/org/mockito/internal/creation/bytebuddy/inject/MockMethodDispatcher.java @@ -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 DISPATCHERS = new ConcurrentHashMap<>(); From c5406ae5c0c5915ffd02e49a916a261fe7d44d0c Mon Sep 17 00:00:00 2001 From: Rafael Winterhalter Date: Sat, 22 Aug 2020 21:46:10 +0200 Subject: [PATCH 2/2] Add validation to MockMethodDispatcher that this class is only ever loaded 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. --- .../creation/bytebuddy/inject/MockMethodDispatcher.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/mockito/internal/creation/bytebuddy/inject/MockMethodDispatcher.java b/src/main/java/org/mockito/internal/creation/bytebuddy/inject/MockMethodDispatcher.java index 8bc489e7dd..a6784d36e1 100644 --- a/src/main/java/org/mockito/internal/creation/bytebuddy/inject/MockMethodDispatcher.java +++ b/src/main/java/org/mockito/internal/creation/bytebuddy/inject/MockMethodDispatcher.java @@ -17,8 +17,8 @@ public abstract class MockMethodDispatcher { // 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() + + " is not loaded by the bootstrap class loader but by an instance of " + + classLoader.getClass().getName() + ".\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. "