Skip to content

Commit aebb60d

Browse files
committed
Fix tests
Setting the log file name to a random value in smoke tests doesn't work because the logger context has already been initialized.
1 parent 773dda3 commit aebb60d

File tree

3 files changed

+24
-13
lines changed

3 files changed

+24
-13
lines changed

spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/logging/LoggingApplicationListenerIntegrationTests.java

+21-1
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,15 @@
1818

1919
import org.junit.Rule;
2020
import org.junit.Test;
21+
import org.junit.rules.TemporaryFolder;
2122
import org.slf4j.Logger;
2223
import org.slf4j.LoggerFactory;
2324

25+
import org.springframework.beans.factory.ObjectProvider;
2426
import org.springframework.boot.WebApplicationType;
2527
import org.springframework.boot.builder.SpringApplicationBuilder;
2628
import org.springframework.boot.context.event.ApplicationStartingEvent;
29+
import org.springframework.boot.logging.LogFile;
2730
import org.springframework.boot.logging.LoggingSystem;
2831
import org.springframework.boot.testsupport.rule.OutputCapture;
2932
import org.springframework.context.ApplicationListener;
@@ -42,6 +45,9 @@ public class LoggingApplicationListenerIntegrationTests {
4245
@Rule
4346
public OutputCapture outputCapture = new OutputCapture();
4447

48+
@Rule
49+
public final TemporaryFolder temp = new TemporaryFolder();
50+
4551
@Test
4652
public void loggingSystemRegisteredInTheContext() {
4753
try (ConfigurableApplicationContext context = new SpringApplicationBuilder(SampleService.class)
@@ -51,6 +57,17 @@ public void loggingSystemRegisteredInTheContext() {
5157
}
5258
}
5359

60+
@Test
61+
public void logFileRegisteredInTheContextWhenApplicable() throws Exception {
62+
String logFile = this.temp.newFile().getAbsolutePath();
63+
try (ConfigurableApplicationContext context = new SpringApplicationBuilder(SampleService.class)
64+
.web(WebApplicationType.NONE).properties("logging.file=" + logFile).run()) {
65+
SampleService service = context.getBean(SampleService.class);
66+
assertThat(service.logFile).isNotNull();
67+
assertThat(service.logFile.toString()).isEqualTo(logFile);
68+
}
69+
}
70+
5471
@Test
5572
public void loggingPerformedDuringChildApplicationStartIsNotLost() {
5673
new SpringApplicationBuilder(Config.class).web(WebApplicationType.NONE).child(Config.class)
@@ -72,8 +89,11 @@ static class SampleService {
7289

7390
private final LoggingSystem loggingSystem;
7491

75-
SampleService(LoggingSystem loggingSystem) {
92+
private final LogFile logFile;
93+
94+
SampleService(LoggingSystem loggingSystem, ObjectProvider<LogFile> logFile) {
7695
this.loggingSystem = loggingSystem;
96+
this.logFile = logFile.getIfAvailable();
7797
}
7898

7999
}

spring-boot-samples/spring-boot-sample-actuator/src/test/java/sample/actuator/EndpointsPropertiesSampleActuatorApplicationTests.java

-7
Original file line numberDiff line numberDiff line change
@@ -66,13 +66,6 @@ public void testCustomContextPath() {
6666
assertThat(entity.getBody()).contains("\"hello\":\"world\"");
6767
}
6868

69-
@Test
70-
public void logfileWithRandomName() {
71-
ResponseEntity<String> entity = this.restTemplate.withBasicAuth("user", getPassword())
72-
.getForEntity("/admin/logfile", String.class);
73-
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK);
74-
}
75-
7669
private String getPassword() {
7770
return "password";
7871
}
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
server.error.path: /oops
2-
management.endpoint.health.show-details: always
3-
management.endpoints.web.base-path: /admin
4-
logging.file=./target/${spring.application.instance_id}.log
5-
spring.application.instance_id=${random.value}
1+
server.error.path:/oops
2+
management.endpoint.health.show-details:always
3+
management.endpoints.web.base-path:/admin

0 commit comments

Comments
 (0)