Skip to content

Commit 934b746

Browse files
committed
Decorate all Assert implementations with @CheckReturnValue
Signed-off-by: Stefano Cordio <[email protected]>
1 parent d8d47a0 commit 934b746

File tree

9 files changed

+46
-11
lines changed

9 files changed

+46
-11
lines changed

build-plugin/spring-boot-maven-plugin/src/intTest/java/org/springframework/boot/maven/AbstractArchiveIntegrationTests.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@
3737
import org.assertj.core.api.AssertProvider;
3838
import org.assertj.core.api.ListAssert;
3939

40+
import org.springframework.lang.CheckReturnValue;
41+
4042
import static org.assertj.core.api.Assertions.assertThat;
4143
import static org.assertj.core.api.Assertions.contentOf;
4244

@@ -176,6 +178,7 @@ JarAssert doesNotHaveEntryWithNameStartingWith(String prefix) {
176178
return this;
177179
}
178180

181+
@CheckReturnValue
179182
ListAssert<String> entryNamesInPath(String path) {
180183
List<String> matches = new ArrayList<>();
181184
withJarFile((jarFile) -> withEntries(jarFile,

config/checkstyle/import-control.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
<import-control pkg="org.springframework.boot">
44
<allow pkg="io.micrometer.observation" />
55
<disallow pkg="io.micrometer" />
6+
<allow class="org.springframework.lang.CheckReturnValue" />
67
<allow class="org.springframework.lang.Contract" />
78
<disallow pkg="org.springframework.lang" />
89
<allow pkg=".*" regex="true" />

core/spring-boot-test/src/main/java/org/springframework/boot/test/context/assertj/ApplicationContextAssert.java

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
import org.assertj.core.api.AbstractObjectArrayAssert;
2727
import org.assertj.core.api.AbstractObjectAssert;
2828
import org.assertj.core.api.AbstractThrowableAssert;
29-
import org.assertj.core.api.Assertions;
3029
import org.assertj.core.api.MapAssert;
3130
import org.assertj.core.error.BasicErrorMessageFactory;
3231
import org.jspecify.annotations.Nullable;
@@ -37,6 +36,7 @@
3736
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
3837
import org.springframework.context.ApplicationContext;
3938
import org.springframework.context.ConfigurableApplicationContext;
39+
import org.springframework.lang.CheckReturnValue;
4040
import org.springframework.util.Assert;
4141

4242
import static org.assertj.core.api.Assertions.assertThat;
@@ -224,13 +224,14 @@ public ApplicationContextAssert<C> doesNotHaveBean(String name) {
224224
* @return array assertions for the bean names
225225
* @throws AssertionError if the application context did not start
226226
*/
227+
@CheckReturnValue
227228
public <T> AbstractObjectArrayAssert<?, String> getBeanNames(Class<T> type) {
228229
if (this.startupFailure != null) {
229230
throwAssertionError(contextFailedToStartWhenExpecting(this.startupFailure,
230231
"to get beans names with type:%n <%s>", type));
231232
}
232-
return Assertions.assertThat(getApplicationContext().getBeanNamesForType(type))
233-
.as("Bean names of type <%s> from <%s>", type, getApplicationContext());
233+
return assertThat(getApplicationContext().getBeanNamesForType(type)).as("Bean names of type <%s> from <%s>",
234+
type, getApplicationContext());
234235
}
235236

236237
/**
@@ -249,6 +250,7 @@ public <T> AbstractObjectArrayAssert<?, String> getBeanNames(Class<T> type) {
249250
* @throws AssertionError if the application context contains multiple beans of the
250251
* given type
251252
*/
253+
@CheckReturnValue
252254
public <T> AbstractObjectAssert<?, T> getBean(Class<T> type) {
253255
return getBean(type, Scope.INCLUDE_ANCESTORS);
254256
}
@@ -270,6 +272,7 @@ public <T> AbstractObjectAssert<?, T> getBean(Class<T> type) {
270272
* @throws AssertionError if the application context contains multiple beans of the
271273
* given type
272274
*/
275+
@CheckReturnValue
273276
public <T> AbstractObjectAssert<?, T> getBean(Class<T> type, Scope scope) {
274277
Assert.notNull(scope, "'scope' must not be null");
275278
if (this.startupFailure != null) {
@@ -284,7 +287,7 @@ public <T> AbstractObjectAssert<?, T> getBean(Class<T> type, Scope scope) {
284287
getApplicationContext(), type, names));
285288
}
286289
T bean = (name != null) ? getApplicationContext().getBean(name, type) : null;
287-
return Assertions.assertThat(bean).as("Bean of type <%s> from <%s>", type, getApplicationContext());
290+
return assertThat(bean).as("Bean of type <%s> from <%s>", type, getApplicationContext());
288291
}
289292

290293
private @Nullable String getPrimary(String[] names, Scope scope) {
@@ -330,13 +333,14 @@ private boolean isPrimary(String name, Scope scope) {
330333
* is found
331334
* @throws AssertionError if the application context did not start
332335
*/
336+
@CheckReturnValue
333337
public AbstractObjectAssert<?, Object> getBean(String name) {
334338
if (this.startupFailure != null) {
335339
throwAssertionError(
336340
contextFailedToStartWhenExpecting(this.startupFailure, "to contain a bean of name:%n <%s>", name));
337341
}
338342
Object bean = findBean(name);
339-
return Assertions.assertThat(bean).as("Bean of name <%s> from <%s>", name, getApplicationContext());
343+
return assertThat(bean).as("Bean of name <%s> from <%s>", name, getApplicationContext());
340344
}
341345

342346
/**
@@ -357,6 +361,7 @@ public AbstractObjectAssert<?, Object> getBean(String name) {
357361
* name but a different type
358362
*/
359363
@SuppressWarnings("unchecked")
364+
@CheckReturnValue
360365
public <T> AbstractObjectAssert<?, T> getBean(String name, Class<T> type) {
361366
if (this.startupFailure != null) {
362367
throwAssertionError(contextFailedToStartWhenExpecting(this.startupFailure,
@@ -368,8 +373,8 @@ public <T> AbstractObjectAssert<?, T> getBean(String name, Class<T> type) {
368373
"%nExpecting:%n <%s>%nto contain a bean of name:%n <%s> (%s)%nbut found:%n <%s> of type <%s>",
369374
getApplicationContext(), name, type, bean, bean.getClass()));
370375
}
371-
return Assertions.assertThat((T) bean)
372-
.as("Bean of name <%s> and type <%s> from <%s>", name, type, getApplicationContext());
376+
return assertThat((T) bean).as("Bean of name <%s> and type <%s> from <%s>", name, type,
377+
getApplicationContext());
373378
}
374379

375380
private @Nullable Object findBean(String name) {
@@ -395,6 +400,7 @@ public <T> AbstractObjectAssert<?, T> getBean(String name, Class<T> type) {
395400
* no beans are found
396401
* @throws AssertionError if the application context did not start
397402
*/
403+
@CheckReturnValue
398404
public <T> MapAssert<String, T> getBeans(Class<T> type) {
399405
return getBeans(type, Scope.INCLUDE_ANCESTORS);
400406
}
@@ -414,14 +420,15 @@ public <T> MapAssert<String, T> getBeans(Class<T> type) {
414420
* no beans are found
415421
* @throws AssertionError if the application context did not start
416422
*/
423+
@CheckReturnValue
417424
public <T> MapAssert<String, T> getBeans(Class<T> type, Scope scope) {
418425
Assert.notNull(scope, "'scope' must not be null");
419426
if (this.startupFailure != null) {
420427
throwAssertionError(
421428
contextFailedToStartWhenExpecting(this.startupFailure, "to get beans of type:%n <%s>", type));
422429
}
423-
return Assertions.assertThat(scope.getBeansOfType(getApplicationContext(), type))
424-
.as("Beans of type <%s> from <%s>", type, getApplicationContext());
430+
return assertThat(scope.getBeansOfType(getApplicationContext(), type)).as("Beans of type <%s> from <%s>", type,
431+
getApplicationContext());
425432
}
426433

427434
/**
@@ -434,6 +441,7 @@ public <T> MapAssert<String, T> getBeans(Class<T> type, Scope scope) {
434441
* @return assertions on the cause of the failure
435442
* @throws AssertionError if the application context started without a failure
436443
*/
444+
@CheckReturnValue
437445
public AbstractThrowableAssert<?, ? extends Throwable> getFailure() {
438446
hasFailed();
439447
return assertThat(this.startupFailure);

core/spring-boot-test/src/main/java/org/springframework/boot/test/json/JsonContentAssert.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
import org.skyscreamer.jsonassert.comparator.JSONComparator;
4141

4242
import org.springframework.core.io.Resource;
43+
import org.springframework.lang.CheckReturnValue;
4344
import org.springframework.util.ObjectUtils;
4445
import org.springframework.util.StringUtils;
4546

@@ -916,6 +917,7 @@ public JsonContentAssert doesNotHaveEmptyJsonPathValue(CharSequence expression,
916917
* @return a new assertion object whose object under test is the extracted item
917918
* @throws AssertionError if the path is not valid
918919
*/
920+
@CheckReturnValue
919921
public AbstractObjectAssert<?, Object> extractingJsonPathValue(CharSequence expression, Object... args) {
920922
return Assertions.assertThat(new JsonPathValue(expression, args).getValue(false));
921923
}
@@ -928,6 +930,7 @@ public AbstractObjectAssert<?, Object> extractingJsonPathValue(CharSequence expr
928930
* @return a new assertion object whose object under test is the extracted item
929931
* @throws AssertionError if the path is not valid or does not result in a string
930932
*/
933+
@CheckReturnValue
931934
public AbstractCharSequenceAssert<?, String> extractingJsonPathStringValue(CharSequence expression,
932935
Object... args) {
933936
return Assertions.assertThat(extractingJsonPathValue(expression, args, String.class, "a string"));
@@ -941,6 +944,7 @@ public AbstractCharSequenceAssert<?, String> extractingJsonPathStringValue(CharS
941944
* @return a new assertion object whose object under test is the extracted item
942945
* @throws AssertionError if the path is not valid or does not result in a number
943946
*/
947+
@CheckReturnValue
944948
public AbstractObjectAssert<?, Number> extractingJsonPathNumberValue(CharSequence expression, Object... args) {
945949
return Assertions.assertThat(extractingJsonPathValue(expression, args, Number.class, "a number"));
946950
}
@@ -953,6 +957,7 @@ public AbstractObjectAssert<?, Number> extractingJsonPathNumberValue(CharSequenc
953957
* @return a new assertion object whose object under test is the extracted item
954958
* @throws AssertionError if the path is not valid or does not result in a boolean
955959
*/
960+
@CheckReturnValue
956961
public AbstractBooleanAssert<?> extractingJsonPathBooleanValue(CharSequence expression, Object... args) {
957962
return Assertions.assertThat(extractingJsonPathValue(expression, args, Boolean.class, "a boolean"));
958963
}
@@ -967,6 +972,7 @@ public AbstractBooleanAssert<?> extractingJsonPathBooleanValue(CharSequence expr
967972
* @throws AssertionError if the path is not valid or does not result in an array
968973
*/
969974
@SuppressWarnings("unchecked")
975+
@CheckReturnValue
970976
public <E> ListAssert<E> extractingJsonPathArrayValue(CharSequence expression, Object... args) {
971977
return Assertions.assertThat(extractingJsonPathValue(expression, args, List.class, "an array"));
972978
}
@@ -982,6 +988,7 @@ public <E> ListAssert<E> extractingJsonPathArrayValue(CharSequence expression, O
982988
* @throws AssertionError if the path is not valid or does not result in a map
983989
*/
984990
@SuppressWarnings("unchecked")
991+
@CheckReturnValue
985992
public <K, V> MapAssert<K, V> extractingJsonPathMapValue(CharSequence expression, Object... args) {
986993
return Assertions.assertThat(extractingJsonPathValue(expression, args, Map.class, "a map"));
987994
}

core/spring-boot-test/src/main/java/org/springframework/boot/test/json/ObjectContentAssert.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
import org.assertj.core.api.Assert;
2323
import org.assertj.core.api.InstanceOfAssertFactories;
2424

25+
import org.springframework.lang.CheckReturnValue;
26+
2527
/**
2628
* AssertJ {@link Assert} for {@link ObjectContent}.
2729
*
@@ -41,6 +43,7 @@ protected ObjectContentAssert(A actual) {
4143
* allow chaining of array-specific assertions from this call.
4244
* @return an array assertion object
4345
*/
46+
@CheckReturnValue
4447
public AbstractObjectArrayAssert<?, Object> asArray() {
4548
return asInstanceOf(InstanceOfAssertFactories.ARRAY);
4649
}
@@ -50,6 +53,7 @@ public AbstractObjectArrayAssert<?, Object> asArray() {
5053
* chaining of map-specific assertions from this call.
5154
* @return a map assertion object
5255
*/
56+
@CheckReturnValue
5357
public AbstractMapAssert<?, ?, Object, Object> asMap() {
5458
return asInstanceOf(InstanceOfAssertFactories.MAP);
5559
}

system-test/spring-boot-image-system-tests/src/systemTest/java/org/springframework/boot/image/assertions/ContainerConfigAssert.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import org.assertj.core.api.ObjectAssert;
3333

3434
import org.springframework.boot.test.json.JsonContentAssert;
35+
import org.springframework.lang.CheckReturnValue;
3536

3637
/**
3738
* AssertJ {@link org.assertj.core.api.Assert} for Docker image container configuration.
@@ -89,7 +90,7 @@ protected LabelsAssert(Map<String, String> labels) {
8990
/**
9091
* Asserts for the JSON content in the {@code io.buildpacks.build.metadata} label.
9192
*
92-
* See <a href=
93+
* @see <a href=
9394
* "https://github.com/buildpacks/spec/blob/main/platform.md#iobuildpacksbuildmetadata-json">the
9495
* spec</a>
9596
*/
@@ -99,10 +100,12 @@ public static class BuildMetadataAssert extends AbstractAssert<BuildMetadataAsse
99100
super(jsonContentAssert, BuildMetadataAssert.class);
100101
}
101102

103+
@CheckReturnValue
102104
public ListAssert<Object> buildpacks() {
103105
return this.actual.extractingJsonPathArrayValue("$.buildpacks[*].id");
104106
}
105107

108+
@CheckReturnValue
106109
public AbstractListAssert<?, List<? extends String>, String, ObjectAssert<String>> processOfType(String type) {
107110
return this.actual.extractingJsonPathArrayValue("$.processes[?(@.type=='%s')]", type)
108111
.singleElement()
@@ -122,7 +125,7 @@ private Collection<String> getArgs(Object obj) {
122125
/**
123126
* Asserts for the JSON content in the {@code io.buildpacks.lifecycle.metadata} label.
124127
*
125-
* See <a href=
128+
* @see <a href=
126129
* "https://github.com/buildpacks/spec/blob/main/platform.md#iobuildpackslifecyclemetadata-json">the
127130
* spec</a>
128131
*/
@@ -132,14 +135,17 @@ public static class LifecycleMetadataAssert extends AbstractAssert<LifecycleMeta
132135
super(jsonContentAssert, LifecycleMetadataAssert.class);
133136
}
134137

138+
@CheckReturnValue
135139
public ListAssert<Object> buildpackLayers(String buildpackId) {
136140
return this.actual.extractingJsonPathArrayValue("$.buildpacks[?(@.key=='%s')].layers", buildpackId);
137141
}
138142

143+
@CheckReturnValue
139144
public AbstractListAssert<?, List<?>, Object, ObjectAssert<Object>> appLayerShas() {
140145
return this.actual.extractingJsonPathArrayValue("$.app").extracting("sha");
141146
}
142147

148+
@CheckReturnValue
143149
public AbstractObjectAssert<?, Object> sbomLayerSha() {
144150
return this.actual.extractingJsonPathValue("$.sbom.sha");
145151
}

system-test/spring-boot-image-system-tests/src/systemTest/java/org/springframework/boot/image/assertions/ImageAssert.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import org.springframework.boot.buildpack.platform.docker.type.ImageReference;
3535
import org.springframework.boot.buildpack.platform.docker.type.Layer;
3636
import org.springframework.boot.test.json.JsonContentAssert;
37+
import org.springframework.lang.CheckReturnValue;
3738
import org.springframework.util.StreamUtils;
3839

3940
/**
@@ -73,6 +74,7 @@ public LayerContentAssert(Layer layer) {
7374
super(layer, LayerContentAssert.class);
7475
}
7576

77+
@CheckReturnValue
7678
public ListAssert<String> entries() {
7779
List<String> entryNames = new ArrayList<>();
7880
try {

test-support/spring-boot-test-support/src/main/java/org/springframework/boot/testsupport/assertj/ScheduledExecutorServiceAssert.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import org.assertj.core.api.AbstractAssert;
2525
import org.assertj.core.api.Assert;
2626

27+
import org.springframework.lang.CheckReturnValue;
2728
import org.springframework.util.ReflectionUtils;
2829

2930
/**
@@ -85,6 +86,7 @@ private boolean producesVirtualThreads() {
8586
* @param actual the {@link ScheduledExecutorService}
8687
* @return the assertion instance
8788
*/
89+
@CheckReturnValue
8890
public static ScheduledExecutorServiceAssert assertThat(ScheduledExecutorService actual) {
8991
return new ScheduledExecutorServiceAssert(actual);
9092
}

test-support/spring-boot-test-support/src/main/java/org/springframework/boot/testsupport/assertj/SimpleAsyncTaskExecutorAssert.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import org.assertj.core.api.Assert;
2323

2424
import org.springframework.core.task.SimpleAsyncTaskExecutor;
25+
import org.springframework.lang.CheckReturnValue;
2526
import org.springframework.util.ReflectionUtils;
2627

2728
/**
@@ -77,6 +78,7 @@ private boolean producesVirtualThreads() {
7778
* @param actual the {@link SimpleAsyncTaskExecutor}
7879
* @return the assertion instance
7980
*/
81+
@CheckReturnValue
8082
public static SimpleAsyncTaskExecutorAssert assertThat(SimpleAsyncTaskExecutor actual) {
8183
return new SimpleAsyncTaskExecutorAssert(actual);
8284
}

0 commit comments

Comments
 (0)