Skip to content

Commit 65ee9e9

Browse files
committed
Feat: automatically detect class to mock in mockStatic and mockConstruction
As PR mockito#2779 introduced the ability to automatically detect the class to mock for mocks and spies, this commit allows doing the same with mockStatic and mockConstruction. Added overloaded reified methods to match all the counterparts that consume an explicit Class literal parameter. For instance, users can now leverage these: * MockedStatic<SomeClass> mock = mockStatic(); * MockedConstruction<SomeClass> mock = mockConstruction(); instead of: * MockedStatic<SomeClass> mock = mockStatic(SomeClass.class); * MockedConstruction<SomeClass> mock = mockConstruction(SomeClass.class); The following methods have been added: * mockStatic(T...) * mockStatic(Answer, T...) * mockStatic(String, T...) * mockStatic(MockSettings, T...) * mockConstruction(T...) * mockConstruction(MockInitializer, T...) * mockConstruction(MockSettings, T...) * mockConstruction(Function<Context, MockSettings>, T...) * mockConstruction(MockSettings, MockInitializer, T...) * mockConstruction(Function<Context, MockSettings>, MockInitializer, T...)
1 parent 3a1a19e commit 65ee9e9

File tree

2 files changed

+421
-0
lines changed

2 files changed

+421
-0
lines changed

mockito-core/src/main/java/org/mockito/Mockito.java

Lines changed: 224 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2422,6 +2422,107 @@ public static <T> MockedStatic<T> mockStatic(Class<T> classToMock, MockSettings
24222422
return MOCKITO_CORE.mockStatic(classToMock, mockSettings);
24232423
}
24242424

