Skip to content

Commit bb8cddd

Browse files
committed
Assert instances (vs classes) in MockMvc/WebTestClient
Issue: SPR-16520
1 parent 3bfa56d commit bb8cddd

File tree

4 files changed

+40
-8
lines changed

4 files changed

+40
-8
lines changed

spring-test/src/main/java/org/springframework/test/web/reactive/server/DefaultControllerSpec.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2017 the original author or authors.
2+
* Copyright 2002-2018 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.
@@ -56,16 +56,21 @@ class DefaultControllerSpec extends AbstractMockServerSpec<WebTestClient.Control
5656

5757
DefaultControllerSpec(Object... controllers) {
5858
Assert.isTrue(!ObjectUtils.isEmpty(controllers), "At least one controller is required");
59+
Assert.isTrue(checkInstances(controllers), "Controller instances are required");
5960
this.controllers = Arrays.asList(controllers);
6061
}
6162

62-
6363
@Override
6464
public DefaultControllerSpec controllerAdvice(Object... controllerAdvice) {
65+
Assert.isTrue(checkInstances(controllerAdvice), "ControllerAdvice instances are required");
6566
this.controllerAdvice.addAll(Arrays.asList(controllerAdvice));
6667
return this;
6768
}
6869

70+
private boolean checkInstances(Object[] objects) {
71+
return Arrays.stream(objects).noneMatch(Class.class::isInstance);
72+
}
73+
6974
@Override
7075
public DefaultControllerSpec contentTypeResolver(Consumer<RequestedContentTypeResolverBuilder> consumer) {
7176
this.configurer.contentTypeResolverConsumer = consumer;

spring-test/src/main/java/org/springframework/test/web/servlet/setup/StandaloneMockMvcBuilder.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2017 the original author or authors.
2+
* Copyright 2002-2018 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.
@@ -143,6 +143,7 @@ public class StandaloneMockMvcBuilder extends AbstractMockMvcBuilder<StandaloneM
143143
*/
144144
protected StandaloneMockMvcBuilder(Object... controllers) {
145145
Assert.isTrue(!ObjectUtils.isEmpty(controllers), "At least one controller is required");
146+
Assert.isTrue(checkInstances(controllers), "Controller instances are required");
146147
this.controllers = Arrays.asList(controllers);
147148
}
148149

@@ -157,10 +158,15 @@ protected StandaloneMockMvcBuilder(Object... controllers) {
157158
* @since 4.2
158159
*/
159160
public StandaloneMockMvcBuilder setControllerAdvice(Object... controllerAdvice) {
161+
Assert.isTrue(checkInstances(controllerAdvice), "ControllerAdvice instances are required");
160162
this.controllerAdvice = Arrays.asList(controllerAdvice);
161163
return this;
162164
}
163165

166+
private boolean checkInstances(Object[] objects) {
167+
return Arrays.stream(objects).noneMatch(Class.class::isInstance);
168+
}
169+
164170
/**
165171
* Set the message converters to use in argument resolvers and in return value
166172
* handlers, which support reading and/or writing to the body of the request

spring-test/src/test/java/org/springframework/test/web/reactive/server/DefaultControllerSpecTests.java

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2017 the original author or authors.
2+
* Copyright 2002-2018 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.
@@ -44,7 +44,7 @@
4444
public class DefaultControllerSpecTests {
4545

4646
@Test
47-
public void controller() throws Exception {
47+
public void controller() {
4848
new DefaultControllerSpec(new MyController()).build()
4949
.get().uri("/")
5050
.exchange()
@@ -53,7 +53,7 @@ public void controller() throws Exception {
5353
}
5454

5555
@Test
56-
public void controllerAdvice() throws Exception {
56+
public void controllerAdvice() {
5757
new DefaultControllerSpec(new MyController())
5858
.controllerAdvice(new MyControllerAdvice())
5959
.build()
@@ -63,8 +63,18 @@ public void controllerAdvice() throws Exception {
6363
.expectBody(String.class).isEqualTo("Handled exception");
6464
}
6565

66+
@Test(expected = IllegalArgumentException.class)
67+
public void controllerIsAnObjectInstance() {
68+
new DefaultControllerSpec(MyController.class);
69+
}
70+
71+
@Test(expected = IllegalArgumentException.class)
72+
public void controllerAdviceIsAnObjectInstance() {
73+
new DefaultControllerSpec(new MyController()).controllerAdvice(MyControllerAdvice.class);
74+
}
75+
6676
@Test
67-
public void configurerConsumers() throws Exception {
77+
public void configurerConsumers() {
6878

6979
TestConsumer<ArgumentResolverConfigurer> argumentResolverConsumer = new TestConsumer<>();
7080
TestConsumer<RequestedContentTypeResolverBuilder> contenTypeResolverConsumer = new TestConsumer<>();

spring-test/src/test/java/org/springframework/test/web/servlet/setup/StandaloneMockMvcBuilderTests.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2016 the original author or authors.
2+
* Copyright 2002-2018 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.
@@ -125,6 +125,17 @@ public void springHandlerInstantiator() {
125125
assertNotNull(serializer);
126126
}
127127

128+
@Test(expected = IllegalArgumentException.class)
129+
public void controllerIsAnObjectInstance() {
130+
new StandaloneMockMvcBuilder(PersonController.class);
131+
}
132+
133+
@Test(expected = IllegalArgumentException.class)
134+
public void controllerAdviceIsAnObjectInstance() {
135+
new StandaloneMockMvcBuilder(new PersonController()).setControllerAdvice(PersonController.class);
136+
}
137+
138+
128139

129140
@Controller
130141
private static class PlaceholderController {

0 commit comments

Comments
 (0)