Enterprise Java

Mocking files for JUnit testing a Spring Boot web application on Synology NAS

For a Spring Boot application which will check backup files on a Synology RS815+ NAS, we wanted to be able to easily test the files stored on this NAS, without having to copy the 7TB that were stored on it.

Ideally we wanted to create the same file structure to use the web application in a Spring development profile, as well as use these file structures in a JUnit test.

Introducing FileStructureCreator

We started by creating a new class FileStructureCreator which looks like this:

public class FileStructureCreator implements Closeable {

  public static final Path baseTestPath = Paths.get("testFiles");
  private Path fileStructureBasePath;

  public static FileStructureCreator create(Path file) {
      return createStructure(file, false);

  public static FileStructureCreator createTempDirectory(Path file) {
      return createStructure(file, true);

  private static FileStructureCreator createStructure(Path file, boolean createTempDirectory) {
      FileStructureCreator fileStructureCreator = new FileStructureCreator();

      if (!Files.exists(baseTestPath)) {

      String path = baseTestPath.toString() + (createTempDirectory ? "/" + UUID.randomUUID().toString() : "")
              + "/";
      Path basePath = Paths.get(path);

      try (Stream<String> stream = Files.lines(file)) {
          stream.forEach(line -> {
              Metadata fileMetaData = Metadata.from(line);

              Path fileEntry = Paths.get(path + fileMetaData.getWindowsSafeFilename());
              try {
                  if (!Files.exists(fileEntry)) {
                      Files.write(fileEntry, line.getBytes());
                      Files.setLastModifiedTime(fileEntry, FileTime.from(fileMetaData.getModificationTime()));
              } catch (IOException ignore) {
                  throw new RuntimeException("Exception creating directory: " + fileEntry.getParent());
      return fileStructureCreator;
  public void close() {
      if (fileStructureBasePath != null) {

This basically creates the whole directory structure and the necessary files. We just need to pass it a base file which holds the metadata of the file structure.

The metadata holds a timestamp, file size and the path for this file. It looks like this:

2016-04-05T10:30:15.012345678   5120


2018-02-26T00:00:09.012345678  169


On our Synology NAS, we can then easily generate a file with the whole tree structure of a (specific) directory by executing this command:

find backupftp -type f -printf

Copy the generated file from your Synology NAS to your project.

In a JUnit test we use the FileStructureCreator class like in the example below. Note that FileStructureCreator implements AutoCloseable, so we can use a try/catch block to cleanup the files after the test completes.

private Path notEnoughFileSets;

@Test(expected = RuntimeException.class)
public void backupSetWithNoFileSetsThrowException() {
  try( FileStructureCreator creator = FileStructureCreator.createTempDirectory(notEnoughFileSets) ) {
      BackupSet backupSet = BackupSet.builder().uri(creator.getFileStructureBasePath().toString()).build();
      new DiskConsistencyPolicy(backupSet).execute();
      assertTrue( "Expecting a RuntimeException here", false);

For the Spring Boot application, we just define a @Configuration class which will create the data structures for our file shares as defined on the Synology NAS.

public class TestFilesInstaller {
  public FileStructureCreator ftpFiles(@Value("classpath:/backupftp.files.txt") Path file) {
      return FileStructureCreator.create(file);
  public FileStructureCreator nfsFiles(@Value("classpath:/backupnfs.files.txt") Path file) {
      return FileStructureCreator.create(file);

Because they are defined as a @Bean, the close() method will automatically be called when the application shuts down, removing all files from disk when the Spring Boot application is stopped.
Just … don’t run the dev profile in production; I’ll let you figure out what happens. ;-)
In the future, we’ll show you how to build a backup checker to monitor and verify backups on your NAS.

Published on Java Code Geeks with permission by Marc Vanbrabant, partner at our JCG program. See the original article here: Mocking files for JUnit testing a Spring Boot web application on Synology NAS

Opinions expressed by Java Code Geeks contributors are their own.

Notify of

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Inline Feedbacks
View all comments
Back to top button