mirror of
https://github.com/spring-projects/spring-boot.git
synced 2024-07-15 01:07:30 +08:00
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.
This commit is contained in:
parent
773dda3d55
commit
aebb60d8b8
@ -18,12 +18,15 @@ package org.springframework.boot.context.logging;
|
||||
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.rules.TemporaryFolder;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import org.springframework.beans.factory.ObjectProvider;
|
||||
import org.springframework.boot.WebApplicationType;
|
||||
import org.springframework.boot.builder.SpringApplicationBuilder;
|
||||
import org.springframework.boot.context.event.ApplicationStartingEvent;
|
||||
import org.springframework.boot.logging.LogFile;
|
||||
import org.springframework.boot.logging.LoggingSystem;
|
||||
import org.springframework.boot.testsupport.rule.OutputCapture;
|
||||
import org.springframework.context.ApplicationListener;
|
||||
@ -42,6 +45,9 @@ public class LoggingApplicationListenerIntegrationTests {
|
||||
@Rule
|
||||
public OutputCapture outputCapture = new OutputCapture();
|
||||
|
||||
@Rule
|
||||
public final TemporaryFolder temp = new TemporaryFolder();
|
||||
|
||||
@Test
|
||||
public void loggingSystemRegisteredInTheContext() {
|
||||
try (ConfigurableApplicationContext context = new SpringApplicationBuilder(SampleService.class)
|
||||
@ -51,6 +57,17 @@ public class LoggingApplicationListenerIntegrationTests {
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void logFileRegisteredInTheContextWhenApplicable() throws Exception {
|
||||
String logFile = this.temp.newFile().getAbsolutePath();
|
||||
try (ConfigurableApplicationContext context = new SpringApplicationBuilder(SampleService.class)
|
||||
.web(WebApplicationType.NONE).properties("logging.file=" + logFile).run()) {
|
||||
SampleService service = context.getBean(SampleService.class);
|
||||
assertThat(service.logFile).isNotNull();
|
||||
assertThat(service.logFile.toString()).isEqualTo(logFile);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void loggingPerformedDuringChildApplicationStartIsNotLost() {
|
||||
new SpringApplicationBuilder(Config.class).web(WebApplicationType.NONE).child(Config.class)
|
||||
@ -72,8 +89,11 @@ public class LoggingApplicationListenerIntegrationTests {
|
||||
|
||||
private final LoggingSystem loggingSystem;
|
||||
|
||||
SampleService(LoggingSystem loggingSystem) {
|
||||
private final LogFile logFile;
|
||||
|
||||
SampleService(LoggingSystem loggingSystem, ObjectProvider<LogFile> logFile) {
|
||||
this.loggingSystem = loggingSystem;
|
||||
this.logFile = logFile.getIfAvailable();
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -66,13 +66,6 @@ public class EndpointsPropertiesSampleActuatorApplicationTests {
|
||||
assertThat(entity.getBody()).contains("\"hello\":\"world\"");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void logfileWithRandomName() {
|
||||
ResponseEntity<String> entity = this.restTemplate.withBasicAuth("user", getPassword())
|
||||
.getForEntity("/admin/logfile", String.class);
|
||||
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK);
|
||||
}
|
||||
|
||||
private String getPassword() {
|
||||
return "password";
|
||||
}
|
||||
|
@ -1,5 +1,3 @@
|
||||
server.error.path: /oops
|
||||
management.endpoint.health.show-details: always
|
||||
management.endpoints.web.base-path: /admin
|
||||
logging.file=./target/${spring.application.instance_id}.log
|
||||
spring.application.instance_id=${random.value}
|
||||
server.error.path:/oops
|
||||
management.endpoint.health.show-details:always
|
||||
management.endpoints.web.base-path:/admin
|
||||
|
Loading…
Reference in New Issue
Block a user