Skip to content

Commit

Permalink
Merge branch '3.2.x'
Browse files Browse the repository at this point in the history
Closes gh-40517
  • Loading branch information
mhalbritter committed Apr 25, 2024
2 parents 744bea5 + 26f1bbb commit 6bb8a11
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 4 deletions.
Expand Up @@ -52,6 +52,7 @@
*
* @author Phillip Webb
* @author Andy Wilkinson
* @author Moritz Halbritter
* @since 1.4.0
*/
public class SpringBootMockMvcBuilderCustomizer implements MockMvcBuilderCustomizer {
Expand Down Expand Up @@ -100,7 +101,7 @@ private LinesWriter getLinesWriter() {
return null;
}
if (this.print == MockMvcPrint.LOG_DEBUG) {
return new LoggingLinesWriter();
return (LoggingLinesWriter.isDebugEnabled()) ? new LoggingLinesWriter() : null;
}
return new SystemLinesWriter(this.print);
}
Expand Down Expand Up @@ -188,7 +189,12 @@ public void printValue(String label, Object value) {
if (value != null && value.getClass().isArray()) {
value = CollectionUtils.arrayToList(value);
}
this.lines.add(String.format("%17s = %s", label, value));
try {
this.lines.add("%17s = %s".formatted(label, value));
}
catch (RuntimeException ex) {
this.lines.add("%17s = << Exception '%s' occurred while formatting >>".formatted(label, ex));
}
}

List<String> getLines() {
Expand Down Expand Up @@ -273,6 +279,10 @@ public void write(List<String> lines) {
}
}

static boolean isDebugEnabled() {
return logger.isDebugEnabled();
}

}

/**
Expand Down
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2019 the original author or authors.
* Copyright 2012-2024 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.
Expand All @@ -20,11 +20,14 @@
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.WebRequest;

/**
* Example {@link Controller @Controller} used with {@link WebMvcTest @WebMvcTest} tests.
*
* @author Phillip Webb
* @author Moritz Halbritter
*/
@RestController
public class ExampleController1 {
Expand All @@ -44,4 +47,16 @@ public String html() {
return "<html><body>Hello</body></html>";
}

@GetMapping("/formatting")
public String formatting(WebRequest request) {
Object formattingFails = new Object() {
@Override
public String toString() {
throw new IllegalStateException("Formatting failed");
}
};
request.setAttribute("attribute-1", formattingFails, RequestAttributes.SCOPE_SESSION);
return "formatting";
}

}
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2021 the original author or authors.
* Copyright 2012-2024 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.
Expand Down Expand Up @@ -41,6 +41,7 @@
* {@link AutoConfigureMockMvc @AutoConfigureMockMvc} (i.e. full integration test).
*
* @author Phillip Webb
* @author Moritz Halbritter
*/
@SpringBootTest
@AutoConfigureMockMvc(print = MockMvcPrint.SYSTEM_ERR, printOnlyOnFailure = false)
Expand Down Expand Up @@ -83,4 +84,11 @@ void shouldTestWithWebTestClient(@Autowired WebTestClient webTestClient) {
webTestClient.get().uri("/one").exchange().expectStatus().isOk().expectBody(String.class).isEqualTo("one");
}

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

}

0 comments on commit 6bb8a11

Please sign in to comment.