Skip to content
This repository was archived by the owner on Dec 19, 2023. It is now read-only.

Commit a75c772

Browse files
Josef-Reichardtjmisur
authored andcommitted
Fixed missing handler method on error (WebTestClient) (#250)
The current implementation of `WebTestClientInitializer` only works if the rest endpoint succeeds. In case of an error (for example passing an `String` as `PathVariable` of type `int`) the handler method is not determined correctly and this leads for example to a missing headline in the `auto-section.adoc`. The reason is that spring webflux `DispatcherHandler` first calls all implementations of `HandlerAdapter` and only if they succeeds the `HandlerResultHandler` implementations (the `WebTestClientInitializer` is one of them) are called (see [`DispatcherHandler.java#L156-L161`](https://github.com/spring-projects/spring-framework/blob/v5.0.2.RELEASE/spring-webflux/src/main/java/org/springframework/web/reactive/DispatcherHandler.java#L156-L161)). To solve this issue I simply changed the `WebTestClientInitializer` to implement `HandlerAdapter` instead of `HandlerResultHandler`.
1 parent 431f76e commit a75c772

File tree

2 files changed

+15
-23
lines changed

2 files changed

+15
-23
lines changed

spring-auto-restdocs-core/src/main/java/capital/scalable/restdocs/webtestclient/WebTestClientInitializer.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,34 +31,34 @@
3131
import org.springframework.restdocs.snippet.Snippet;
3232
import org.springframework.web.method.HandlerMethod;
3333
import org.springframework.web.reactive.DispatcherHandler;
34+
import org.springframework.web.reactive.HandlerAdapter;
3435
import org.springframework.web.reactive.HandlerResult;
35-
import org.springframework.web.reactive.HandlerResultHandler;
3636
import org.springframework.web.server.ServerWebExchange;
3737
import reactor.core.publisher.Mono;
3838

3939
/**
4040
* Watches the WebTestClient and applies some attributes on each operation.
4141
*/
42-
public class WebTestClientInitializer implements HandlerResultHandler, Ordered {
42+
public class WebTestClientInitializer implements HandlerAdapter, Ordered {
4343

4444
/**
4545
* The called {@link HandlerMethod}.
4646
*/
4747
private HandlerMethod handlerMethod;
4848

4949
@Override
50-
public boolean supports(HandlerResult result) {
50+
public boolean supports(Object handler) {
5151
// if a handler method is set: remember it
52-
if (result.getHandler() instanceof HandlerMethod) {
53-
handlerMethod = (HandlerMethod) result.getHandler();
52+
if (handler instanceof HandlerMethod) {
53+
handlerMethod = (HandlerMethod) handler;
5454
}
5555
// all done - nothing more to do
5656
return false;
5757
}
5858

5959
@Override
60-
public Mono<Void> handleResult(ServerWebExchange exchange, HandlerResult result) {
61-
// nothing to do - handler already registered in #supports(HandlerResult)
60+
public Mono<HandlerResult> handle(ServerWebExchange exchange, Object handler) {
61+
// nothing to do - handler already registered in #supports(Object)
6262
return Mono.empty();
6363
}
6464

spring-auto-restdocs-core/src/test/java/capital/scalable/restdocs/webtestclient/WebTestClientInitializerTest.java

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -50,27 +50,24 @@
5050
public class WebTestClientInitializerTest {
5151

5252
/**
53-
* Test for method {@link WebTestClientInitializer#supports(HandlerResult)}.
53+
* Test for method {@link WebTestClientInitializer#supports(Object)}.
5454
*/
5555
@Test
5656
public void supports_withHandlerMethod() {
5757

5858
// prepare:
59-
HandlerResult handlerResultMock = mock(HandlerResult.class);
60-
HandlerMethod handlerMethodMock = mock(HandlerMethod.class);
61-
Mockito.when(handlerResultMock.getHandler()).thenReturn(handlerMethodMock);
59+
HandlerMethod handlerMethod = mock(HandlerMethod.class);
6260

6361
// perform:
6462
WebTestClientInitializer testInstance = new WebTestClientInitializer();
65-
boolean result = testInstance.supports(handlerResultMock);
63+
boolean result = testInstance.supports(handlerMethod);
6664

6765
// verify:
6866
assertFalse(result);
69-
verify(handlerResultMock, atLeast(1)).getHandler();
7067
}
7168

7269
/**
73-
* Test for method {@link WebTestClientInitializer#supports(HandlerResult)} in the
70+
* Test for method {@link WebTestClientInitializer#supports(Object)} in the
7471
* case that the {@linkplain HandlerResult#getHandler() handler} is no
7572
* {@link HandlerMethod}.
7673
*/
@@ -79,21 +76,18 @@ public void supports_withoutHandlerMethod_noException() {
7976

8077
// prepare:
8178
WebTestClientInitializer testInstance = new WebTestClientInitializer();
82-
HandlerResult handlerResultMock = mock(HandlerResult.class);
8379
String noHandlerMethod = "string value just for unit test";
84-
Mockito.when(handlerResultMock.getHandler()).thenReturn(noHandlerMethod);
8580

8681
// perform:
87-
boolean result = testInstance.supports(handlerResultMock);
82+
boolean result = testInstance.supports(noHandlerMethod);
8883

8984
// verify:
9085
assertFalse(result);
91-
verify(handlerResultMock, atLeast(1)).getHandler();
9286
}
9387

9488
/**
9589
* Test for method
96-
* {@link WebTestClientInitializer#handleResult(ServerWebExchange, HandlerResult)}.
90+
* {@link WebTestClientInitializer#handle(ServerWebExchange, Object)}.
9791
*/
9892
@Test
9993
public void handleResult_doNothing() {
@@ -104,7 +98,7 @@ public void handleResult_doNothing() {
10498
HandlerResult handlerResult = mock(HandlerResult.class);
10599

106100
// perform:
107-
Mono<Void> result = testInstance.handleResult(exchange, handlerResult);
101+
Mono<HandlerResult> result = testInstance.handle(exchange, handlerResult);
108102

109103
// verify:
110104
assertNull(result.block(Duration.ZERO));
@@ -139,10 +133,8 @@ public void prepareSnippets() throws IOException {
139133
when(contextMock.getBean(eq(DispatcherHandler.class)))
140134
.thenReturn(dispatcherHandlerMock);
141135
WebTestClientInitializer webTestClientInitializer = new WebTestClientInitializer();
142-
HandlerResult handlerResultMock = mock(HandlerResult.class);
143136
HandlerMethod handlerMethodMock = mock(HandlerMethod.class);
144-
Mockito.when(handlerResultMock.getHandler()).thenReturn(handlerMethodMock);
145-
webTestClientInitializer.supports(handlerResultMock);
137+
webTestClientInitializer.supports(handlerMethodMock);
146138
when(contextMock.getBean(eq(WebTestClientInitializer.class)))
147139
.thenReturn(webTestClientInitializer);
148140
when(contextMock.getBean(eq(ObjectMapper.class)))

0 commit comments

Comments
 (0)