Skip to content

Commit

Permalink
Make sure interface types are initialized before inline mocking to av…
Browse files Browse the repository at this point in the history
…oid blocking code of static initializers.
  • Loading branch information
raphw committed Dec 10, 2021
1 parent dbcbb3f commit 41557b8
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
Expand Up @@ -252,8 +252,8 @@ private <T> void triggerRetransformation(Set<Class<?>> types, boolean flat) {
} else {
do {
if (mocked.add(type)) {
assureInitialization(type);
if (!flatMocked.remove(type)) {
assureInitialization(type);
targets.add(type);
}
addInterfaces(targets, type.getInterfaces());
Expand Down Expand Up @@ -356,6 +356,7 @@ private void addInterfaces(Set<Class<?>> types, Class<?>[] interfaces) {
for (Class<?> type : interfaces) {
if (mocked.add(type)) {
if (!flatMocked.remove(type)) {
assureInitialization(type);
types.add(type);
}
addInterfaces(types, type.getInterfaces());
Expand Down
@@ -0,0 +1,38 @@
package org.mockitoinline;

import org.junit.Test;

import static org.mockito.Mockito.mock;

public class HierarchyPreInitializationTest {

@Test
public void testOrder() {
mock(MyClass.class);
mock(TestSubInterface.class);
}

public interface TestInterface {

MyClass FOO = new MyClass().probe();
}

public interface TestSubInterface extends TestInterface {
}

public static class MyClass {

private final Object obj;

public MyClass() {
obj = new Object();
}

public MyClass probe() {
if (obj == null) {
throw new RuntimeException();
}
return this;
}
}
}

0 comments on commit 41557b8

Please sign in to comment.