Skip to content

Commit

Permalink
Make MockUtil.getMockMaker() public Mockito API
Browse files Browse the repository at this point in the history
The MockitoPlugins interface now provides access to the getMockMaker()
method.

Fixes mockito#3128
  • Loading branch information
AndreasTu committed Oct 5, 2023
1 parent edc6243 commit 35345e1
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import java.util.Set;

import org.mockito.MockMakers;
import org.mockito.internal.util.MockUtil;
import org.mockito.plugins.AnnotationEngine;
import org.mockito.plugins.DoNotMockEnforcer;
import org.mockito.plugins.InstantiatorProvider2;
Expand Down Expand Up @@ -114,4 +115,9 @@ private <T> T create(Class<T> pluginType, String className) {
public MockMaker getInlineMockMaker() {
return create(MockMaker.class, DEFAULT_PLUGINS.get(INLINE_ALIAS));
}

@Override
public MockMaker getMockMaker(String mockMaker) {
return MockUtil.getMockMaker(mockMaker);
}
}
2 changes: 1 addition & 1 deletion src/main/java/org/mockito/internal/util/MockUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public class MockUtil {

private MockUtil() {}

private static MockMaker getMockMaker(String mockMaker) {
public static MockMaker getMockMaker(String mockMaker) {
if (mockMaker == null) {
return defaultMockMaker;
}
Expand Down
15 changes: 15 additions & 0 deletions src/main/java/org/mockito/plugins/MockitoPlugins.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,19 @@ public interface MockitoPlugins {
* @since 2.10.0
*/
MockMaker getInlineMockMaker();

/**
* Returns {@link MockMaker} instance used by Mockito with the passed name {@code mockMaker}.
*
* <p>This will return the instance used by Mockito itself, not a new instance of it.
*
* <p>This method can be used to increase the interop of mocks created by Mockito and other
* libraries using Mockito mock maker API.
*
* @param mockMaker the name of the mock maker or {@code null} to retrieve the default mock maker
* @return instance of the mock maker
* @throws IllegalStateException if a mock maker with the name is not found
* @since 5.6.0
*/
MockMaker getMockMaker(String mockMaker);
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
import static org.mockito.internal.configuration.plugins.DefaultMockitoPlugins.PROXY_ALIAS;
import static org.mockito.internal.configuration.plugins.DefaultMockitoPlugins.SUBCLASS_ALIAS;

import org.assertj.core.api.Assertions;
import org.junit.Test;
import org.mockito.MockMakers;
import org.mockito.internal.creation.bytebuddy.InlineByteBuddyMockMaker;
import org.mockito.internal.util.ConsoleMockitoLogger;
import org.mockito.plugins.InstantiatorProvider2;
Expand Down Expand Up @@ -41,4 +43,17 @@ public void provides_plugins() throws Exception {
ConsoleMockitoLogger.class,
plugins.getDefaultPlugin(MockitoLogger.class).getClass());
}

@Test
public void test_getMockMaker() {
assertNotNull(plugins.getMockMaker(null));
assertTrue(plugins.getMockMaker(MockMakers.INLINE) instanceof InlineByteBuddyMockMaker);

Assertions.assertThatThrownBy(
() -> {
plugins.getMockMaker("non existing");
})
.isInstanceOf(IllegalStateException.class)
.hasMessage("Failed to load MockMaker: non existing");
}
}

0 comments on commit 35345e1

Please sign in to comment.