Skip to content

Commit 26f1bbb

Browse files
committed
Merge branch '3.1.x' into 3.2.x
Closes gh-40516
2 parents 503d8c1 + 2e906c4 commit 26f1bbb

File tree

3 files changed

+37
-4
lines changed

3 files changed

+37
-4
lines changed

spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/web/servlet/SpringBootMockMvcBuilderCustomizer.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
*
5353
* @author Phillip Webb
5454
* @author Andy Wilkinson
55+
* @author Moritz Halbritter
5556
* @since 1.4.0
5657
*/
5758
public class SpringBootMockMvcBuilderCustomizer implements MockMvcBuilderCustomizer {
@@ -100,7 +101,7 @@ private LinesWriter getLinesWriter() {
100101
return null;
101102
}
102103
if (this.print == MockMvcPrint.LOG_DEBUG) {
103-
return new LoggingLinesWriter();
104+
return (LoggingLinesWriter.isDebugEnabled()) ? new LoggingLinesWriter() : null;
104105
}
105106
return new SystemLinesWriter(this.print);
106107
}
@@ -188,7 +189,12 @@ public void printValue(String label, Object value) {
188189
if (value != null && value.getClass().isArray()) {
189190
value = CollectionUtils.arrayToList(value);
190191
}
191-
this.lines.add(String.format("%17s = %s", label, value));
192+
try {
193+
this.lines.add("%17s = %s".formatted(label, value));
194+
}
195+
catch (RuntimeException ex) {
196+
this.lines.add("%17s = << Exception '%s' occurred while formatting >>".formatted(label, ex));
197+
}
192198
}
193199

194200
List<String> getLines() {
@@ -273,6 +279,10 @@ public void write(List<String> lines) {
273279
}
274280
}
275281

282+
static boolean isDebugEnabled() {
283+
return logger.isDebugEnabled();
284+
}
285+
276286
}
277287

278288
/**

spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/web/servlet/mockmvc/ExampleController1.java

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2019 the original author or authors.
2+
* Copyright 2012-2024 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.
@@ -20,11 +20,14 @@
2020
import org.springframework.stereotype.Controller;
2121
import org.springframework.web.bind.annotation.GetMapping;
2222
import org.springframework.web.bind.annotation.RestController;
23+
import org.springframework.web.context.request.RequestAttributes;
24+
import org.springframework.web.context.request.WebRequest;
2325

2426
/**
2527
* Example {@link Controller @Controller} used with {@link WebMvcTest @WebMvcTest} tests.
2628
*
2729
* @author Phillip Webb
30+
* @author Moritz Halbritter
2831
*/
2932
@RestController
3033
public class ExampleController1 {
@@ -44,4 +47,16 @@ public String html() {
4447
return "<html><body>Hello</body></html>";
4548
}
4649

50+
@GetMapping("/formatting")
51+
public String formatting(WebRequest request) {
52+
Object formattingFails = new Object() {
53+
@Override
54+
public String toString() {
55+
throw new IllegalStateException("Formatting failed");
56+
}
57+
};
58+
request.setAttribute("attribute-1", formattingFails, RequestAttributes.SCOPE_SESSION);
59+
return "formatting";
60+
}
61+
4762
}

spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/web/servlet/mockmvc/MockMvcSpringBootTestIntegrationTests.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2021 the original author or authors.
2+
* Copyright 2012-2024 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.
@@ -41,6 +41,7 @@
4141
* {@link AutoConfigureMockMvc @AutoConfigureMockMvc} (i.e. full integration test).
4242
*
4343
* @author Phillip Webb
44+
* @author Moritz Halbritter
4445
*/
4546
@SpringBootTest
4647
@AutoConfigureMockMvc(print = MockMvcPrint.SYSTEM_ERR, printOnlyOnFailure = false)
@@ -83,4 +84,11 @@ void shouldTestWithWebTestClient(@Autowired WebTestClient webTestClient) {
8384
webTestClient.get().uri("/one").exchange().expectStatus().isOk().expectBody(String.class).isEqualTo("one");
8485
}
8586

87+
@Test
88+
void shouldNotFailIfFormattingValueThrowsException(CapturedOutput output) throws Exception {
89+
this.mvc.perform(get("/formatting")).andExpect(content().string("formatting")).andExpect(status().isOk());
90+
assertThat(output).contains(
91+
"Session Attrs = << Exception 'java.lang.IllegalStateException: Formatting failed' occurred while formatting >>");
92+
}
93+
8694
}

0 commit comments

Comments
 (0)