Enforce use of BDDMockito

1. Replace Mockito.verify*() with BDDMockito.then()
2. Replace Mockito.doReturn() with BDDMockito.willReturn()
3. Adjust checkstyle rule

See gh-29178
This commit is contained in:
Yanming Zhou
2021-12-27 18:36:32 +08:00
committed by Stephane Nicoll
parent f60af4dbbd
commit b49418aaaf
190 changed files with 1291 additions and 1142 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2020 the original author or authors.
* Copyright 2012-2021 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.
@@ -31,13 +31,14 @@ import org.springframework.context.support.StaticApplicationContext;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
import static org.mockito.Mockito.verify;
import static org.mockito.BDDMockito.then;
/**
* Tests for {@link ApplicationContextAssertProvider} and
* {@link AssertProviderApplicationContextInvocationHandler}.
*
* @author Phillip Webb
* @author Yanming Zhou
*/
@ExtendWith(MockitoExtension.class)
class ApplicationContextAssertProviderTests {
@@ -102,7 +103,7 @@ class ApplicationContextAssertProviderTests {
ApplicationContextAssertProvider<ApplicationContext> context = get(this.mockContextSupplier);
assertThat((Object) context).isNotNull();
context.getBean("foo");
verify(this.mockContext).getBean("foo");
then(this.mockContext).should().getBean("foo");
}
@Test
@@ -186,7 +187,7 @@ class ApplicationContextAssertProviderTests {
void closeShouldCloseContext() {
ApplicationContextAssertProvider<ApplicationContext> context = get(this.mockContextSupplier);
context.close();
verify(this.mockContext).close();
then(this.mockContext).should().close();
}
private ApplicationContextAssertProvider<ApplicationContext> get(Supplier<ApplicationContext> contextSupplier) {

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2020 the original author or authors.
* Copyright 2012-2021 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.
@@ -28,14 +28,15 @@ import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.context.annotation.Import;
import org.springframework.stereotype.Component;
import static org.mockito.BDDMockito.then;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
/**
* Tests for nested test configuration when the configuration is inherited from the
* enclosing class (the default behaviour).
*
* @author Andy Wilkinson
* @author Yanming Zhou
*/
@SpringBootTest(classes = AppConfiguration.class)
@Import(ActionPerformer.class)
@@ -50,14 +51,14 @@ class InheritedNestedTestConfigurationTests {
@Test
void mockWasInvokedOnce() {
this.performer.run();
verify(this.action, times(1)).perform();
then(this.action).should().perform();
}
@Test
void mockWasInvokedTwice() {
this.performer.run();
this.performer.run();
verify(this.action, times(2)).perform();
then(this.action).should(times(2)).perform();
}
@Nested
@@ -66,14 +67,14 @@ class InheritedNestedTestConfigurationTests {
@Test
void mockWasInvokedOnce() {
InheritedNestedTestConfigurationTests.this.performer.run();
verify(InheritedNestedTestConfigurationTests.this.action, times(1)).perform();
then(InheritedNestedTestConfigurationTests.this.action).should().perform();
}
@Test
void mockWasInvokedTwice() {
InheritedNestedTestConfigurationTests.this.performer.run();
InheritedNestedTestConfigurationTests.this.performer.run();
verify(InheritedNestedTestConfigurationTests.this.action, times(2)).perform();
then(InheritedNestedTestConfigurationTests.this.action).should(times(2)).perform();
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2019 the original author or authors.
* Copyright 2012-2021 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.
@@ -31,7 +31,7 @@ import org.springframework.context.annotation.Configuration;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.verify;
import static org.mockito.BDDMockito.then;
/**
* Test {@link MockBean @MockBean} on a test class field can be used to replace existing
@@ -39,6 +39,7 @@ import static org.mockito.Mockito.verify;
*
* @author Stephane Nicoll
* @author Phillip Webb
* @author Yanming Zhou
*/
@ExtendWith(SpringExtension.class)
class MockBeanOnTestFieldForExistingBeanWithQualifierIntegrationTests {
@@ -56,7 +57,7 @@ class MockBeanOnTestFieldForExistingBeanWithQualifierIntegrationTests {
@Test
void testMocking() {
this.caller.sayGreeting();
verify(this.service).greeting();
then(this.service).should().greeting();
}
@Test

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2019 the original author or authors.
* Copyright 2012-2021 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.
@@ -37,8 +37,8 @@ import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.anyBoolean;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.BDDMockito.given;
import static org.mockito.BDDMockito.then;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
/**
* Test {@link MockBean @MockBean} when mixed with Spring AOP.
@@ -60,9 +60,9 @@ class MockBeanWithAopProxyTests {
given(this.dateService.getDate(false)).willReturn(2L);
Long d2 = this.dateService.getDate(false);
assertThat(d2).isEqualTo(2L);
verify(this.dateService, times(2)).getDate(false);
verify(this.dateService, times(2)).getDate(eq(false));
verify(this.dateService, times(2)).getDate(anyBoolean());
then(this.dateService).should(times(2)).getDate(false);
then(this.dateService).should(times(2)).getDate(eq(false));
then(this.dateService).should(times(2)).getDate(anyBoolean());
}
@Configuration(proxyBeanMethods = false)

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2020 the original author or authors.
* Copyright 2012-2021 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.
@@ -34,14 +34,14 @@ import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.BDDMockito.given;
import static org.mockito.BDDMockito.then;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoMoreInteractions;
/**
* Tests for {@link MockitoTestExecutionListener}.
*
* @author Phillip Webb
* @author Yanming Zhou
*/
@ExtendWith(MockitoExtension.class)
class MockitoTestExecutionListenerTests {
@@ -72,14 +72,14 @@ class MockitoTestExecutionListenerTests {
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));
then(this.postProcessor).should().inject(this.fieldCaptor.capture(), eq(instance), any(MockDefinition.class));
assertThat(this.fieldCaptor.getValue().getName()).isEqualTo("mockBean");
}
@Test
void beforeTestMethodShouldDoNothingWhenDirtiesContextAttributeIsNotSet() throws Exception {
this.listener.beforeTestMethod(mock(TestContext.class));
verifyNoMoreInteractions(this.postProcessor);
then(this.postProcessor).shouldHaveNoMoreInteractions();
}
@Test
@@ -91,7 +91,7 @@ class MockitoTestExecutionListenerTests {
given(mockTestContext.getAttribute(DependencyInjectionTestExecutionListener.REINJECT_DEPENDENCIES_ATTRIBUTE))
.willReturn(Boolean.TRUE);
this.listener.beforeTestMethod(mockTestContext);
verify(this.postProcessor).inject(this.fieldCaptor.capture(), eq(instance), any(MockDefinition.class));
then(this.postProcessor).should().inject(this.fieldCaptor.capture(), eq(instance), any(MockDefinition.class));
assertThat(this.fieldCaptor.getValue().getName()).isEqualTo("mockBean");
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2020 the original author or authors.
* Copyright 2012-2021 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.
@@ -36,12 +36,13 @@ import org.springframework.util.ReflectionUtils;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.verify;
import static org.mockito.BDDMockito.then;
/**
* Tests for {@link QualifierDefinition}.
*
* @author Phillip Webb
* @author Yanming Zhou
*/
@ExtendWith(MockitoExtension.class)
class QualifierDefinitionTests {
@@ -81,7 +82,7 @@ class QualifierDefinitionTests {
Field field = ReflectionUtils.findField(ConfigA.class, "directQualifier");
QualifierDefinition qualifierDefinition = QualifierDefinition.forElement(field);
qualifierDefinition.matches(this.beanFactory, "bean");
verify(this.beanFactory).isAutowireCandidate(eq("bean"), this.descriptorCaptor.capture());
then(this.beanFactory).should().isAutowireCandidate(eq("bean"), this.descriptorCaptor.capture());
assertThat(this.descriptorCaptor.getValue().getAnnotatedElement()).isEqualTo(field);
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2019 the original author or authors.
* Copyright 2012-2021 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.
@@ -27,13 +27,14 @@ import org.springframework.context.annotation.Import;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.verify;
import static org.mockito.BDDMockito.then;
/**
* Test {@link SpyBean @SpyBean} on a configuration class can be used to spy existing
* beans.
*
* @author Phillip Webb
* @author Yanming ZHou
*/
@ExtendWith(SpringExtension.class)
class SpyBeanOnConfigurationClassForExistingBeanIntegrationTests {
@@ -44,7 +45,7 @@ class SpyBeanOnConfigurationClassForExistingBeanIntegrationTests {
@Test
void testSpying() {
assertThat(this.caller.sayGreeting()).isEqualTo("I say simple");
verify(this.caller.getService()).greeting();
then(this.caller.getService()).should().greeting();
}
@Configuration(proxyBeanMethods = false)

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2019 the original author or authors.
* Copyright 2012-2021 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.
@@ -27,13 +27,14 @@ import org.springframework.context.annotation.Import;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.verify;
import static org.mockito.BDDMockito.then;
/**
* Test {@link SpyBean @SpyBean} on a configuration class can be used to inject new spy
* instances.
*
* @author Phillip Webb
* @author Yanming Zhou
*/
@ExtendWith(SpringExtension.class)
class SpyBeanOnConfigurationClassForNewBeanIntegrationTests {
@@ -44,7 +45,7 @@ class SpyBeanOnConfigurationClassForNewBeanIntegrationTests {
@Test
void testSpying() {
assertThat(this.caller.sayGreeting()).isEqualTo("I say simple");
verify(this.caller.getService()).greeting();
then(this.caller.getService()).should().greeting();
}
@Configuration(proxyBeanMethods = false)

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2019 the original author or authors.
* Copyright 2012-2021 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.
@@ -28,13 +28,14 @@ import org.springframework.context.annotation.Import;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.verify;
import static org.mockito.BDDMockito.then;
/**
* Test {@link SpyBean @SpyBean} on a field on a {@code @Configuration} class can be used
* to replace existing beans.
*
* @author Phillip Webb
* @author Yanming Zhou
*/
@ExtendWith(SpringExtension.class)
class SpyBeanOnConfigurationFieldForExistingBeanIntegrationTests {
@@ -48,7 +49,7 @@ class SpyBeanOnConfigurationFieldForExistingBeanIntegrationTests {
@Test
void testSpying() {
assertThat(this.caller.sayGreeting()).isEqualTo("I say simple");
verify(this.config.exampleService).greeting();
then(this.config.exampleService).should().greeting();
}
@Configuration(proxyBeanMethods = false)

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2019 the original author or authors.
* Copyright 2012-2021 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.
@@ -27,13 +27,14 @@ import org.springframework.context.annotation.Import;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.verify;
import static org.mockito.BDDMockito.then;
/**
* Test {@link SpyBean @SpyBean} on a field on a {@code @Configuration} class can be used
* to inject new spy instances.
*
* @author Phillip Webb
* @author Yanming Zhou
*/
@ExtendWith(SpringExtension.class)
class SpyBeanOnConfigurationFieldForNewBeanIntegrationTests {
@@ -47,7 +48,7 @@ class SpyBeanOnConfigurationFieldForNewBeanIntegrationTests {
@Test
void testSpying() {
assertThat(this.caller.sayGreeting()).isEqualTo("I say simple");
verify(this.config.exampleService).greeting();
then(this.config.exampleService).should().greeting();
}
@Configuration(proxyBeanMethods = false)

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2019 the original author or authors.
* Copyright 2012-2021 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.
@@ -27,12 +27,13 @@ import org.springframework.context.annotation.Import;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.verify;
import static org.mockito.BDDMockito.then;
/**
* Test {@link SpyBean @SpyBean} on a test class can be used to replace existing beans.
*
* @author Phillip Webb
* @author Yanming Zhou
*/
@ExtendWith(SpringExtension.class)
@SpyBean(SimpleExampleService.class)
@@ -44,7 +45,7 @@ class SpyBeanOnTestClassForExistingBeanIntegrationTests {
@Test
void testSpying() {
assertThat(this.caller.sayGreeting()).isEqualTo("I say simple");
verify(this.caller.getService()).greeting();
then(this.caller.getService()).should().greeting();
}
@Configuration(proxyBeanMethods = false)

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2019 the original author or authors.
* Copyright 2012-2021 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.
@@ -27,12 +27,13 @@ import org.springframework.context.annotation.Import;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.verify;
import static org.mockito.BDDMockito.then;
/**
* Test {@link SpyBean @SpyBean} on a test class can be used to inject new spy instances.
*
* @author Phillip Webb
* @author Yanming Zhou
*/
@ExtendWith(SpringExtension.class)
@SpyBean(SimpleExampleService.class)
@@ -44,7 +45,7 @@ class SpyBeanOnTestClassForNewBeanIntegrationTests {
@Test
void testSpying() {
assertThat(this.caller.sayGreeting()).isEqualTo("I say simple");
verify(this.caller.getService()).greeting();
then(this.caller.getService()).should().greeting();
}
@Configuration(proxyBeanMethods = false)

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2019 the original author or authors.
* Copyright 2012-2021 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.
@@ -26,7 +26,7 @@ import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.verify;
import static org.mockito.BDDMockito.then;
/**
* Test {@link SpyBean @SpyBean} on a test class field can be used to replace existing
@@ -35,6 +35,7 @@ import static org.mockito.Mockito.verify;
* application context caching.
*
* @author Phillip Webb
* @author Yanming Zhou
* @see SpyBeanOnTestFieldForExistingBeanIntegrationTests
*/
@ExtendWith(SpringExtension.class)
@@ -50,7 +51,7 @@ class SpyBeanOnTestFieldForExistingBeanCacheIntegrationTests {
@Test
void testSpying() {
assertThat(this.caller.sayGreeting()).isEqualTo("I say simple");
verify(this.caller.getService()).greeting();
then(this.caller.getService()).should().greeting();
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2019 the original author or authors.
* Copyright 2012-2021 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.
@@ -26,13 +26,14 @@ import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.verify;
import static org.mockito.BDDMockito.then;
/**
* Test {@link SpyBean @SpyBean} on a test class field can be used to replace existing
* beans.
*
* @author Phillip Webb
* @author Yanming Zhou
* @see SpyBeanOnTestFieldForExistingBeanCacheIntegrationTests
*/
@ExtendWith(SpringExtension.class)
@@ -48,7 +49,7 @@ class SpyBeanOnTestFieldForExistingBeanIntegrationTests {
@Test
void testSpying() {
assertThat(this.caller.sayGreeting()).isEqualTo("I say simple");
verify(this.caller.getService()).greeting();
then(this.caller.getService()).should().greeting();
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2019 the original author or authors.
* Copyright 2012-2021 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.
@@ -31,13 +31,14 @@ import org.springframework.context.annotation.Configuration;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.verify;
import static org.mockito.BDDMockito.then;
/**
* Test {@link SpyBean @SpyBean} on a test class field can be used to replace existing
* bean while preserving qualifiers.
*
* @author Andreas Neiser
* @author Yanming Zhou
*/
@ExtendWith(SpringExtension.class)
class SpyBeanOnTestFieldForExistingBeanWithQualifierIntegrationTests {
@@ -55,7 +56,7 @@ class SpyBeanOnTestFieldForExistingBeanWithQualifierIntegrationTests {
@Test
void testMocking() throws Exception {
this.caller.sayGreeting();
verify(this.service).greeting();
then(this.service).should().greeting();
}
@Test

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2019 the original author or authors.
* Copyright 2012-2021 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.
@@ -30,13 +30,14 @@ import org.springframework.context.annotation.Import;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.verify;
import static org.mockito.BDDMockito.then;
/**
* Test {@link SpyBean @SpyBean} on a test class field can be used to replace existing
* beans.
*
* @author Phillip Webb
* @author Yanming Zhou
* @see SpyBeanOnTestFieldForExistingBeanCacheIntegrationTests
*/
@ExtendWith(SpringExtension.class)
@@ -53,7 +54,7 @@ class SpyBeanOnTestFieldForExistingGenericBeanIntegrationTests {
@Test
void testSpying() {
assertThat(this.caller.sayGreeting()).isEqualTo("I say 123 simple");
verify(this.exampleService).greeting();
then(this.exampleService).should().greeting();
}
@Configuration(proxyBeanMethods = false)

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2019 the original author or authors.
* Copyright 2012-2021 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.
@@ -30,13 +30,14 @@ import org.springframework.context.annotation.Primary;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.verify;
import static org.mockito.BDDMockito.then;
/**
* Test {@link SpyBean @SpyBean} on a test class field can be used to inject a spy
* instance when there are multiple candidates and one is primary.
*
* @author Phillip Webb
* @author Yanming Zhou
*/
@ExtendWith(SpringExtension.class)
class SpyBeanOnTestFieldForMultipleExistingBeansWithOnePrimaryIntegrationTests {
@@ -52,7 +53,7 @@ class SpyBeanOnTestFieldForMultipleExistingBeansWithOnePrimaryIntegrationTests {
assertThat(this.caller.sayGreeting()).isEqualTo("I say two");
assertThat(Mockito.mockingDetails(this.spy).getMockCreationSettings().getMockName().toString())
.isEqualTo("two");
verify(this.spy).greeting();
then(this.spy).should().greeting();
}
@Configuration(proxyBeanMethods = false)

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2019 the original author or authors.
* Copyright 2012-2021 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.
@@ -27,13 +27,14 @@ import org.springframework.context.annotation.Import;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.verify;
import static org.mockito.BDDMockito.then;
/**
* Test {@link SpyBean @SpyBean} on a test class field can be used to inject new spy
* instances.
*
* @author Phillip Webb
* @author Yanming Zhou
*/
@ExtendWith(SpringExtension.class)
class SpyBeanOnTestFieldForNewBeanIntegrationTests {
@@ -47,7 +48,7 @@ class SpyBeanOnTestFieldForNewBeanIntegrationTests {
@Test
void testSpying() {
assertThat(this.caller.sayGreeting()).isEqualTo("I say simple");
verify(this.caller.getService()).greeting();
then(this.caller.getService()).should().greeting();
}
@Configuration(proxyBeanMethods = false)

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2019 the original author or authors.
* Copyright 2012-2021 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.
@@ -35,14 +35,14 @@ import org.springframework.stereotype.Service;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.mockito.BDDMockito.then;
import static org.mockito.Mockito.reset;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
/**
* Test {@link SpyBean @SpyBean} when mixed with Spring AOP.
*
* @author Phillip Webb
* @author Yanming Zhou
* @see <a href="https://github.com/spring-projects/spring-boot/issues/5837">5837</a>
*/
@ExtendWith(SpringExtension.class)
@@ -54,7 +54,7 @@ class SpyBeanWithAopProxyAndNotProxyTargetAwareTests {
@Test
void verifyShouldUseProxyTarget() {
this.dateService.getDate(false);
verify(this.dateService, times(1)).getDate(false);
then(this.dateService).should().getDate(false);
assertThatExceptionOfType(UnfinishedVerificationException.class).isThrownBy(() -> reset(this.dateService));
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2019 the original author or authors.
* Copyright 2012-2021 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.
@@ -36,13 +36,13 @@ import org.springframework.test.context.junit.jupiter.SpringExtension;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.anyBoolean;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.BDDMockito.then;
/**
* Test {@link SpyBean @SpyBean} when mixed with Spring AOP.
*
* @author Phillip Webb
* @author Yanming Zhou
* @see <a href="https://github.com/spring-projects/spring-boot/issues/5837">5837</a>
*/
@ExtendWith(SpringExtension.class)
@@ -57,9 +57,9 @@ class SpyBeanWithAopProxyTests {
Thread.sleep(200);
Long d2 = this.dateService.getDate(false);
assertThat(d1).isEqualTo(d2);
verify(this.dateService, times(1)).getDate(false);
verify(this.dateService, times(1)).getDate(eq(false));
verify(this.dateService, times(1)).getDate(anyBoolean());
then(this.dateService).should().getDate(false);
then(this.dateService).should().getDate(eq(false));
then(this.dateService).should().getDate(anyBoolean());
}
@Configuration(proxyBeanMethods = false)

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2019 the original author or authors.
* Copyright 2012-2021 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.
@@ -28,13 +28,14 @@ import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.annotation.DirtiesContext.ClassMode;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import static org.mockito.Mockito.verify;
import static org.mockito.BDDMockito.then;
/**
* Integration tests for using {@link SpyBean @SpyBean} with
* {@link DirtiesContext @DirtiesContext} and {@link ClassMode#BEFORE_EACH_TEST_METHOD}.
*
* @author Andy Wilkinson
* @author Yanming Zhou
*/
@ExtendWith(SpringExtension.class)
@DirtiesContext(classMode = ClassMode.BEFORE_EACH_TEST_METHOD)
@@ -49,7 +50,7 @@ class SpyBeanWithDirtiesContextClassModeBeforeMethodIntegrationTests {
@Test
void testSpying() throws Exception {
this.caller.sayGreeting();
verify(this.exampleService).greeting();
then(this.exampleService).should().greeting();
}
@Configuration(proxyBeanMethods = false)

View File

@@ -30,12 +30,13 @@ import org.springframework.context.annotation.Import;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.verify;
import static org.mockito.BDDMockito.then;
/**
* Tests for {@link SpyBean @SpyBean} with a JDK proxy.
*
* @author Andy Wilkinson
* @author Yanming Zhou
*/
@ExtendWith(SpringExtension.class)
class SpyBeanWithJdkProxyTests {
@@ -50,7 +51,7 @@ class SpyBeanWithJdkProxyTests {
void jdkProxyCanBeSpied() throws Exception {
Example example = this.service.find("id");
assertThat(example.id).isEqualTo("id");
verify(this.repository).find("id");
then(this.repository).should().find("id");
}
@Configuration(proxyBeanMethods = false)

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2019 the original author or authors.
* Copyright 2012-2021 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.
@@ -22,13 +22,14 @@ import org.springframework.context.ApplicationContext;
import org.springframework.context.ConfigurableApplicationContext;
import static org.mockito.BDDMockito.given;
import static org.mockito.BDDMockito.then;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
/**
* Tests for {@link ApplicationContextTestUtils}.
*
* @author Stephane Nicoll
* @author Yanming Zhou
*/
class ApplicationContextTestUtilsTests {
@@ -50,10 +51,10 @@ class ApplicationContextTestUtilsTests {
given(mock.getParent()).willReturn(parent);
given(parent.getParent()).willReturn(null);
ApplicationContextTestUtils.closeAll(mock);
verify(mock).getParent();
verify(mock).close();
verify(parent).getParent();
verify(parent).close();
then(mock).should().getParent();
then(mock).should().close();
then(parent).should().getParent();
then(parent).should().close();
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2019 the original author or authors.
* Copyright 2012-2021 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.
@@ -28,8 +28,8 @@ import org.springframework.web.util.UriTemplateHandler;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
import static org.mockito.BDDMockito.given;
import static org.mockito.BDDMockito.then;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
/**
* Tests for {@link LocalHostUriTemplateHandler}.
@@ -37,6 +37,7 @@ import static org.mockito.Mockito.verify;
* @author Phillip Webb
* @author Andy Wilkinson
* @author Eddú Meléndez
* @author Yanming Zhou
*/
class LocalHostUriTemplateHandlerTests {
@@ -99,7 +100,7 @@ class LocalHostUriTemplateHandlerTests {
given(uriTemplateHandler.expand("https://localhost:8080/", uriVariables)).willReturn(uri);
LocalHostUriTemplateHandler handler = new LocalHostUriTemplateHandler(environment, "https", uriTemplateHandler);
assertThat(handler.expand("/", uriVariables)).isEqualTo(uri);
verify(uriTemplateHandler).expand("https://localhost:8080/", uriVariables);
then(uriTemplateHandler).should().expand("https://localhost:8080/", uriVariables);
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2020 the original author or authors.
* Copyright 2012-2021 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.
@@ -40,8 +40,8 @@ import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.BDDMockito.given;
import static org.mockito.BDDMockito.then;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.springframework.test.web.client.match.MockRestRequestMatchers.requestTo;
import static org.springframework.test.web.client.response.MockRestResponseCreators.withSuccess;
@@ -49,6 +49,7 @@ import static org.springframework.test.web.client.response.MockRestResponseCreat
* Tests for {@link RootUriRequestExpectationManager}.
*
* @author Phillip Webb
* @author Yanming Zhou
*/
@ExtendWith(MockitoExtension.class)
class RootUriRequestExpectationManagerTests {
@@ -85,7 +86,7 @@ class RootUriRequestExpectationManagerTests {
ExpectedCount count = ExpectedCount.once();
RequestMatcher requestMatcher = mock(RequestMatcher.class);
this.manager.expectRequest(count, requestMatcher);
verify(this.delegate).expectRequest(count, requestMatcher);
then(this.delegate).should().expectRequest(count, requestMatcher);
}
@Test
@@ -93,7 +94,7 @@ class RootUriRequestExpectationManagerTests {
ClientHttpRequest request = mock(ClientHttpRequest.class);
given(request.getURI()).willReturn(new URI("https://spring.io/test"));
this.manager.validateRequest(request);
verify(this.delegate).validateRequest(request);
then(this.delegate).should().validateRequest(request);
}
@Test
@@ -101,7 +102,7 @@ class RootUriRequestExpectationManagerTests {
ClientHttpRequest request = mock(ClientHttpRequest.class);
given(request.getURI()).willReturn(new URI(this.uri + "/hello"));
this.manager.validateRequest(request);
verify(this.delegate).validateRequest(this.requestCaptor.capture());
then(this.delegate).should().validateRequest(this.requestCaptor.capture());
HttpRequestWrapper actual = (HttpRequestWrapper) this.requestCaptor.getValue();
assertThat(actual.getRequest()).isSameAs(request);
assertThat(actual.getURI()).isEqualTo(new URI("/hello"));
@@ -120,7 +121,7 @@ class RootUriRequestExpectationManagerTests {
@Test
void resetRequestShouldDelegateToExpectationManager() {
this.manager.reset();
verify(this.delegate).reset();
then(this.delegate).should().reset();
}
@Test

View File

@@ -56,8 +56,8 @@ import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.BDDMockito.given;
import static org.mockito.BDDMockito.then;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
/**
* Tests for {@link TestRestTemplate}.
@@ -67,6 +67,7 @@ import static org.mockito.Mockito.verify;
* @author Stephane Nicoll
* @author Andy Wilkinson
* @author Kristine Jetzke
* @author Yanming Zhou
*/
class TestRestTemplateTests {
@@ -246,7 +247,7 @@ class TestRestTemplateTests {
LocalHostUriTemplateHandler uriTemplateHandler = new LocalHostUriTemplateHandler(new MockEnvironment());
template.setUriTemplateHandler(uriTemplateHandler);
template.exchange(entity, String.class);
verify(requestFactory).createRequest(eq(absoluteUri), eq(HttpMethod.GET));
then(requestFactory).should().createRequest(eq(absoluteUri), eq(HttpMethod.GET));
}
@Test
@@ -260,7 +261,7 @@ class TestRestTemplateTests {
given(requestFactory.createRequest(eq(absoluteUri), eq(HttpMethod.GET))).willReturn(request);
template.getRestTemplate().setRequestFactory(requestFactory);
template.exchange(entity, String.class);
verify(requestFactory).createRequest(eq(absoluteUri), eq(HttpMethod.GET));
then(requestFactory).should().createRequest(eq(absoluteUri), eq(HttpMethod.GET));
}
@Test
@@ -362,7 +363,7 @@ class TestRestTemplateTests {
LocalHostUriTemplateHandler uriTemplateHandler = new LocalHostUriTemplateHandler(new MockEnvironment());
template.setUriTemplateHandler(uriTemplateHandler);
callback.doWithTestRestTemplate(template, URI.create("/a/b/c.txt?param=%7Bsomething%7D"));
verify(requestFactory).createRequest(eq(absoluteUri), any(HttpMethod.class));
then(requestFactory).should().createRequest(eq(absoluteUri), any(HttpMethod.class));
}
private void assertBasicAuthorizationCredentials(TestRestTemplate testRestTemplate, String username,

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2020 the original author or authors.
* Copyright 2012-2021 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.
@@ -36,13 +36,14 @@ import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.BDDMockito.given;
import static org.mockito.BDDMockito.then;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
/**
* Tests for {@link LocalHostWebClient}.
*
* @author Phillip Webb
* @author Yanming Zhou
*/
@SuppressWarnings("resource")
@ExtendWith(MockitoExtension.class)
@@ -64,7 +65,7 @@ class LocalHostWebClientTests {
WebConnection connection = mockConnection();
client.setWebConnection(connection);
client.getPage("/test");
verify(connection).getResponse(this.requestCaptor.capture());
then(connection).should().getResponse(this.requestCaptor.capture());
assertThat(this.requestCaptor.getValue().getUrl()).isEqualTo(new URL("http://localhost:8080/test"));
}
@@ -76,7 +77,7 @@ class LocalHostWebClientTests {
WebConnection connection = mockConnection();
client.setWebConnection(connection);
client.getPage("/test");
verify(connection).getResponse(this.requestCaptor.capture());
then(connection).should().getResponse(this.requestCaptor.capture());
assertThat(this.requestCaptor.getValue().getUrl()).isEqualTo(new URL("http://localhost:8181/test"));
}

View File

@@ -38,13 +38,14 @@ import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.argThat;
import static org.mockito.BDDMockito.given;
import static org.mockito.BDDMockito.then;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
/**
* Tests for {@link LocalHostWebConnectionHtmlUnitDriver}.
*
* @author Phillip Webb
* @author Yanming Zhou
*/
@ExtendWith(MockitoExtension.class)
class LocalHostWebConnectionHtmlUnitDriverTests {
@@ -91,7 +92,7 @@ class LocalHostWebConnectionHtmlUnitDriverTests {
MockEnvironment environment = new MockEnvironment();
LocalHostWebConnectionHtmlUnitDriver driver = new TestLocalHostWebConnectionHtmlUnitDriver(environment);
driver.get("/test");
verify(this.webClient).getPage(any(WebWindow.class), requestToUrl(new URL("http://localhost:8080/test")));
then(this.webClient).should().getPage(any(WebWindow.class), requestToUrl(new URL("http://localhost:8080/test")));
}
@Test
@@ -100,7 +101,7 @@ class LocalHostWebConnectionHtmlUnitDriverTests {
environment.setProperty("local.server.port", "8181");
LocalHostWebConnectionHtmlUnitDriver driver = new TestLocalHostWebConnectionHtmlUnitDriver(environment);
driver.get("/test");
verify(this.webClient).getPage(any(WebWindow.class), requestToUrl(new URL("http://localhost:8181/test")));
then(this.webClient).should().getPage(any(WebWindow.class), requestToUrl(new URL("http://localhost:8181/test")));
}
private WebRequest requestToUrl(URL url) {

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2019 the original author or authors.
* Copyright 2012-2021 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.
@@ -36,13 +36,14 @@ import org.springframework.test.web.reactive.server.WebTestClient;
import org.springframework.test.web.reactive.server.WebTestClient.Builder;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.BDDMockito.then;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
/**
* Integration test for {@link WebTestClientContextCustomizer}.
*
* @author Phillip Webb
* @author Yanming Zhou
*/
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT, properties = "spring.main.web-application-type=reactive")
@DirtiesContext
@@ -56,7 +57,7 @@ class WebTestClientContextCustomizerIntegrationTests {
@Test
void test() {
verify(this.clientBuilderCustomizer).customize(any(Builder.class));
then(this.clientBuilderCustomizer).should().customize(any(Builder.class));
this.webTestClient.get().uri("/").exchange().expectBody(String.class).isEqualTo("hello");
}