Migrate to BDD Mockito
Migrate all tests to consistently use BDD Mockito. Also add checksyle rule to enforce going forwards.
This commit is contained in:
@@ -37,11 +37,11 @@ import static org.hamcrest.MatcherAssert.assertThat;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.hamcrest.Matchers.instanceOf;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.Mockito.doCallRealMethod;
|
||||
import static org.mockito.BDDMockito.given;
|
||||
import static org.mockito.BDDMockito.willCallRealMethod;
|
||||
import static org.mockito.Mockito.never;
|
||||
import static org.mockito.Mockito.times;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
/**
|
||||
* Unit tests for {@link EventPublishingTestExecutionListener}.
|
||||
@@ -71,10 +71,10 @@ public class EventPublishingTestExecutionListenerTests {
|
||||
@Before
|
||||
public void configureMock() {
|
||||
// Force Mockito to invoke the interface default method
|
||||
doCallRealMethod().when(testContext).publishEvent(any());
|
||||
when(testContext.getApplicationContext()).thenReturn(applicationContext);
|
||||
willCallRealMethod().given(testContext).publishEvent(any());
|
||||
given(testContext.getApplicationContext()).willReturn(applicationContext);
|
||||
// Only allow events to be published for test methods named "publish*".
|
||||
when(testContext.hasApplicationContext()).thenReturn(testName.getMethodName().startsWith("publish"));
|
||||
given(testContext.hasApplicationContext()).willReturn(testName.getMethodName().startsWith("publish"));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
@@ -41,8 +41,8 @@ import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.BDDMockito.given;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
/**
|
||||
* Tests for {@link DisabledIfCondition} that verify actual condition evaluation
|
||||
@@ -124,12 +124,12 @@ class DisabledIfConditionTests {
|
||||
Class<?> testClass = SpringTestCase.class;
|
||||
Method method = ReflectionUtils.findMethod(getClass(), methodName);
|
||||
Store store = mock(Store.class);
|
||||
when(store.getOrComputeIfAbsent(any(), any(), any())).thenReturn(new TestContextManager(testClass));
|
||||
given(store.getOrComputeIfAbsent(any(), any(), any())).willReturn(new TestContextManager(testClass));
|
||||
|
||||
ExtensionContext extensionContext = mock(ExtensionContext.class);
|
||||
when(extensionContext.getTestClass()).thenReturn(Optional.of(testClass));
|
||||
when(extensionContext.getElement()).thenReturn(Optional.of(method));
|
||||
when(extensionContext.getStore(any())).thenReturn(store);
|
||||
given(extensionContext.getTestClass()).willReturn(Optional.of(testClass));
|
||||
given(extensionContext.getElement()).willReturn(Optional.of(method));
|
||||
given(extensionContext.getStore(any())).willReturn(store);
|
||||
return extensionContext;
|
||||
}
|
||||
|
||||
|
||||
@@ -25,8 +25,8 @@ import org.mockito.stubbing.Answer;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
|
||||
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
|
||||
import static org.mockito.Mockito.doAnswer;
|
||||
import static org.mockito.Mockito.doThrow;
|
||||
import static org.mockito.BDDMockito.willAnswer;
|
||||
import static org.mockito.BDDMockito.willThrow;
|
||||
import static org.mockito.Mockito.mock;
|
||||
|
||||
/**
|
||||
@@ -55,7 +55,7 @@ public class SpringFailOnTimeoutTests {
|
||||
|
||||
@Test
|
||||
public void userExceptionPropagates() throws Throwable {
|
||||
doThrow(new Boom()).when(statement).evaluate();
|
||||
willThrow(new Boom()).given(statement).evaluate();
|
||||
|
||||
assertThatExceptionOfType(Boom.class).isThrownBy(() ->
|
||||
new SpringFailOnTimeout(statement, 1).evaluate());
|
||||
@@ -63,10 +63,10 @@ public class SpringFailOnTimeoutTests {
|
||||
|
||||
@Test
|
||||
public void timeoutExceptionThrownIfNoUserException() throws Throwable {
|
||||
doAnswer((Answer<Void>) invocation -> {
|
||||
willAnswer((Answer<Void>) invocation -> {
|
||||
TimeUnit.MILLISECONDS.sleep(50);
|
||||
return null;
|
||||
}).when(statement).evaluate();
|
||||
}).given(statement).evaluate();
|
||||
|
||||
assertThatExceptionOfType(TimeoutException.class).isThrownBy(() ->
|
||||
new SpringFailOnTimeout(statement, 1).evaluate());
|
||||
@@ -74,7 +74,7 @@ public class SpringFailOnTimeoutTests {
|
||||
|
||||
@Test
|
||||
public void noExceptionThrownIfNoUserExceptionAndTimeoutDoesNotOccur() throws Throwable {
|
||||
doAnswer((Answer<Void>) invocation -> null).when(statement).evaluate();
|
||||
willAnswer((Answer<Void>) invocation -> null).given(statement).evaluate();
|
||||
new SpringFailOnTimeout(statement, 100).evaluate();
|
||||
}
|
||||
|
||||
|
||||
@@ -37,8 +37,8 @@ import static org.junit.Assert.assertArrayEquals;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.mockito.ArgumentMatchers.anyString;
|
||||
import static org.mockito.BDDMockito.given;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
import static org.springframework.test.context.support.TestPropertySourceUtils.addInlinedPropertiesToEnvironment;
|
||||
import static org.springframework.test.context.support.TestPropertySourceUtils.addPropertiesFilesToEnvironment;
|
||||
import static org.springframework.test.context.support.TestPropertySourceUtils.buildMergedTestPropertySources;
|
||||
@@ -165,7 +165,7 @@ public class TestPropertySourceUtilsTests {
|
||||
String pair = "key = value";
|
||||
ByteArrayResource resource = new ByteArrayResource(pair.getBytes(), "from inlined property: " + pair);
|
||||
ResourceLoader resourceLoader = mock(ResourceLoader.class);
|
||||
when(resourceLoader.getResource(anyString())).thenReturn(resource);
|
||||
given(resourceLoader.getResource(anyString())).willReturn(resource);
|
||||
|
||||
addPropertiesFilesToEnvironment(environment, resourceLoader, FOO_LOCATIONS);
|
||||
assertEquals(1, propertySources.size());
|
||||
|
||||
@@ -44,9 +44,9 @@ import static org.hamcrest.CoreMatchers.sameInstance;
|
||||
import static org.hamcrest.MatcherAssert.assertThat;
|
||||
import static org.hamcrest.Matchers.isEmptyString;
|
||||
import static org.hamcrest.core.IsNot.not;
|
||||
import static org.mockito.BDDMockito.given;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.verifyNoMoreInteractions;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
/**
|
||||
* Unit and integration tests for {@link DelegatingWebConnection}.
|
||||
@@ -92,7 +92,7 @@ public class DelegatingWebConnectionTests {
|
||||
|
||||
@Test
|
||||
public void getResponseDefault() throws Exception {
|
||||
when(defaultConnection.getResponse(request)).thenReturn(expectedResponse);
|
||||
given(defaultConnection.getResponse(request)).willReturn(expectedResponse);
|
||||
WebResponse response = webConnection.getResponse(request);
|
||||
|
||||
assertThat(response, sameInstance(expectedResponse));
|
||||
@@ -104,8 +104,8 @@ public class DelegatingWebConnectionTests {
|
||||
|
||||
@Test
|
||||
public void getResponseAllMatches() throws Exception {
|
||||
when(matcher1.matches(request)).thenReturn(true);
|
||||
when(connection1.getResponse(request)).thenReturn(expectedResponse);
|
||||
given(matcher1.matches(request)).willReturn(true);
|
||||
given(connection1.getResponse(request)).willReturn(expectedResponse);
|
||||
WebResponse response = webConnection.getResponse(request);
|
||||
|
||||
assertThat(response, sameInstance(expectedResponse));
|
||||
@@ -116,8 +116,8 @@ public class DelegatingWebConnectionTests {
|
||||
|
||||
@Test
|
||||
public void getResponseSecondMatches() throws Exception {
|
||||
when(matcher2.matches(request)).thenReturn(true);
|
||||
when(connection2.getResponse(request)).thenReturn(expectedResponse);
|
||||
given(matcher2.matches(request)).willReturn(true);
|
||||
given(connection2.getResponse(request)).willReturn(expectedResponse);
|
||||
WebResponse response = webConnection.getResponse(request);
|
||||
|
||||
assertThat(response, sameInstance(expectedResponse));
|
||||
|
||||
@@ -44,8 +44,8 @@ import static org.hamcrest.CoreMatchers.equalTo;
|
||||
import static org.hamcrest.CoreMatchers.nullValue;
|
||||
import static org.hamcrest.MatcherAssert.assertThat;
|
||||
import static org.hamcrest.Matchers.notNullValue;
|
||||
import static org.mockito.BDDMockito.given;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
/**
|
||||
* Integration tests for {@link MockMvcWebConnectionBuilderSupport}.
|
||||
@@ -70,7 +70,7 @@ public class MockMvcConnectionBuilderSupportTests {
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
when(this.client.getWebConnection()).thenReturn(mock(WebConnection.class));
|
||||
given(this.client.getWebConnection()).willReturn(mock(WebConnection.class));
|
||||
this.builder = new MockMvcWebConnectionBuilderSupport(this.wac) {};
|
||||
}
|
||||
|
||||
|
||||
@@ -33,7 +33,7 @@ import static org.hamcrest.CoreMatchers.equalTo;
|
||||
import static org.hamcrest.MatcherAssert.assertThat;
|
||||
import static org.hamcrest.Matchers.notNullValue;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.Mockito.when;
|
||||
import static org.mockito.BDDMockito.given;
|
||||
|
||||
/**
|
||||
* Unit tests for {@link WebConnectionHtmlUnitDriver}.
|
||||
@@ -52,7 +52,7 @@ public class WebConnectionHtmlUnitDriverTests {
|
||||
|
||||
@Before
|
||||
public void setup() throws Exception {
|
||||
when(this.connection.getResponse(any(WebRequest.class))).thenThrow(new IOException(""));
|
||||
given(this.connection.getResponse(any(WebRequest.class))).willThrow(new IOException(""));
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user