Skip to content

Commit

Permalink
Add test for value attribute in @ModelAttribute in WebFlux
Browse files Browse the repository at this point in the history
This complements the previous commit which tested only the `name`
attribute.

See spring-projectsgh-28423
  • Loading branch information
sbrannen committed May 9, 2022
1 parent 7dd622b commit a1c7380
Showing 1 changed file with 30 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ void supports() {
}

@Test
void resolve() {
void resolveWithInferredModelAttributeName() {
BindingResult bindingResult = createBindingResult(new Foo(), "foo");
this.bindingContext.getModel().asMap().put(BindingResult.MODEL_KEY_PREFIX + "foo", bindingResult);

Expand All @@ -82,11 +82,11 @@ void resolve() {
}

@Test
void resolveOnBindingResultAndModelAttributeWithCustomName() {
void resolveWithCustomModelAttributeNameConfiguredViaValueAttribute() {
BindingResult bindingResult = createBindingResult(new Foo(), "custom");
this.bindingContext.getModel().asMap().put(BindingResult.MODEL_KEY_PREFIX + "custom", bindingResult);

ResolvableMethod testMethod = ResolvableMethod.on(getClass()).named("handleWithCustomModelAttributeName").build();
ResolvableMethod testMethod = ResolvableMethod.on(getClass()).named("handleWithCustomModelAttributeNameViaValueAttribute").build();

MethodParameter parameter = testMethod.arg(Errors.class);
Object actual = this.resolver.resolveArgument(parameter, this.bindingContext, this.exchange)
Expand All @@ -95,9 +95,18 @@ void resolveOnBindingResultAndModelAttributeWithCustomName() {
assertThat(actual).isSameAs(bindingResult);
}

private BindingResult createBindingResult(Foo target, String name) {
DataBinder binder = this.bindingContext.createDataBinder(this.exchange, target, name);
return binder.getBindingResult();
@Test
void resolveWithCustomModelAttributeNameConfiguredViaNameAttribute() {
BindingResult bindingResult = createBindingResult(new Foo(), "custom");
this.bindingContext.getModel().asMap().put(BindingResult.MODEL_KEY_PREFIX + "custom", bindingResult);

ResolvableMethod testMethod = ResolvableMethod.on(getClass()).named("handleWithCustomModelAttributeNameViaNameAttribute").build();

MethodParameter parameter = testMethod.arg(Errors.class);
Object actual = this.resolver.resolveArgument(parameter, this.bindingContext, this.exchange)
.block(Duration.ofMillis(5000));

assertThat(actual).isSameAs(bindingResult);
}

@Test
Expand All @@ -113,11 +122,11 @@ void resolveWithMono() {
}

@Test
void resolveWithMonoOnBindingResultAndModelAttributeWithCustomName() {
void resolveWithMonoAndCustomModelAttributeNameConfiguredViaValueAttribute() {
BindingResult bindingResult = createBindingResult(new Foo(), "custom");
this.bindingContext.getModel().asMap().put(BindingResult.MODEL_KEY_PREFIX + "custom", Mono.just(bindingResult));

ResolvableMethod testMethod = ResolvableMethod.on(getClass()).named("handleWithCustomModelAttributeName").build();
ResolvableMethod testMethod = ResolvableMethod.on(getClass()).named("handleWithCustomModelAttributeNameViaValueAttribute").build();

MethodParameter parameter = testMethod.arg(Errors.class);
Object actual = this.resolver.resolveArgument(parameter, this.bindingContext, this.exchange)
Expand Down Expand Up @@ -146,6 +155,11 @@ void resolveWithBindingResultNotFound() {
"immediately after the @ModelAttribute argument");
}

private BindingResult createBindingResult(Foo target, String name) {
DataBinder binder = this.bindingContext.createDataBinder(this.exchange, target, name);
return binder.getBindingResult();
}


@SuppressWarnings("unused")
private static class Foo {
Expand Down Expand Up @@ -179,13 +193,15 @@ void handle(
}

@SuppressWarnings("unused")
void handleWithCustomModelAttributeName(
void handleWithCustomModelAttributeNameViaValueAttribute(
@ModelAttribute("custom") Foo foo,
Errors errors) {
}

@SuppressWarnings("unused")
void handleWithCustomModelAttributeNameViaNameAttribute(
@ModelAttribute(name = "custom") Foo foo,
Errors errors,
@ModelAttribute Mono<Foo> fooMono,
BindingResult bindingResult,
Mono<Errors> errorsMono,
String string) {
Errors errors) {
}

}

0 comments on commit a1c7380

Please sign in to comment.