Skip to content

Commit ca15d17

Browse files
committed
Enforce using BDDMockito
1. Replace Mockito.verify*() with BDDMockito.then() 2. Replace Mockito.doReturn() with BDDMockito.willReturn() 3. Adjust checkstyle rule
1 parent 3f7b1f7 commit ca15d17

File tree

195 files changed

+1358
-1200
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

195 files changed

+1358
-1200
lines changed

spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/cloudfoundry/servlet/CloudFoundrySecurityInterceptorTests.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2020 the original author or authors.
2+
* Copyright 2012-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -35,12 +35,13 @@
3535

3636
import static org.assertj.core.api.Assertions.assertThat;
3737
import static org.mockito.BDDMockito.given;
38-
import static org.mockito.Mockito.verify;
38+
import static org.mockito.BDDMockito.then;
3939

4040
/**
4141
* Tests for {@link CloudFoundrySecurityInterceptor}.
4242
*
4343
* @author Madhura Bhave
44+
* @author Yanming Zhou
4445
*/
4546
@ExtendWith(MockitoExtension.class)
4647
class CloudFoundrySecurityInterceptorTests {
@@ -115,7 +116,7 @@ void preHandleSuccessfulWithFullAccess() {
115116
given(this.securityService.getAccessLevel(accessToken, "my-app-id")).willReturn(AccessLevel.FULL);
116117
SecurityResponse response = this.interceptor.preHandle(this.request, EndpointId.of("test"));
117118
ArgumentCaptor<Token> tokenArgumentCaptor = ArgumentCaptor.forClass(Token.class);
118-
verify(this.tokenValidator).validate(tokenArgumentCaptor.capture());
119+
then(this.tokenValidator).should().validate(tokenArgumentCaptor.capture());
119120
Token token = tokenArgumentCaptor.getValue();
120121
assertThat(token.toString()).isEqualTo(accessToken);
121122
assertThat(response.getStatus()).isEqualTo(HttpStatus.OK);
@@ -129,7 +130,7 @@ void preHandleSuccessfulWithRestrictedAccess() {
129130
given(this.securityService.getAccessLevel(accessToken, "my-app-id")).willReturn(AccessLevel.RESTRICTED);
130131
SecurityResponse response = this.interceptor.preHandle(this.request, EndpointId.of("info"));
131132
ArgumentCaptor<Token> tokenArgumentCaptor = ArgumentCaptor.forClass(Token.class);
132-
verify(this.tokenValidator).validate(tokenArgumentCaptor.capture());
133+
then(this.tokenValidator).should().validate(tokenArgumentCaptor.capture());
133134
Token token = tokenArgumentCaptor.getValue();
134135
assertThat(token.toString()).isEqualTo(accessToken);
135136
assertThat(response.getStatus()).isEqualTo(HttpStatus.OK);

spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/cloudfoundry/servlet/TokenValidatorTests.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333
import org.junit.jupiter.api.Test;
3434
import org.junit.jupiter.api.extension.ExtendWith;
3535
import org.mockito.Mock;
36-
import org.mockito.Mockito;
3736
import org.mockito.junit.jupiter.MockitoExtension;
3837

3938
import org.springframework.boot.actuate.autoconfigure.cloudfoundry.CloudFoundryAuthorizationException;
@@ -46,12 +45,14 @@
4645
import static org.assertj.core.api.Assertions.assertThat;
4746
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
4847
import static org.mockito.BDDMockito.given;
49-
import static org.mockito.Mockito.verify;
48+
import static org.mockito.BDDMockito.then;
49+
import static org.mockito.Mockito.never;
5050

5151
/**
5252
* Tests for {@link TokenValidator}.
5353
*
5454
* @author Madhura Bhave
55+
* @author Yanming Zhou
5556
*/
5657
@ExtendWith(MockitoExtension.class)
5758
class TokenValidatorTests {
@@ -109,7 +110,7 @@ void validateTokenWhenKidValidationSucceedsInTheSecondAttempt() throws Exception
109110
String header = "{ \"alg\": \"RS256\", \"kid\": \"valid-key\",\"typ\": \"JWT\"}";
110111
String claims = "{ \"exp\": 2147483647, \"iss\": \"http://localhost:8080/uaa/oauth/token\", \"scope\": [\"actuator.read\"]}";
111112
this.tokenValidator.validate(new Token(getSignedToken(header.getBytes(), claims.getBytes())));
112-
verify(this.securityService).fetchTokenKeys();
113+
then(this.securityService).should().fetchTokenKeys();
113114
}
114115

115116
@Test
@@ -119,7 +120,7 @@ void validateTokenShouldFetchTokenKeysIfNull() throws Exception {
119120
String header = "{ \"alg\": \"RS256\", \"kid\": \"valid-key\",\"typ\": \"JWT\"}";
120121
String claims = "{ \"exp\": 2147483647, \"iss\": \"http://localhost:8080/uaa/oauth/token\", \"scope\": [\"actuator.read\"]}";
121122
this.tokenValidator.validate(new Token(getSignedToken(header.getBytes(), claims.getBytes())));
122-
verify(this.securityService).fetchTokenKeys();
123+
then(this.securityService).should().fetchTokenKeys();
123124
}
124125

125126
@Test
@@ -129,7 +130,7 @@ void validateTokenWhenValidShouldNotFetchTokenKeys() throws Exception {
129130
String header = "{ \"alg\": \"RS256\", \"kid\": \"valid-key\",\"typ\": \"JWT\"}";
130131
String claims = "{ \"exp\": 2147483647, \"iss\": \"http://localhost:8080/uaa/oauth/token\", \"scope\": [\"actuator.read\"]}";
131132
this.tokenValidator.validate(new Token(getSignedToken(header.getBytes(), claims.getBytes())));
132-
verify(this.securityService, Mockito.never()).fetchTokenKeys();
133+
then(this.securityService).should(never()).fetchTokenKeys();
133134
}
134135

135136
@Test

spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/endpoint/jmx/JmxEndpointAutoConfigurationTests.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,14 @@
3535
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
3636

3737
import static org.assertj.core.api.Assertions.assertThat;
38+
import static org.mockito.BDDMockito.then;
3839
import static org.mockito.Mockito.mock;
39-
import static org.mockito.Mockito.verify;
4040

4141
/**
4242
* Tests for {@link JmxEndpointAutoConfiguration}.
4343
*
4444
* @author Stephane Nicoll
45+
* @author Yanming Zhou
4546
*/
4647
class JmxEndpointAutoConfigurationTests {
4748

@@ -72,7 +73,7 @@ void jmxEndpointWithCustomEndpointObjectNameFactory() {
7273
.withBean(EndpointObjectNameFactory.class, () -> factory).run((context) -> {
7374
ArgumentCaptor<ExposableJmxEndpoint> argumentCaptor = ArgumentCaptor
7475
.forClass(ExposableJmxEndpoint.class);
75-
verify(factory).getObjectName(argumentCaptor.capture());
76+
then(factory).should().getObjectName(argumentCaptor.capture());
7677
ExposableJmxEndpoint jmxEndpoint = argumentCaptor.getValue();
7778
assertThat(jmxEndpoint.getEndpointId().toLowerCaseString()).isEqualTo("test");
7879
});

spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/endpoint/web/documentation/AuditEventsEndpointDocumentationTests.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2019 the original author or authors.
2+
* Copyright 2012-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -33,7 +33,7 @@
3333

3434
import static org.mockito.ArgumentMatchers.any;
3535
import static org.mockito.BDDMockito.given;
36-
import static org.mockito.Mockito.verify;
36+
import static org.mockito.BDDMockito.then;
3737
import static org.springframework.restdocs.mockmvc.MockMvcRestDocumentation.document;
3838
import static org.springframework.restdocs.payload.PayloadDocumentation.fieldWithPath;
3939
import static org.springframework.restdocs.payload.PayloadDocumentation.responseFields;
@@ -46,6 +46,7 @@
4646
* Tests for generating documentation describing {@link AuditEventsEndpoint}.
4747
*
4848
* @author Andy Wilkinson
49+
* @author Yanming Zhou
4950
*/
5051
class AuditEventsEndpointDocumentationTests extends MockMvcEndpointDocumentationTests {
5152

@@ -83,7 +84,7 @@ void filteredAuditEvents() throws Exception {
8384
"Restricts the events to those with the given principal. Optional."),
8485
parameterWithName("type")
8586
.description("Restricts the events to those with the given type. Optional."))));
86-
verify(this.repository).find("alice", now.toInstant(), "logout");
87+
then(this.repository).should().find("alice", now.toInstant(), "logout");
8788
}
8889

8990
@Configuration(proxyBeanMethods = false)

spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/endpoint/web/documentation/LoggersEndpointDocumentationTests.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2019 the original author or authors.
2+
* Copyright 2012-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -40,7 +40,7 @@
4040
import org.springframework.restdocs.payload.JsonFieldType;
4141

4242
import static org.mockito.BDDMockito.given;
43-
import static org.mockito.Mockito.verify;
43+
import static org.mockito.BDDMockito.then;
4444
import static org.springframework.restdocs.payload.PayloadDocumentation.fieldWithPath;
4545
import static org.springframework.restdocs.payload.PayloadDocumentation.requestFields;
4646
import static org.springframework.restdocs.payload.PayloadDocumentation.responseFields;
@@ -52,6 +52,7 @@
5252
* Tests for generating documentation describing the {@link LoggersEndpoint}.
5353
*
5454
* @author Andy Wilkinson
55+
* @author Yanming Zhou
5556
*/
5657
class LoggersEndpointDocumentationTests extends MockMvcEndpointDocumentationTests {
5758

@@ -114,7 +115,7 @@ void setLogLevel() throws Exception {
114115
.andExpect(status().isNoContent())
115116
.andDo(MockMvcRestDocumentation.document("loggers/set", requestFields(fieldWithPath("configuredLevel")
116117
.description("Level for the logger. May be omitted to clear the level.").optional())));
117-
verify(this.loggingSystem).setLogLevel("com.example", LogLevel.DEBUG);
118+
then(this.loggingSystem).should().setLogLevel("com.example", LogLevel.DEBUG);
118119
}
119120

120121
@Test
@@ -127,8 +128,8 @@ void setLogLevelOfLoggerGroup() throws Exception {
127128
requestFields(fieldWithPath("configuredLevel").description(
128129
"Level for the logger group. May be omitted to clear the level of the loggers.")
129130
.optional())));
130-
verify(this.loggingSystem).setLogLevel("test.member1", LogLevel.DEBUG);
131-
verify(this.loggingSystem).setLogLevel("test.member2", LogLevel.DEBUG);
131+
then(this.loggingSystem).should().setLogLevel("test.member1", LogLevel.DEBUG);
132+
then(this.loggingSystem).should().setLogLevel("test.member2", LogLevel.DEBUG);
132133
resetLogger();
133134
}
134135

@@ -142,7 +143,7 @@ void clearLogLevel() throws Exception {
142143
this.mockMvc
143144
.perform(post("/actuator/loggers/com.example").content("{}").contentType(MediaType.APPLICATION_JSON))
144145
.andExpect(status().isNoContent()).andDo(MockMvcRestDocumentation.document("loggers/clear"));
145-
verify(this.loggingSystem).setLogLevel("com.example", null);
146+
then(this.loggingSystem).should().setLogLevel("com.example", null);
146147
}
147148

148149
@Configuration(proxyBeanMethods = false)

spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/endpoint/web/documentation/SessionsEndpointDocumentationTests.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2020 the original author or authors.
2+
* Copyright 2012-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -38,7 +38,7 @@
3838
import org.springframework.test.context.TestPropertySource;
3939

4040
import static org.mockito.BDDMockito.given;
41-
import static org.mockito.Mockito.verify;
41+
import static org.mockito.BDDMockito.then;
4242
import static org.springframework.restdocs.mockmvc.MockMvcRestDocumentation.document;
4343
import static org.springframework.restdocs.payload.PayloadDocumentation.fieldWithPath;
4444
import static org.springframework.restdocs.payload.PayloadDocumentation.responseFields;
@@ -52,6 +52,7 @@
5252
* Tests for generating documentation describing the {@link ShutdownEndpoint}.
5353
*
5454
* @author Andy Wilkinson
55+
* @author Yanming Zhou
5556
*/
5657
@TestPropertySource(properties = "spring.jackson.serialization.write-dates-as-timestamps=false")
5758
class SessionsEndpointDocumentationTests extends MockMvcEndpointDocumentationTests {
@@ -102,7 +103,7 @@ void sessionWithId() throws Exception {
102103
void deleteASession() throws Exception {
103104
this.mockMvc.perform(delete("/actuator/sessions/{id}", sessionTwo.getId())).andExpect(status().isNoContent())
104105
.andDo(document("sessions/delete"));
105-
verify(this.sessionRepository).deleteById(sessionTwo.getId());
106+
then(this.sessionRepository).should().deleteById(sessionTwo.getId());
106107
}
107108

108109
private static MapSession createSession(Instant creationTime, Instant lastAccessedTime) {

spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/metrics/MeterRegistryConfigurerTests.java

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2020 the original author or authors.
2+
* Copyright 2012-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -35,16 +35,16 @@
3535

3636
import static org.assertj.core.api.Assertions.assertThat;
3737
import static org.mockito.BDDMockito.given;
38+
import static org.mockito.BDDMockito.then;
3839
import static org.mockito.Mockito.inOrder;
3940
import static org.mockito.Mockito.mock;
40-
import static org.mockito.Mockito.verify;
41-
import static org.mockito.Mockito.verifyNoInteractions;
4241

4342
/**
4443
* Tests for {@link MeterRegistryConfigurer}.
4544
*
4645
* @author Phillip Webb
4746
* @author Andy Wilkinson
47+
* @author Yanming Zhou
4848
*/
4949
@ExtendWith(MockitoExtension.class)
5050
class MeterRegistryConfigurerTests {
@@ -77,7 +77,7 @@ void configureWhenCompositeShouldApplyCustomizer() {
7777
createObjectProvider(this.filters), createObjectProvider(this.binders), false, false);
7878
CompositeMeterRegistry composite = new CompositeMeterRegistry();
7979
configurer.configure(composite);
80-
verify(this.mockCustomizer).customize(composite);
80+
then(this.mockCustomizer).should().customize(composite);
8181
}
8282

8383
@Test
@@ -87,7 +87,7 @@ void configureShouldApplyCustomizer() {
8787
MeterRegistryConfigurer configurer = new MeterRegistryConfigurer(createObjectProvider(this.customizers),
8888
createObjectProvider(this.filters), createObjectProvider(this.binders), false, false);
8989
configurer.configure(this.mockRegistry);
90-
verify(this.mockCustomizer).customize(this.mockRegistry);
90+
then(this.mockCustomizer).should().customize(this.mockRegistry);
9191
}
9292

9393
@Test
@@ -97,7 +97,7 @@ void configureShouldApplyFilter() {
9797
MeterRegistryConfigurer configurer = new MeterRegistryConfigurer(createObjectProvider(this.customizers),
9898
createObjectProvider(this.filters), createObjectProvider(this.binders), false, false);
9999
configurer.configure(this.mockRegistry);
100-
verify(this.mockConfig).meterFilter(this.mockFilter);
100+
then(this.mockConfig).should().meterFilter(this.mockFilter);
101101
}
102102

103103
@Test
@@ -107,7 +107,7 @@ void configureShouldApplyBinder() {
107107
MeterRegistryConfigurer configurer = new MeterRegistryConfigurer(createObjectProvider(this.customizers),
108108
createObjectProvider(this.filters), createObjectProvider(this.binders), false, false);
109109
configurer.configure(this.mockRegistry);
110-
verify(this.mockBinder).bindTo(this.mockRegistry);
110+
then(this.mockBinder).should().bindTo(this.mockRegistry);
111111
}
112112

113113
@Test
@@ -117,7 +117,7 @@ void configureShouldApplyBinderToComposite() {
117117
createObjectProvider(this.filters), createObjectProvider(this.binders), false, true);
118118
CompositeMeterRegistry composite = new CompositeMeterRegistry();
119119
configurer.configure(composite);
120-
verify(this.mockBinder).bindTo(composite);
120+
then(this.mockBinder).should().bindTo(composite);
121121
}
122122

123123
@Test
@@ -126,7 +126,7 @@ void configureShouldNotApplyBinderWhenCompositeExists() {
126126
MeterRegistryConfigurer configurer = new MeterRegistryConfigurer(createObjectProvider(this.customizers),
127127
createObjectProvider(this.filters), null, false, true);
128128
configurer.configure(this.mockRegistry);
129-
verifyNoInteractions(this.mockBinder);
129+
then(this.mockBinder).shouldHaveNoInteractions();
130130
}
131131

132132
@Test
@@ -139,9 +139,9 @@ void configureShouldBeCalledInOrderCustomizerFilterBinder() {
139139
createObjectProvider(this.filters), createObjectProvider(this.binders), false, false);
140140
configurer.configure(this.mockRegistry);
141141
InOrder ordered = inOrder(this.mockBinder, this.mockConfig, this.mockCustomizer);
142-
ordered.verify(this.mockCustomizer).customize(this.mockRegistry);
143-
ordered.verify(this.mockConfig).meterFilter(this.mockFilter);
144-
ordered.verify(this.mockBinder).bindTo(this.mockRegistry);
142+
then(this.mockCustomizer).should(ordered).customize(this.mockRegistry);
143+
then(this.mockConfig).should(ordered).meterFilter(this.mockFilter);
144+
then(this.mockBinder).should(ordered).bindTo(this.mockRegistry);
145145
}
146146

147147
@Test

spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/metrics/MetricsAutoConfigurationTests.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2019 the original author or authors.
2+
* Copyright 2012-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -33,13 +33,14 @@
3333
import org.springframework.test.util.ReflectionTestUtils;
3434

3535
import static org.assertj.core.api.Assertions.assertThat;
36+
import static org.mockito.BDDMockito.then;
3637
import static org.mockito.Mockito.mock;
37-
import static org.mockito.Mockito.verify;
3838

3939
/**
4040
* Tests for {@link MetricsAutoConfiguration}.
4141
*
4242
* @author Andy Wilkinson
43+
* @author Yanming Zhou
4344
*/
4445
class MetricsAutoConfigurationTests {
4546

@@ -67,8 +68,8 @@ void configuresMeterRegistries() {
6768
assertThat(filters[0].accept((Meter.Id) null)).isEqualTo(MeterFilterReply.DENY);
6869
assertThat(filters[1]).isInstanceOf(PropertiesMeterFilter.class);
6970
assertThat(filters[2].accept((Meter.Id) null)).isEqualTo(MeterFilterReply.ACCEPT);
70-
verify((MeterBinder) context.getBean("meterBinder")).bindTo(meterRegistry);
71-
verify(context.getBean(MeterRegistryCustomizer.class)).customize(meterRegistry);
71+
then((MeterBinder) context.getBean("meterBinder")).should().bindTo(meterRegistry);
72+
then(context.getBean(MeterRegistryCustomizer.class)).should().customize(meterRegistry);
7273
});
7374
}
7475

spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/metrics/data/MetricsRepositoryMethodInvocationListenerBeanPostProcessorTests.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,14 @@
2626
import org.springframework.util.function.SingletonSupplier;
2727

2828
import static org.assertj.core.api.Assertions.assertThat;
29+
import static org.mockito.BDDMockito.then;
2930
import static org.mockito.Mockito.mock;
30-
import static org.mockito.Mockito.verify;
3131

3232
/**
3333
* Tests for {@link MetricsRepositoryMethodInvocationListenerBeanPostProcessor} .
3434
*
3535
* @author Phillip Webb
36+
* @author Yanming Zhou
3637
*/
3738
class MetricsRepositoryMethodInvocationListenerBeanPostProcessorTests {
3839

@@ -49,10 +50,10 @@ void postProcessBeforeInitializationWhenRepositoryFactoryBeanSupportAddsListener
4950
assertThat(result).isSameAs(bean);
5051
ArgumentCaptor<RepositoryFactoryCustomizer> customizer = ArgumentCaptor
5152
.forClass(RepositoryFactoryCustomizer.class);
52-
verify(bean).addRepositoryFactoryCustomizer(customizer.capture());
53+
then(bean).should().addRepositoryFactoryCustomizer(customizer.capture());
5354
RepositoryFactorySupport repositoryFactory = mock(RepositoryFactorySupport.class);
5455
customizer.getValue().customize(repositoryFactory);
55-
verify(repositoryFactory).addInvocationListener(this.listener);
56+
then(repositoryFactory).should().addInvocationListener(this.listener);
5657
}
5758

5859
@Test

0 commit comments

Comments
 (0)