title: In Spring Boot Test, how do I map a temporary folder to a configuration property?
body: <p>I want to do a self-cleaning test</p>
  <p>In my situation, I have one of the components depend on a directory</p>
  <pre class="lang-java prettyprint-override"><code>public class FileRepositoryManagerImpl implements ....
  
      @Value(&quot;${acme.fileRepository.basePath}&quot;)
      private File basePath; 
  }
  </code></pre>
  <p>The value is defined in the <code>application.yml</code> file, and in DEV it points to a directory under <code>build</code>.</p>
  <p>This is not the worst idea, because <code>gradle clean</code> will eventually clean up the mess the tests create.</p>
  <p>But, really, what I would like to achieve here, is to make sure that every test runs in an isolated temporary directory that is cleaned up after execution.</p>
  <p>I know that JUnit has a tool for the temporary directories. But once I have defined that directory in the scope of JUnit 4, how do I tell Spring to use that temporary directory?</p>
  <p>I <strong>tried</strong> the inner class unsuccessfully:</p>
  <pre class="lang-java prettyprint-override"><code>@RunWith(SpringRunner.class)
  @SpringBootTest(classes = { SecurityBeanOverrideConfiguration.class, App.class })
  @EnableConfigurationProperties
  public abstract class AbstractFileRepositoryManagerIntTests {
  
      private final static TemporaryFolder temporaryFolder = new TemporaryFolder();
  
      @ClassRule
      public static TemporaryFolder getTemporaryFolder()
      {
          return temporaryFolder;
      }
  
      @ConfigurationProperties(prefix = &quot;acme&quot;)
      static class Configuration
      {
  
          public FileRepository getFileRepository()
          {
              return new FileRepository();
          }
  
          static class FileRepository
          {
  
              public File basePath() throws Exception
              {
                  return temporaryFolder.newFolder(&quot;fileRepositoryBaseDir&quot;);
              }
          }
      }
  }
  </code></pre>
  <p>I was thinking about <em>tinkering</em> with the <code>Environment</code>, but what should be the correct way to inject properties programmatically in a Spring Boot test?</p>