Over a million developers have joined DZone.

Mocking Files for JUnit Testing a Spring Boot Web Application on Synology NAS

DZone's Guide to

Mocking Files for JUnit Testing a Spring Boot Web Application on Synology NAS

You can use a Spring Boot application to perform JUnit testing on NAS — learn how to build such a web app in this tutorial.

· DevOps Zone ·
Free Resource

Is the concept of adopting a continuous everything model a daunting task for your fast moving business? Read this whitepaper to break down and understand one of the key pillars of this model in Continuous Governance: The Guardrails for Continuous Everything.

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 FileStructureCreatorimplements AutoCloseable, so we can use a try/catch block to clean up 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 and how to monitor and verify backups on your NAS.

Are you looking for greater insight into your software development value stream? Check out this whitepaper: DevOps Performance: The Importance of Measuring Throughput and Stability to see how CloudBees DevOptics can give you the visibility to improve your continuous delivery process.

nas ,junit ,testing ,devops ,spring boot

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}