refactor(test): move arguments variable to class

This commit is contained in:
Anthony Berg 2024-10-17 19:27:52 +02:00
parent 5041ac1f74
commit 80c7f92e88

View File

@ -13,8 +13,8 @@ class MainTest {
// File handling variables // File handling variables
@TempDir @TempDir
static Path tempDir; private static Path tempDir;
static Path tempFile; private static Path tempFile;
// PrintStream (to capture the console output) // PrintStream (to capture the console output)
private final ByteArrayOutputStream outContent = new ByteArrayOutputStream(); private final ByteArrayOutputStream outContent = new ByteArrayOutputStream();
@ -24,6 +24,9 @@ class MainTest {
private final PrintStream oldOut = System.out; private final PrintStream oldOut = System.out;
private final PrintStream oldErr = System.err; private final PrintStream oldErr = System.err;
// Argument handling
private String[] arguments;
@BeforeAll @BeforeAll
static void start() throws IOException { static void start() throws IOException {
tempFile = Files.createFile(tempDir.resolve("testing.txt")); tempFile = Files.createFile(tempDir.resolve("testing.txt"));
@ -34,6 +37,9 @@ class MainTest {
// Create new PrintStream to capture console outputs // Create new PrintStream to capture console outputs
System.setOut(new PrintStream(outContent)); System.setOut(new PrintStream(outContent));
System.setErr(new PrintStream(errContent)); System.setErr(new PrintStream(errContent));
// Reset arguments to new array with only 1 item
arguments = new String[1];
} }
@AfterEach @AfterEach
@ -46,7 +52,7 @@ class MainTest {
@Test @Test
@DisplayName("No arguments provided") @DisplayName("No arguments provided")
void noArguments() { void noArguments() {
String[] arguments = new String[0]; arguments = new String[0];
Main.main(arguments); Main.main(arguments);
@ -57,7 +63,7 @@ class MainTest {
@Test @Test
@DisplayName("Too many arguments provided") @DisplayName("Too many arguments provided")
void tooManyArguments() { void tooManyArguments() {
String[] arguments = new String[2]; arguments = new String[2];
// Simulating a potential typo of the path with a space // Simulating a potential typo of the path with a space
arguments[0] = "/path/to/"; arguments[0] = "/path/to/";
arguments[1] = "file.txt"; arguments[1] = "file.txt";