feat(test): test arguments in Main class

This commit is contained in:
Anthony Berg 2024-10-17 19:22:31 +02:00
parent 5d5c295ac4
commit 5041ac1f74

View File

@ -0,0 +1,75 @@
import org.junit.jupiter.api.*;
import org.junit.jupiter.api.io.TempDir;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.nio.file.Files;
import java.nio.file.Path;
import static org.junit.jupiter.api.Assertions.*;
class MainTest {
// File handling variables
@TempDir
static Path tempDir;
static Path tempFile;
// PrintStream (to capture the console output)
private final ByteArrayOutputStream outContent = new ByteArrayOutputStream();
private final ByteArrayOutputStream errContent = new ByteArrayOutputStream();
// Save the old PrintStreams
private final PrintStream oldOut = System.out;
private final PrintStream oldErr = System.err;
@BeforeAll
static void start() throws IOException {
tempFile = Files.createFile(tempDir.resolve("testing.txt"));
}
@BeforeEach
void setUp() {
// Create new PrintStream to capture console outputs
System.setOut(new PrintStream(outContent));
System.setErr(new PrintStream(errContent));
}
@AfterEach
void tearDown() {
// Reset PrintStream to original
System.setOut(oldOut);
System.setErr(oldErr);
}
@Test
@DisplayName("No arguments provided")
void noArguments() {
String[] arguments = new String[0];
Main.main(arguments);
// Check that the program quit at the expected point, with expected output
assertEquals("Invalid arguments, you need to define the text file to read from!\n", outContent.toString());
}
@Test
@DisplayName("Too many arguments provided")
void tooManyArguments() {
String[] arguments = new String[2];
// Simulating a potential typo of the path with a space
arguments[0] = "/path/to/";
arguments[1] = "file.txt";
Main.main(arguments);
// Check that the program quit at the expected point, with expected output
assertEquals("Invalid arguments, you need to define the text file to read from!\n", outContent.toString());
}
@Test
@DisplayName("Normal test run")
void normalRun() {
}
}