2425+
/**
2426+
* Creates a thread-local mock controller for all static methods of the given class or interface.
2427+
* The returned object's {@link MockedStatic#close()} method must be called upon completing the
2428+
* test or the mock will remain active on the current thread.
2429+
* <p>
2430+
* <b>Note</b>: We recommend against mocking static methods of classes in the standard library or
2431+
* classes used by custom class loaders used to execute the block with the mocked class. A mock
2432+
* maker might forbid mocking static methods of know classes that are known to cause problems.
2433+
* Also, if a static method is a JVM-intrinsic, it cannot typically be mocked even if not
2434+
* explicitly forbidden.
2435+
* <p>
2436+
* See examples in javadoc for {@link Mockito} class
2437+
*
2438+
* @param reified don't pass any values to it. It's a trick to detect the class/interface you
2439+
* want to mock.
2440+
* @return mock controller
2441+
* @since 5.21.0
2442+
*/
2443+
@SafeVarargs
2444+
public static <T> MockedStatic<T> mockStatic(T... reified) {
2445+
return mockStatic(withSettings(), reified);
2446+
}
2447+
2448+
/**
2449+
* Creates a thread-local mock controller for all static methods of the given class or interface.
2450+
* The returned object's {@link MockedStatic#close()} method must be called upon completing the
2451+
* test or the mock will remain active on the current thread.
2452+
* <p>
2453+
* <b>Note</b>: We recommend against mocking static methods of classes in the standard library or
2454+
* classes used by custom class loaders used to execute the block with the mocked class. A mock
2455+
* maker might forbid mocking static methods of know classes that are known to cause problems.
2456+
* Also, if a static method is a JVM-intrinsic, it cannot typically be mocked even if not
2457+
* explicitly forbidden.
2458+
* <p>
2459+
* See examples in javadoc for {@link Mockito} class
2460+
*
2461+
* @param defaultAnswer the default answer when invoking static methods.
2462+
* @param reified don't pass any values to it. It's a trick to detect the class/interface you
2463+
* want to mock.
2464+
* @return mock controller
2465+
* @since 5.21.0
2466+
*/
2467+
@SafeVarargs
2468+
public static <T> MockedStatic<T> mockStatic(
2469+
@SuppressWarnings("rawtypes") Answer defaultAnswer, T... reified) {
2470+
return mockStatic(withSettings().defaultAnswer(defaultAnswer), reified);
2471+
}
2472+
2473+
/**
2474+
* Creates a thread-local mock controller for all static methods of the given class or interface.
2475+
* The returned object's {@link MockedStatic#close()} method must be called upon completing the
2476+
* test or the mock will remain active on the current thread.
2477+
* <p>
2478+
* <b>Note</b>: We recommend against mocking static methods of classes in the standard library or
2479+
* classes used by custom class loaders used to execute the block with the mocked class. A mock
2480+
* maker might forbid mocking static methods of know classes that are known to cause problems.
2481+
* Also, if a static method is a JVM-intrinsic, it cannot typically be mocked even if not
2482+
* explicitly forbidden.
2483+
* <p>
2484+
* See examples in javadoc for {@link Mockito} class
2485+
*
2486+
* @param name the name of the mock to use in error messages.
2487+
* @param reified don't pass any values to it. It's a trick to detect the class/interface you
2488+
* want to mock.
2489+
* @return mock controller
2490+
* @since 5.21.0
2491+
*/
2492+
@SafeVarargs
2493+
public static <T> MockedStatic<T> mockStatic(String name, T... reified) {
2494+
return mockStatic(withSettings().name(name).defaultAnswer(RETURNS_DEFAULTS), reified);
2495+
}
2496+
2497+
/**
2498+
* Creates a thread-local mock controller for all static methods of the given class or interface.
2499+
* The returned object's {@link MockedStatic#close()} method must be called upon completing the
2500+
* test or the mock will remain active on the current thread.
2501+
* <p>
2502+
* <b>Note</b>: We recommend against mocking static methods of classes in the standard library or
2503+
* classes used by custom class loaders used to execute the block with the mocked class. A mock
2504+
* maker might forbid mocking static methods of know classes that are known to cause problems.
2505+
* Also, if a static method is a JVM-intrinsic, it cannot typically be mocked even if not
2506+
* explicitly forbidden.
2507+
* <p>
2508+
* See examples in javadoc for {@link Mockito} class
2509+
*
2510+
* @param mockSettings the settings to use where only name and default answer are considered.
2511+
* @param reified don't pass any values to it. It's a trick to detect the class/interface you
2512+
* want to mock.
2513+
* @return mock controller
2514+
* @since 5.21.0
2515+
*/
2516+
@SafeVarargs
2517+
public static <T> MockedStatic<T> mockStatic(MockSettings mockSettings, T... reified) {
2518+
if (reified == null || reified.length > 0) {
2519+
throw new IllegalArgumentException(
2520+
"Please don't pass any values here. Java will detect class automagically.");
2521+
}
2522+
2523+
return mockStatic(getClassOf(reified), mockSettings);
2524+
}
2525+
24252526
/**
24262527
* Creates a thread-local mock controller for all constructions of the given class.
24272528
* The returned object's {@link MockedConstruction#close()} method must be called upon completing the
@@ -2554,6 +2655,129 @@ public static <T> MockedConstruction<T> mockConstruction(
25542655
return MOCKITO_CORE.mockConstruction(classToMock, mockSettingsFactory, mockInitializer);
25552656
}
25562657

2658+
/**
2659+
* Creates a thread-local mock controller for all constructions of the given class.
2660+
* The returned object's {@link MockedConstruction#close()} method must be called upon completing the
2661+
* test or the mock will remain active on the current thread.
2662+
* <p>
2663+
* See examples in javadoc for {@link Mockito} class
2664+
*
2665+
* @param reified don't pass any values to it. It's a trick to detect the class/interface you
2666+
* want to mock.
2667+
* @return mock controller
2668+
* @since 5.21.0
2669+
*/
2670+
@SafeVarargs
2671+
public static <T> MockedConstruction<T> mockConstruction(T... reified) {
2672+
return mockConstruction(index -> withSettings(), (mock, context) -> {}, reified);
2673+
}
2674+
2675+
/**
2676+
* Creates a thread-local mock controller for all constructions of the given class.
2677+
* The returned object's {@link MockedConstruction#close()} method must be called upon completing the
2678+
* test or the mock will remain active on the current thread.
2679+
* <p>
2680+
* See examples in javadoc for {@link Mockito} class
2681+
*
2682+
* @param mockInitializer a callback to prepare the methods on a mock after its instantiation.
2683+
* @param reified don't pass any values to it. It's a trick to detect the class/interface you
2684+
* want to mock.
2685+
* @return mock controller
2686+
* @since 5.21.0
2687+
*/
2688+
@SafeVarargs
2689+
public static <T> MockedConstruction<T> mockConstruction(
2690+
MockedConstruction.MockInitializer<T> mockInitializer, T... reified) {
2691+
return mockConstruction(withSettings(), mockInitializer, reified);
2692+
}
2693+
2694+
/**
2695+
* Creates a thread-local mock controller for all constructions of the given class.
2696+
* The returned object's {@link MockedConstruction#close()} method must be called upon completing the
2697+
* test or the mock will remain active on the current thread.
2698+
* <p>
2699+
* See examples in javadoc for {@link Mockito} class
2700+
*
2701+
* @param mockSettings the mock settings to use.
2702+
* @param reified don't pass any values to it. It's a trick to detect the class/interface you
2703+
* want to mock.
2704+
* @return mock controller
2705+
* @since 5.21.0
2706+
*/
2707+
@SafeVarargs
2708+
public static <T> MockedConstruction<T> mockConstruction(
2709+
MockSettings mockSettings, T... reified) {
2710+
return mockConstruction(context -> mockSettings, reified);
2711+
}
2712+
2713+
/**
2714+
* Creates a thread-local mock controller for all constructions of the given class.
2715+
* The returned object's {@link MockedConstruction#close()} method must be called upon completing the
2716+
* test or the mock will remain active on the current thread.
2717+
* <p>
2718+
* See examples in javadoc for {@link Mockito} class
2719+
*
2720+
* @param mockSettingsFactory the mock settings to use.
2721+
* @param reified don't pass any values to it. It's a trick to detect the class/interface you
2722+
* want to mock.
2723+
* @return mock controller
2724+
* @since 5.21.0
2725+
*/
2726+
@SafeVarargs
2727+
public static <T> MockedConstruction<T> mockConstruction(
2728+
Function<MockedConstruction.Context, MockSettings> mockSettingsFactory, T... reified) {
2729+
return mockConstruction(mockSettingsFactory, (mock, context) -> {}, reified);
2730+
}
2731+
2732+
/**
2733+
* Creates a thread-local mock controller for all constructions of the given class.
2734+
* The returned object's {@link MockedConstruction#close()} method must be called upon completing the
2735+
* test or the mock will remain active on the current thread.
2736+
* <p>
2737+
* See examples in javadoc for {@link Mockito} class
2738+
*
2739+
* @param mockSettings the settings to use.
2740+
* @param mockInitializer a callback to prepare the methods on a mock after its instantiation.
2741+
* @param reified don't pass any values to it. It's a trick to detect the class/interface you
2742+
* want to mock.
2743+
* @return mock controller
2744+
* @since 5.21.0
2745+
*/
2746+
@SafeVarargs
2747+
public static <T> MockedConstruction<T> mockConstruction(
2748+
MockSettings mockSettings,
2749+
MockedConstruction.MockInitializer<T> mockInitializer,
2750+
T... reified) {
2751+
return mockConstruction(index -> mockSettings, mockInitializer, reified);
2752+
}
2753+
2754+
/**
2755+
* Creates a thread-local mock controller for all constructions of the given class.
2756+
* The returned object's {@link MockedConstruction#close()} method must be called upon completing the
2757+
* test or the mock will remain active on the current thread.
2758+
* <p>
2759+
* See examples in javadoc for {@link Mockito} class
2760+
*
2761+
* @param mockSettingsFactory a function to create settings to use.
2762+
* @param mockInitializer a callback to prepare the methods on a mock after its instantiation.
2763+
* @param reified don't pass any values to it. It's a trick to detect the class/interface you
2764+
* want to mock.
2765+
* @return mock controller
2766+
* @since 5.21.0
2767+
*/
2768+
@SafeVarargs
2769+
public static <T> MockedConstruction<T> mockConstruction(
2770+
Function<MockedConstruction.Context, MockSettings> mockSettingsFactory,
2771+
MockedConstruction.MockInitializer<T> mockInitializer,
2772+
T... reified) {
2773+
if (reified == null || reified.length > 0) {
2774+
throw new IllegalArgumentException(
2775+
"Please don't pass any values here. Java will detect class automagically.");
2776+
}
2777+
2778+
return mockConstruction(getClassOf(reified), mockSettingsFactory, mockInitializer);
2779+
}
2780+
25572781
/**
25582782
* Enables stubbing methods. Use it when you want the mock to return particular value when particular method is called.
25592783
* <p>

0 commit comments

Comments
 (0)