Skip to content

Commit

Permalink
Migrate to Mockito.mock(T...) where feasible
Browse files Browse the repository at this point in the history
  • Loading branch information
sbrannen authored and marcphilipp committed Apr 22, 2023
1 parent ee34052 commit e99921b
Show file tree
Hide file tree
Showing 36 changed files with 82 additions and 103 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
@TestInstance(Lifecycle.PER_CLASS)
abstract class AbstractExecutionConditionTests {

private final ExtensionContext context = mock(ExtensionContext.class);
private final ExtensionContext context = mock();

private ConditionEvaluationResult result;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import java.lang.annotation.ElementType;
Expand All @@ -32,7 +33,6 @@
import org.junit.jupiter.api.extension.ParameterResolver;
import org.junit.platform.commons.PreconditionViolationException;
import org.junit.platform.commons.util.ReflectionUtils;
import org.mockito.Mockito;

/**
* @since 5.6
Expand Down Expand Up @@ -86,13 +86,13 @@ void resolve() {
}

private static ParameterContext parameterContext(Parameter parameter) {
ParameterContext parameterContext = Mockito.mock(ParameterContext.class);
ParameterContext parameterContext = mock();
when(parameterContext.getParameter()).thenReturn(parameter);
return parameterContext;
}

private static ExtensionContext extensionContext() {
ExtensionContext extensionContext = Mockito.mock(ExtensionContext.class);
ExtensionContext extensionContext = mock();
when(extensionContext.getDisplayName()).thenReturn("Displaying");
return extensionContext;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
*/
class CachingJupiterConfigurationTests {

private final JupiterConfiguration delegate = mock(JupiterConfiguration.class);
private final JupiterConfiguration delegate = mock();
private final JupiterConfiguration cache = new CachingJupiterConfiguration(delegate);

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,14 @@ void getDefaultTestInstanceLifecyclePreconditions() {

@Test
void getDefaultTestInstanceLifecycleWithNoConfigParamSet() {
JupiterConfiguration configuration = new DefaultJupiterConfiguration(mock(ConfigurationParameters.class));
JupiterConfiguration configuration = new DefaultJupiterConfiguration(mock());
Lifecycle lifecycle = configuration.getDefaultTestInstanceLifecycle();
assertThat(lifecycle).isEqualTo(PER_METHOD);
}

@Test
void getDefaultTempDirCleanupModeWithNoConfigParamSet() {
JupiterConfiguration configuration = new DefaultJupiterConfiguration(mock(ConfigurationParameters.class));
JupiterConfiguration configuration = new DefaultJupiterConfiguration(mock());
CleanupMode cleanupMode = configuration.getDefaultTempDirCleanupMode();
assertThat(cleanupMode).isEqualTo(ALWAYS);
}
Expand All @@ -74,7 +74,7 @@ void getDefaultTestInstanceLifecycleWithConfigParamSet() {

@Test
void shouldGetDefaultDisplayNameGeneratorWithConfigParamSet() {
ConfigurationParameters parameters = mock(ConfigurationParameters.class);
ConfigurationParameters parameters = mock();
String key = Constants.DEFAULT_DISPLAY_NAME_GENERATOR_PROPERTY_NAME;
when(parameters.get(key)).thenReturn(Optional.of(CustomDisplayNameGenerator.class.getName()));
JupiterConfiguration configuration = new DefaultJupiterConfiguration(parameters);
Expand All @@ -86,7 +86,7 @@ void shouldGetDefaultDisplayNameGeneratorWithConfigParamSet() {

@Test
void shouldGetStandardAsDefaultDisplayNameGeneratorWithoutConfigParamSet() {
ConfigurationParameters parameters = mock(ConfigurationParameters.class);
ConfigurationParameters parameters = mock();
String key = Constants.DEFAULT_DISPLAY_NAME_GENERATOR_PROPERTY_NAME;
when(parameters.get(key)).thenReturn(Optional.empty());
JupiterConfiguration configuration = new DefaultJupiterConfiguration(parameters);
Expand All @@ -98,7 +98,7 @@ void shouldGetStandardAsDefaultDisplayNameGeneratorWithoutConfigParamSet() {

@Test
void shouldGetNothingAsDefaultTestMethodOrderWithoutConfigParamSet() {
ConfigurationParameters parameters = mock(ConfigurationParameters.class);
ConfigurationParameters parameters = mock();
String key = Constants.DEFAULT_TEST_METHOD_ORDER_PROPERTY_NAME;
when(parameters.get(key)).thenReturn(Optional.empty());
JupiterConfiguration configuration = new DefaultJupiterConfiguration(parameters);
Expand All @@ -109,7 +109,7 @@ void shouldGetNothingAsDefaultTestMethodOrderWithoutConfigParamSet() {
}

private void assertDefaultConfigParam(String configValue, Lifecycle expected) {
ConfigurationParameters configParams = mock(ConfigurationParameters.class);
ConfigurationParameters configParams = mock();
when(configParams.get(KEY)).thenReturn(Optional.ofNullable(configValue));
Lifecycle lifecycle = new DefaultJupiterConfiguration(configParams).getDefaultTestInstanceLifecycle();
assertThat(lifecycle).isEqualTo(expected);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class InstantiatingConfigurationParameterConverterTests {
@Test
void shouldInstantiateConfiguredClass(LogRecordListener listener) {

ConfigurationParameters configurationParameters = mock(ConfigurationParameters.class);
ConfigurationParameters configurationParameters = mock();
when(configurationParameters.get(KEY)).thenReturn(Optional.of(CustomDisplayNameGenerator.class.getName()));

InstantiatingConfigurationParameterConverter<DisplayNameGenerator> converter = new InstantiatingConfigurationParameterConverter<>(
Expand All @@ -54,7 +54,7 @@ void shouldInstantiateConfiguredClass(LogRecordListener listener) {

@Test
void shouldReturnEmptyOptionalIfNoConfigurationFound() {
ConfigurationParameters configurationParameters = mock(ConfigurationParameters.class);
ConfigurationParameters configurationParameters = mock();
when(configurationParameters.get(KEY)).thenReturn(Optional.empty());

InstantiatingConfigurationParameterConverter<DisplayNameGenerator> converter = new InstantiatingConfigurationParameterConverter<>(
Expand All @@ -66,7 +66,7 @@ void shouldReturnEmptyOptionalIfNoConfigurationFound() {

@Test
void shouldReturnEmptyOptionalIfConfigurationIsBlank() {
ConfigurationParameters configurationParameters = mock(ConfigurationParameters.class);
ConfigurationParameters configurationParameters = mock();
when(configurationParameters.get(KEY)).thenReturn(Optional.of(""));

InstantiatingConfigurationParameterConverter<DisplayNameGenerator> converter = new InstantiatingConfigurationParameterConverter<>(
Expand All @@ -78,7 +78,7 @@ void shouldReturnEmptyOptionalIfConfigurationIsBlank() {

@Test
void shouldTrimAndInstantiateConfiguredClass(LogRecordListener listener) {
ConfigurationParameters configurationParameters = mock(ConfigurationParameters.class);
ConfigurationParameters configurationParameters = mock();
String classNameWithSpaces = " " + CustomDisplayNameGenerator.class.getName() + " ";
when(configurationParameters.get(KEY)).thenReturn(Optional.of(classNameWithSpaces));

Expand All @@ -95,7 +95,7 @@ void shouldTrimAndInstantiateConfiguredClass(LogRecordListener listener) {

@Test
void shouldReturnEmptyOptionalIfNoClassFound(LogRecordListener listener) {
ConfigurationParameters configurationParameters = mock(ConfigurationParameters.class);
ConfigurationParameters configurationParameters = mock();
when(configurationParameters.get(KEY)).thenReturn(Optional.of("random-string"));

InstantiatingConfigurationParameterConverter<DisplayNameGenerator> converter = new InstantiatingConfigurationParameterConverter<>(
Expand All @@ -111,7 +111,7 @@ void shouldReturnEmptyOptionalIfNoClassFound(LogRecordListener listener) {

@Test
void shouldReturnEmptyOptionalIfClassFoundIsNotATypeOfExpectedType(LogRecordListener listener) {
ConfigurationParameters configurationParameters = mock(ConfigurationParameters.class);
ConfigurationParameters configurationParameters = mock();
when(configurationParameters.get(KEY)).thenReturn(Optional.of(Object.class.getName()));

InstantiatingConfigurationParameterConverter<DisplayNameGenerator> converter = new InstantiatingConfigurationParameterConverter<>(
Expand All @@ -127,7 +127,7 @@ void shouldReturnEmptyOptionalIfClassFoundIsNotATypeOfExpectedType(LogRecordList

@Test
void shouldReturnEmptyOptionalIfClassNameIsNotFullyQualified(LogRecordListener listener) {
ConfigurationParameters configurationParameters = mock(ConfigurationParameters.class);
ConfigurationParameters configurationParameters = mock();
when(configurationParameters.get(KEY)).thenReturn(
Optional.of(CustomDisplayNameGenerator.class.getSimpleName()));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ void shouldGetDisplayNameFromSupplierIfNoDisplayNameAnnotationPresent() {
@Nested
class ClassDisplayNameSupplierTests {

private JupiterConfiguration configuration = mock(JupiterConfiguration.class);
private JupiterConfiguration configuration = mock();

@Test
void shouldGetDisplayNameFromDisplayNameGenerationAnnotation() {
Expand Down Expand Up @@ -107,7 +107,7 @@ void shouldGetDisplayNameFromDefaultDisplayNameGenerator() {
@Nested
class NestedClassDisplayNameTests {

private JupiterConfiguration configuration = mock(JupiterConfiguration.class);
private JupiterConfiguration configuration = mock();

@Test
void shouldGetDisplayNameFromDisplayNameGenerationAnnotation() {
Expand All @@ -131,7 +131,7 @@ void shouldGetDisplayNameFromDefaultDisplayNameGenerator() {
@Nested
class MethodDisplayNameTests {

private JupiterConfiguration configuration = mock(JupiterConfiguration.class);
private JupiterConfiguration configuration = mock();

@Test
void shouldGetDisplayNameFromDisplayNameGenerationAnnotation() throws Exception {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
*/
public class ExtensionContextTests {

private final JupiterConfiguration configuration = mock(JupiterConfiguration.class);
private final JupiterConfiguration configuration = mock();

@BeforeEach
void setUp() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class JupiterTestDescriptorTests {

private static final UniqueId uniqueId = UniqueId.root("enigma", "foo");

private final JupiterConfiguration configuration = mock(JupiterConfiguration.class);
private final JupiterConfiguration configuration = mock();

@BeforeEach
void setUp() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
import org.junit.jupiter.api.extension.ExtensionContext;
import org.junit.jupiter.engine.config.JupiterConfiguration;
import org.junit.jupiter.engine.execution.JupiterEngineExecutionContext;
import org.junit.jupiter.engine.extension.MutableExtensionRegistry;
import org.junit.platform.engine.TestSource;
import org.junit.platform.engine.UniqueId;
import org.junit.platform.engine.support.descriptor.ClasspathResourceSource;
Expand All @@ -39,7 +38,6 @@
import org.junit.platform.engine.support.descriptor.FileSource;
import org.junit.platform.engine.support.descriptor.MethodSource;
import org.junit.platform.engine.support.descriptor.UriSource;
import org.junit.platform.engine.support.hierarchical.Node;
import org.junit.platform.engine.support.hierarchical.OpenTest4JAwareThrowableCollector;

/**
Expand Down Expand Up @@ -134,17 +132,17 @@ class Streams {

@BeforeEach
void before() throws Exception {
jupiterConfiguration = mock(JupiterConfiguration.class);
jupiterConfiguration = mock();
when(jupiterConfiguration.getDefaultDisplayNameGenerator()).thenReturn(new DisplayNameGenerator.Standard());

extensionContext = mock(ExtensionContext.class);
extensionContext = mock();
isClosed = false;

context = new JupiterEngineExecutionContext(null, null) //
.extend() //
.withThrowableCollector(new OpenTest4JAwareThrowableCollector()) //
.withExtensionContext(extensionContext) //
.withExtensionRegistry(mock(MutableExtensionRegistry.class)) //
.withExtensionRegistry(mock()) //
.build();

Method testMethod = CustomStreamTestCase.class.getDeclaredMethod("customStream");
Expand All @@ -158,7 +156,7 @@ void streamsFromTestFactoriesShouldBeClosed() {
Stream<DynamicTest> dynamicTestStream = Stream.empty();
prepareMockForTestInstanceWithCustomStream(dynamicTestStream);

descriptor.invokeTestMethod(context, mock(Node.DynamicTestExecutor.class));
descriptor.invokeTestMethod(context, mock());

assertTrue(isClosed);
}
Expand All @@ -168,7 +166,7 @@ void streamsFromTestFactoriesShouldBeClosedWhenTheyThrow() {
Stream<Integer> integerStream = Stream.of(1, 2);
prepareMockForTestInstanceWithCustomStream(integerStream);

descriptor.invokeTestMethod(context, mock(Node.DynamicTestExecutor.class));
descriptor.invokeTestMethod(context, mock());

assertTrue(isClosed);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ class TestInstanceLifecycleUtilsTests {
@Test
void getTestInstanceLifecyclePreconditions() {
PreconditionViolationException exception = assertThrows(PreconditionViolationException.class,
() -> getTestInstanceLifecycle(null, new DefaultJupiterConfiguration(mock(ConfigurationParameters.class))));
() -> getTestInstanceLifecycle(null, new DefaultJupiterConfiguration(mock())));
assertThat(exception).hasMessage("testClass must not be null");

exception = assertThrows(PreconditionViolationException.class,
Expand All @@ -60,22 +60,21 @@ void getTestInstanceLifecyclePreconditions() {

@Test
void getTestInstanceLifecycleWithNoConfigParamSet() {
Lifecycle lifecycle = getTestInstanceLifecycle(getClass(),
new DefaultJupiterConfiguration(mock(ConfigurationParameters.class)));
Lifecycle lifecycle = getTestInstanceLifecycle(getClass(), new DefaultJupiterConfiguration(mock()));
assertThat(lifecycle).isEqualTo(PER_METHOD);
}

@Test
void getTestInstanceLifecycleWithConfigParamSet() {
ConfigurationParameters configParams = mock(ConfigurationParameters.class);
ConfigurationParameters configParams = mock();
when(configParams.get(KEY)).thenReturn(Optional.of(PER_CLASS.name().toLowerCase()));
Lifecycle lifecycle = getTestInstanceLifecycle(getClass(), new DefaultJupiterConfiguration(configParams));
assertThat(lifecycle).isEqualTo(PER_CLASS);
}

@Test
void getTestInstanceLifecycleWithLocalConfigThatOverridesCustomDefaultSetViaConfigParam() {
ConfigurationParameters configParams = mock(ConfigurationParameters.class);
ConfigurationParameters configParams = mock();
when(configParams.get(KEY)).thenReturn(Optional.of(PER_CLASS.name().toLowerCase()));
Lifecycle lifecycle = getTestInstanceLifecycle(TestCase.class, new DefaultJupiterConfiguration(configParams));
assertThat(lifecycle).isEqualTo(PER_METHOD);
Expand All @@ -84,16 +83,14 @@ void getTestInstanceLifecycleWithLocalConfigThatOverridesCustomDefaultSetViaConf
@Test
void getTestInstanceLifecycleFromMetaAnnotationWithNoConfigParamSet() {
Class<?> testClass = BaseMetaAnnotatedTestCase.class;
Lifecycle lifecycle = getTestInstanceLifecycle(testClass,
new DefaultJupiterConfiguration(mock(ConfigurationParameters.class)));
Lifecycle lifecycle = getTestInstanceLifecycle(testClass, new DefaultJupiterConfiguration(mock()));
assertThat(lifecycle).isEqualTo(PER_CLASS);
}

@Test
void getTestInstanceLifecycleFromSpecializedClassWithNoConfigParamSet() {
Class<?> testClass = SpecializedTestCase.class;
Lifecycle lifecycle = getTestInstanceLifecycle(testClass,
new DefaultJupiterConfiguration(mock(ConfigurationParameters.class)));
Lifecycle lifecycle = getTestInstanceLifecycle(testClass, new DefaultJupiterConfiguration(mock()));
assertThat(lifecycle).isEqualTo(PER_CLASS);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@ class TestTemplateInvocationTestDescriptorTests {
void invocationsDoNotDeclareExclusiveResources() throws Exception {
Class<MyTestCase> testClass = MyTestCase.class;
Method testTemplateMethod = testClass.getDeclaredMethod("testTemplate");
JupiterConfiguration configuration = mock(JupiterConfiguration.class);
JupiterConfiguration configuration = mock();
when(configuration.getDefaultDisplayNameGenerator()).thenReturn(new DisplayNameGenerator.Standard());
TestTemplateTestDescriptor parent = new TestTemplateTestDescriptor(UniqueId.root("segment", "template"),
testClass, testTemplateMethod, configuration);
TestTemplateInvocationContext invocationContext = mock(TestTemplateInvocationContext.class);
TestTemplateInvocationContext invocationContext = mock();
when(invocationContext.getDisplayName(anyInt())).thenReturn("invocation");

TestTemplateInvocationTestDescriptor testDescriptor = new TestTemplateInvocationTestDescriptor(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
* @since 5.0
*/
class TestTemplateTestDescriptorTests {
private JupiterConfiguration jupiterConfiguration = mock(JupiterConfiguration.class);
private JupiterConfiguration jupiterConfiguration = mock();

@Test
void inheritsTagsFromParent() throws Exception {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,8 @@
*/
class DiscoverySelectorResolverTests {

private final JupiterConfiguration configuration = mock(JupiterConfiguration.class);
private final LauncherDiscoveryListener discoveryListener = mock(LauncherDiscoveryListener.class);
private final JupiterConfiguration configuration = mock();
private final LauncherDiscoveryListener discoveryListener = mock();
private final JupiterEngineDescriptor engineDescriptor = new JupiterEngineDescriptor(engineId(), configuration);

@BeforeEach
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,12 @@ abstract class AbstractExecutableInvokerTests {

private static final String ENIGMA = "enigma";

protected final MethodSource instance = mock(MethodSource.class);
protected final MethodSource instance = mock();
protected Method method;

protected final ExtensionContext extensionContext = mock(ExtensionContext.class);
protected final ExtensionContext extensionContext = mock();

private final JupiterConfiguration configuration = mock(JupiterConfiguration.class);
private final JupiterConfiguration configuration = mock();

protected final MutableExtensionRegistry extensionRegistry = MutableExtensionRegistry.createRegistryWithDefaultExtensions(
configuration);
Expand Down
Loading

0 comments on commit e99921b

Please sign in to comment.