Upgrade to Mockito 3.4.6

Closes gh-22838
This commit is contained in:
Andy Wilkinson
2020-08-08 15:53:42 +01:00
parent f2a52a87ec
commit 969dd35e45
79 changed files with 444 additions and 443 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2019 the original author or authors.
* Copyright 2012-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -33,7 +33,7 @@ import org.springframework.util.ReflectionUtils;
import org.springframework.util.ReflectionUtils.FieldCallback;
/**
* {@link TestExecutionListener} to trigger {@link MockitoAnnotations#initMocks(Object)}
* {@link TestExecutionListener} to trigger {@link MockitoAnnotations#openMocks(Object)}
* when {@link MockBean @MockBean} annotations are used. Primarily to allow
* {@link Captor @Captor} annotations.
*
@@ -43,6 +43,8 @@ import org.springframework.util.ReflectionUtils.FieldCallback;
*/
public class MockitoTestExecutionListener extends AbstractTestExecutionListener {
private static final String MOCKS_ATTRIBUTE_NAME = MockitoTestExecutionListener.class.getName() + ".mocks";
@Override
public final int getOrder() {
return 1950;
@@ -63,9 +65,17 @@ public class MockitoTestExecutionListener extends AbstractTestExecutionListener
}
}
@Override
public void afterTestMethod(TestContext testContext) throws Exception {
Object mocks = testContext.getAttribute(MOCKS_ATTRIBUTE_NAME);
if (mocks instanceof AutoCloseable) {
((AutoCloseable) mocks).close();
}
}
private void initMocks(TestContext testContext) {
if (hasMockitoAnnotations(testContext)) {
MockitoAnnotations.initMocks(testContext.getTestInstance());
testContext.setAttribute(MOCKS_ATTRIBUTE_NAME, MockitoAnnotations.openMocks(testContext.getTestInstance()));
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2019 the original author or authors.
* Copyright 2012-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -20,8 +20,9 @@ import java.util.function.Supplier;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ConfigurableApplicationContext;
@@ -38,6 +39,7 @@ import static org.mockito.Mockito.verify;
*
* @author Phillip Webb
*/
@ExtendWith(MockitoExtension.class)
class ApplicationContextAssertProviderTests {
@Mock
@@ -51,7 +53,6 @@ class ApplicationContextAssertProviderTests {
@BeforeEach
void setup() {
MockitoAnnotations.initMocks(this);
this.startupFailure = new RuntimeException();
this.mockContextSupplier = () -> this.mockContext;
this.startupFailureSupplier = () -> {

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2019 the original author or authors.
* Copyright 2012-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -19,12 +19,12 @@ package org.springframework.boot.test.mock.mockito;
import java.io.InputStream;
import java.lang.reflect.Field;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.ArgumentCaptor;
import org.mockito.Captor;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.context.ApplicationContext;
import org.springframework.test.context.TestContext;
@@ -43,6 +43,7 @@ import static org.mockito.Mockito.verifyNoMoreInteractions;
*
* @author Phillip Webb
*/
@ExtendWith(MockitoExtension.class)
class MockitoTestExecutionListenerTests {
private MockitoTestExecutionListener listener = new MockitoTestExecutionListener();
@@ -56,12 +57,6 @@ class MockitoTestExecutionListenerTests {
@Captor
private ArgumentCaptor<Field> fieldCaptor;
@BeforeEach
void setup() {
MockitoAnnotations.initMocks(this);
given(this.applicationContext.getBean(MockitoPostProcessor.class)).willReturn(this.postProcessor);
}
@Test
void prepareTestInstanceShouldInitMockitoAnnotations() throws Exception {
WithMockitoAnnotations instance = new WithMockitoAnnotations();
@@ -72,23 +67,27 @@ class MockitoTestExecutionListenerTests {
@Test
void prepareTestInstanceShouldInjectMockBean() throws Exception {
given(this.applicationContext.getBean(MockitoPostProcessor.class)).willReturn(this.postProcessor);
WithMockBean instance = new WithMockBean();
this.listener.prepareTestInstance(mockTestContext(instance));
TestContext testContext = mockTestContext(instance);
given(testContext.getApplicationContext()).willReturn(this.applicationContext);
this.listener.prepareTestInstance(testContext);
verify(this.postProcessor).inject(this.fieldCaptor.capture(), eq(instance), any(MockDefinition.class));
assertThat(this.fieldCaptor.getValue().getName()).isEqualTo("mockBean");
}
@Test
void beforeTestMethodShouldDoNothingWhenDirtiesContextAttributeIsNotSet() throws Exception {
WithMockBean instance = new WithMockBean();
this.listener.beforeTestMethod(mockTestContext(instance));
this.listener.beforeTestMethod(mock(TestContext.class));
verifyNoMoreInteractions(this.postProcessor);
}
@Test
void beforeTestMethodShouldInjectMockBeanWhenDirtiesContextAttributeIsSet() throws Exception {
given(this.applicationContext.getBean(MockitoPostProcessor.class)).willReturn(this.postProcessor);
WithMockBean instance = new WithMockBean();
TestContext mockTestContext = mockTestContext(instance);
given(mockTestContext.getApplicationContext()).willReturn(this.applicationContext);
given(mockTestContext.getAttribute(DependencyInjectionTestExecutionListener.REINJECT_DEPENDENCIES_ATTRIBUTE))
.willReturn(Boolean.TRUE);
this.listener.beforeTestMethod(mockTestContext);
@@ -101,7 +100,6 @@ class MockitoTestExecutionListenerTests {
TestContext testContext = mock(TestContext.class);
given(testContext.getTestInstance()).willReturn(instance);
given(testContext.getTestClass()).willReturn((Class) instance.getClass());
given(testContext.getApplicationContext()).willReturn(this.applicationContext);
return testContext;
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2019 the original author or authors.
* Copyright 2012-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -20,12 +20,12 @@ import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.reflect.Field;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.ArgumentCaptor;
import org.mockito.Captor;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
@@ -43,6 +43,7 @@ import static org.mockito.Mockito.verify;
*
* @author Phillip Webb
*/
@ExtendWith(MockitoExtension.class)
class QualifierDefinitionTests {
@Mock
@@ -51,11 +52,6 @@ class QualifierDefinitionTests {
@Captor
private ArgumentCaptor<DependencyDescriptor> descriptorCaptor;
@BeforeEach
void setup() {
MockitoAnnotations.initMocks(this);
}
@Test
void forElementFieldIsNullShouldReturnNull() {
assertThat(QualifierDefinition.forElement((Field) null)).isNull();

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2019 the original author or authors.
* Copyright 2012-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -20,10 +20,11 @@ import java.net.URI;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.ArgumentCaptor;
import org.mockito.Captor;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.http.client.ClientHttpRequest;
@@ -49,6 +50,7 @@ import static org.springframework.test.web.client.response.MockRestResponseCreat
*
* @author Phillip Webb
*/
@ExtendWith(MockitoExtension.class)
class RootUriRequestExpectationManagerTests {
private String uri = "https://example.com";
@@ -63,7 +65,6 @@ class RootUriRequestExpectationManagerTests {
@BeforeEach
void setup() {
MockitoAnnotations.initMocks(this);
this.manager = new RootUriRequestExpectationManager(this.uri, this.delegate);
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2019 the original author or authors.
* Copyright 2012-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -25,9 +25,10 @@ import com.gargoylesoftware.htmlunit.WebConnection;
import com.gargoylesoftware.htmlunit.WebRequest;
import com.gargoylesoftware.htmlunit.WebResponse;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.ArgumentCaptor;
import org.mockito.Captor;
import org.mockito.MockitoAnnotations;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.mock.env.MockEnvironment;
@@ -44,15 +45,12 @@ import static org.mockito.Mockito.verify;
* @author Phillip Webb
*/
@SuppressWarnings("resource")
@ExtendWith(MockitoExtension.class)
class LocalHostWebClientTests {
@Captor
private ArgumentCaptor<WebRequest> requestCaptor;
LocalHostWebClientTests() {
MockitoAnnotations.initMocks(this);
}
@Test
void createWhenEnvironmentIsNullWillThrowException() {
assertThatIllegalArgumentException().isThrownBy(() -> new LocalHostWebClient(null))

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2019 the original author or authors.
* Copyright 2012-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -25,9 +25,10 @@ import com.gargoylesoftware.htmlunit.WebConsole;
import com.gargoylesoftware.htmlunit.WebRequest;
import com.gargoylesoftware.htmlunit.WebWindow;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.ArgumentMatcher;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.mockito.junit.jupiter.MockitoExtension;
import org.openqa.selenium.Capabilities;
import org.springframework.core.env.Environment;
@@ -45,13 +46,13 @@ import static org.mockito.Mockito.verify;
*
* @author Phillip Webb
*/
@ExtendWith(MockitoExtension.class)
class LocalHostWebConnectionHtmlUnitDriverTests {
@Mock
private WebClient webClient;
private final WebClient webClient;
LocalHostWebConnectionHtmlUnitDriverTests() {
MockitoAnnotations.initMocks(this);
LocalHostWebConnectionHtmlUnitDriverTests(@Mock WebClient webClient) {
this.webClient = webClient;
given(this.webClient.getOptions()).willReturn(new WebClientOptions());
given(this.webClient.getWebConsole()).willReturn(new WebConsole());
}