DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Spring Boot: JUnit Integration Test
  • Spring Boot: Testing Service Layer Code with JUnit 5 and Mockito, RESTful Web Services
  • Using ZK With Spring Boot
  • Actuator Enhancements: Spring Framework 6.2 and Spring Boot 3.4

Trending

  • Kill the Worker, Keep the Research: Build a Recoverable LangGraph Agent on Temporal
  • Why Your AI Agent Works in the Demo and Fails in Production
  • The Twelve-Factor Agents: Building Production-Ready LLM Applications
  • Vision-Language Models in 2026: From Research Breakthroughs to Cloud-Native Production Systems

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  1. DZone
  2. Coding
  3. Frameworks
  4. Mocking Files for JUnit Testing a Spring Boot Web Application on Synology NAS

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.

By 
Marc Vanbrabant
·
May. 17, 18 · Tutorial
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
27.2K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

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:

@Getter
@Setter
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);
  }

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

      if (!Files.exists(baseTestPath)) {
          Files.createDirectory(baseTestPath);
      }

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

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

              Path fileEntry = Paths.get(path + fileMetaData.getWindowsSafeFilename());
              try {
                  FileUtils.forceMkdir(fileEntry.getParent().toFile());
                  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;
  }
  @Override
  @SneakyThrows
  public void close() {
      if (fileStructureBasePath != null) {
          FileUtils.deleteDirectory(fileStructureBasePath.toFile());
      }
  }
}

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
backupftp/@eaDir/sharesnap_share_configuration/[email protected]

2018-02-26T00:00:09.012345678 169

backupftp/@eaDir/sharesnap_share_configuration/share_configuration


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
"%TY-%Tm-%TdT%TH:%TM:%.12TS\t%s\t%p\n">test/backupftp.files.txt

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.

@Value("classpath:/TestDiskConsistencyPolicy-notEnoughFileSets.txt")
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.

@Configuration
@Profile("dev")
public class TestFilesInstaller {
  @Bean
  public FileStructureCreator ftpFiles(@Value("classpath:/backupftp.files.txt") Path file) {
      return FileStructureCreator.create(file);
  }
  @Bean
  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.

Spring Framework Web application Spring Boot JUnit

Published at DZone with permission of Marc Vanbrabant. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Spring Boot: JUnit Integration Test
  • Spring Boot: Testing Service Layer Code with JUnit 5 and Mockito, RESTful Web Services
  • Using ZK With Spring Boot
  • Actuator Enhancements: Spring Framework 6.2 and Spring Boot 3.4

Partner Resources

×

Comments

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook