From e6793bd04a0650d42a4590515877645ca6cf9813 Mon Sep 17 00:00:00 2001 From: Ludy Date: Sat, 3 Aug 2024 13:52:50 +0200 Subject: [PATCH] Fix: fail JUnit test (#1625) --- .../SPDF/{utils => }/SPdfApplicationTest.java | 39 +++++++++++++++++-- .../software/SPDF/utils/FileToPdfTest.java | 2 +- 2 files changed, 36 insertions(+), 5 deletions(-) rename src/test/java/stirling/software/SPDF/{utils => }/SPdfApplicationTest.java (52%) diff --git a/src/test/java/stirling/software/SPDF/utils/SPdfApplicationTest.java b/src/test/java/stirling/software/SPDF/SPdfApplicationTest.java similarity index 52% rename from src/test/java/stirling/software/SPDF/utils/SPdfApplicationTest.java rename to src/test/java/stirling/software/SPDF/SPdfApplicationTest.java index f9229b64..350aef74 100644 --- a/src/test/java/stirling/software/SPDF/utils/SPdfApplicationTest.java +++ b/src/test/java/stirling/software/SPDF/SPdfApplicationTest.java @@ -1,10 +1,11 @@ -package stirling.software.SPDF.utils; +package stirling.software.SPDF; import static org.junit.jupiter.api.Assertions.*; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; +import java.nio.file.Paths; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -14,7 +15,6 @@ import org.mockito.Mock; import org.mockito.junit.jupiter.MockitoExtension; import org.springframework.core.env.Environment; -import stirling.software.SPDF.SPdfApplication; import stirling.software.SPDF.model.ApplicationProperties; @ExtendWith(MockitoExtension.class) @@ -43,12 +43,43 @@ public class SPdfApplicationTest { @Test public void testMainApplicationStartup() throws IOException, InterruptedException { + // Setup mock environment for the main method + Path configPath = Path.of("test/configs"); + Path settingsPath = Paths.get("test/configs/settings.yml"); + Path customSettingsPath = Paths.get("test/configs/custom_settings.yml"); + Path staticPath = Path.of("test/customFiles/static/"); + Path templatesPath = Path.of("test/customFiles/templates/"); + + // Ensure the files do not exist for the test + if (Files.exists(settingsPath)) { + Files.delete(settingsPath); + } + if (Files.exists(customSettingsPath)) { + Files.delete(customSettingsPath); + } + if (Files.exists(staticPath)) { + Files.delete(staticPath); + } + if (Files.exists(templatesPath)) { + Files.delete(templatesPath); + } + + // Ensure the directories are created for testing + Files.createDirectories(configPath); + Files.createDirectories(staticPath); + Files.createDirectories(templatesPath); + + Files.createFile(settingsPath); + Files.createFile(customSettingsPath); + // Run the main method SPdfApplication.main(new String[]{}); // Verify that the directories were created - assertTrue(Files.exists(Path.of("customFiles/static/"))); - assertTrue(Files.exists(Path.of("customFiles/templates/"))); + assertTrue(Files.exists(settingsPath)); + assertTrue(Files.exists(customSettingsPath)); + assertTrue(Files.exists(staticPath)); + assertTrue(Files.exists(templatesPath)); } @Test diff --git a/src/test/java/stirling/software/SPDF/utils/FileToPdfTest.java b/src/test/java/stirling/software/SPDF/utils/FileToPdfTest.java index 66c19539..585f677b 100644 --- a/src/test/java/stirling/software/SPDF/utils/FileToPdfTest.java +++ b/src/test/java/stirling/software/SPDF/utils/FileToPdfTest.java @@ -13,7 +13,7 @@ public class FileToPdfTest { @Test public void testConvertHtmlToPdf() { HTMLToPdfRequest request = new HTMLToPdfRequest(); - byte[] fileBytes = new byte[10]; // Sample file bytes + byte[] fileBytes = new byte[0]; // Sample file bytes String fileName = "test.html"; // Sample file name boolean htmlFormatsInstalled = true; // Sample boolean value