cyrilantony
cyrilantony

Reputation: 294

How to set environment-variable in SpringBootTest for logback?

How to set environment variable for SpringBootTest for logback configuration?
Logback configuration error occurs when using a environment variable in logback.xml.

In logback.xml,

<appender name="plainLogsToFile" class="ch.qos.logback.core.rolling.RollingFileAppender">
    <file>/${LOG_PATH}/service.log</file>

This is the test-class:

@RunWith(SpringRunner.class)
@SpringBootTest(classes = Application.class)
public class MyTestClass {
//tests here
}

Following exception is reported:

openFile(/LOG_PATH_IS_UNDEFINED/service.log,true) call failed. java.io.FileNotFoundException: /LOG_PATH_IS_UNDEFINED/service.log (No such file or directory)
    at org.springframework.boot.logging.logback.LogbackLoggingSystem.loadConfiguration(LogbackLoggingSystem.java:162)
    at org.springframework.boot.logging.logback.LogbackLoggingSystem.reinitialize(LogbackLoggingSystem.java:208)
    at org.springframework.boot.logging.AbstractLoggingSystem.initializeWithConventions(AbstractLoggingSystem.java:74)
    at org.springframework.boot.logging.AbstractLoggingSystem.initialize(AbstractLoggingSystem.java:59)
    at org.springframework.boot.logging.logback.LogbackLoggingSystem.initialize(LogbackLoggingSystem.java:115)
    at org.springframework.boot.logging.LoggingApplicationListener.initializeSystem(LoggingApplicationListener.java:303)
    at org.springframework.boot.logging.LoggingApplicationListener.initialize(LoggingApplicationListener.java:276)
    at org.springframework.boot.logging.LoggingApplicationListener.onApplicationEnvironmentPreparedEvent(LoggingApplicationListener.java:239)
    at org.springframework.boot.logging.LoggingApplicationListener.onApplicationEvent(LoggingApplicationListener.java:212)
    at org.springframework.context.event.SimpleApplicationEventMulticaster.doInvokeListener(SimpleApplicationEventMulticaster.java:172)

I used ways to set property values explicitly using @TestPropertySource , @ActiveProfiles("test") , @SpringBootTest(... , properties ={}) .
But, logback only refers system environment variables.
I am concerned only about test execution.
Some way to ignore the logback also appreciated.

Upvotes: 0

Views: 3936

Answers (1)

rieckpil
rieckpil

Reputation: 12021

Using either @Before, @BeforeAll or a static code block is too late to configure such variables for Logback.

You can solve this with a custom JUnit 5 extension:

public class PropertyExtension implements BeforeAllCallback {

  @Override
  public void beforeAll(ExtensionContext context) {
    System.out.println("Setting system property");
    System.setProperty("LOG_PATH", "/my/logpath/for/testing");
  }

}

and use it within your test like:

@ExtendWith(PropertyExtension.class)
@SpringBootTest(classes = Application.class)
public class MyTestClass {

}

You can achieve the same with JUnit 4 with a custom ClassRule. Read more on this here.

In addition, you might also set a default for such variables:

<appender name="plainLogsToFile" class="ch.qos.logback.core.rolling.RollingFileAppender">
    <file>/${LOG_PATH:-/tmp/default}/service.log</file>
</appender>

Upvotes: 1

Related Questions