Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make sure interface types are initialized before inline mocking to avoid blocking code of static initializers. #2505

Merged
merged 1 commit into from Dec 12, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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,44 @@
/*
* Copyright (c) 2017 Mockito contributors
* This program is made available under the terms of the MIT License.
*/
package org.mockitoinline;

import org.junit.Test;

import static org.mockito.Mockito.mock;

public class HierarchyPreInitializationTest {

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

public interface TestInterface {

@SuppressWarnings("unused")
MyClass INSTANCE = 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;
}
}
}