Support WebExceptionHandler in @WebFluxTest

Closes gh-13627
This commit is contained in:
Madhura Bhave
2018-08-27 16:35:49 -07:00
parent 64a0d2f2ae
commit f3fa952cbc
4 changed files with 52 additions and 0 deletions

View File

@@ -32,6 +32,7 @@ import org.springframework.stereotype.Controller;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.reactive.config.WebFluxConfigurer;
import org.springframework.web.server.WebExceptionHandler;
/**
* {@link TypeExcludeFilter} for {@link WebFluxTest @WebFluxTest}.
@@ -49,6 +50,7 @@ class WebFluxTypeExcludeFilter extends AnnotationCustomizableTypeExcludeFilter {
includes.add(WebFluxConfigurer.class);
includes.add(Converter.class);
includes.add(GenericConverter.class);
includes.add(WebExceptionHandler.class);
DEFAULT_INCLUDES = Collections.unmodifiableSet(includes);
}

View File

@@ -36,4 +36,9 @@ public class ExampleController1 {
return Mono.just("one");
}
@GetMapping("/one/error")
public Mono<String> error() {
throw new RuntimeException("foo");
}
}

View File

@@ -0,0 +1,40 @@
/*
* Copyright 2012-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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.test.autoconfigure.web.reactive.webclient;
import reactor.core.publisher.Mono;
import org.springframework.boot.test.autoconfigure.web.reactive.WebFluxTest;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import org.springframework.web.server.WebExceptionHandler;
/**
* Example {@link WebExceptionHandler} used with {@link WebFluxTest} tests.
*
* @author Madhura Bhave
*/
@Component
public class ExampleWebExceptionHandler implements WebExceptionHandler {
@Override
public Mono<Void> handle(ServerWebExchange exchange, Throwable ex) {
exchange.getResponse().setStatusCode(HttpStatus.BAD_REQUEST);
return exchange.getResponse().setComplete();
}
}

View File

@@ -48,4 +48,9 @@ public class WebFluxTestAllControllersIntegrationTests {
.expectBody(String.class).isEqualTo("two");
}
@Test
public void webExceptionHandling() {
this.webClient.get().uri("/one/error").exchange().expectStatus().isBadRequest();
}
}