Assert instances (vs classes) in MockMvc/WebTestClient

Issue: SPR-16520
This commit is contained in:
Rossen Stoyanchev 2018-02-21 14:01:53 -05:00
parent 3bfa56dff2
commit bb8cddda23
4 changed files with 40 additions and 8 deletions

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2017 the original author or authors.
* Copyright 2002-2018 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.
@ -56,16 +56,21 @@ class DefaultControllerSpec extends AbstractMockServerSpec<WebTestClient.Control
DefaultControllerSpec(Object... controllers) {
Assert.isTrue(!ObjectUtils.isEmpty(controllers), "At least one controller is required");
Assert.isTrue(checkInstances(controllers), "Controller instances are required");
this.controllers = Arrays.asList(controllers);
}
@Override
public DefaultControllerSpec controllerAdvice(Object... controllerAdvice) {
Assert.isTrue(checkInstances(controllerAdvice), "ControllerAdvice instances are required");
this.controllerAdvice.addAll(Arrays.asList(controllerAdvice));
return this;
}
private boolean checkInstances(Object[] objects) {
return Arrays.stream(objects).noneMatch(Class.class::isInstance);
}
@Override
public DefaultControllerSpec contentTypeResolver(Consumer<RequestedContentTypeResolverBuilder> consumer) {
this.configurer.contentTypeResolverConsumer = consumer;

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2017 the original author or authors.
* Copyright 2002-2018 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.
@ -143,6 +143,7 @@ public class StandaloneMockMvcBuilder extends AbstractMockMvcBuilder<StandaloneM
*/
protected StandaloneMockMvcBuilder(Object... controllers) {
Assert.isTrue(!ObjectUtils.isEmpty(controllers), "At least one controller is required");
Assert.isTrue(checkInstances(controllers), "Controller instances are required");
this.controllers = Arrays.asList(controllers);
}
@ -157,10 +158,15 @@ public class StandaloneMockMvcBuilder extends AbstractMockMvcBuilder<StandaloneM
* @since 4.2
*/
public StandaloneMockMvcBuilder setControllerAdvice(Object... controllerAdvice) {
Assert.isTrue(checkInstances(controllerAdvice), "ControllerAdvice instances are required");
this.controllerAdvice = Arrays.asList(controllerAdvice);
return this;
}
private boolean checkInstances(Object[] objects) {
return Arrays.stream(objects).noneMatch(Class.class::isInstance);
}
/**
* Set the message converters to use in argument resolvers and in return value
* handlers, which support reading and/or writing to the body of the request

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2017 the original author or authors.
* Copyright 2002-2018 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.
@ -44,7 +44,7 @@ import static org.junit.Assert.assertNotNull;
public class DefaultControllerSpecTests {
@Test
public void controller() throws Exception {
public void controller() {
new DefaultControllerSpec(new MyController()).build()
.get().uri("/")
.exchange()
@ -53,7 +53,7 @@ public class DefaultControllerSpecTests {
}
@Test
public void controllerAdvice() throws Exception {
public void controllerAdvice() {
new DefaultControllerSpec(new MyController())
.controllerAdvice(new MyControllerAdvice())
.build()
@ -63,8 +63,18 @@ public class DefaultControllerSpecTests {
.expectBody(String.class).isEqualTo("Handled exception");
}
@Test(expected = IllegalArgumentException.class)
public void controllerIsAnObjectInstance() {
new DefaultControllerSpec(MyController.class);
}
@Test(expected = IllegalArgumentException.class)
public void controllerAdviceIsAnObjectInstance() {
new DefaultControllerSpec(new MyController()).controllerAdvice(MyControllerAdvice.class);
}
@Test
public void configurerConsumers() throws Exception {
public void configurerConsumers() {
TestConsumer<ArgumentResolverConfigurer> argumentResolverConsumer = new TestConsumer<>();
TestConsumer<RequestedContentTypeResolverBuilder> contenTypeResolverConsumer = new TestConsumer<>();

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2016 the original author or authors.
* Copyright 2002-2018 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.
@ -125,6 +125,17 @@ public class StandaloneMockMvcBuilderTests {
assertNotNull(serializer);
}
@Test(expected = IllegalArgumentException.class)
public void controllerIsAnObjectInstance() {
new StandaloneMockMvcBuilder(PersonController.class);
}
@Test(expected = IllegalArgumentException.class)
public void controllerAdviceIsAnObjectInstance() {
new StandaloneMockMvcBuilder(new PersonController()).setControllerAdvice(PersonController.class);
}
@Controller
private static class PlaceholderController {