feat(test): add testing with files in Main

This commit is contained in:
Anthony Berg 2024-10-17 20:02:54 +02:00
parent 80c7f92e88
commit 3be78c0e9e

View File

@ -6,6 +6,8 @@ import java.io.IOException;
import java.io.PrintStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
import static org.junit.jupiter.api.Assertions.*;
@ -73,9 +75,64 @@ class MainTest {
// 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("Invalid/Nonexistent file")
void invalidFile() {
arguments = new String[1];
arguments[0] = "doesNotExist.txt";
assertThrowsExactly(RuntimeException.class, () -> Main.main(arguments));
}
@Test
@DisplayName("Normal test run")
void normalRun() {
void normalRun() throws IOException {
// List of emails to add to the file
List<String> emails = new ArrayList<>() {{
add("johndoe@gmail.com");
add("janedoe@gmail.com");
add("person@outlook.com");
add("totallylegitpresident@gmail.com");
add("google@yahoo.com");
add("person1st@outlook.com");
add("google+scam@gmail.com");
}};
Files.write(tempFile, emails);
// The expected output - \n has been added on the end due to the program quitting at the end
String expectedOutput = "gmail.com 4\noutlook.com 2\nyahoo.com 1\n";
// Add argument for filepath
arguments[0] = tempFile.toString();
Main.main(arguments);
assertEquals(expectedOutput, outContent.toString());
}
@Test
@DisplayName("File with invalid emails")
void brokenData() throws IOException {
// List of emails, with some of the emails being invalid
List<String> emails = new ArrayList<>() {{
add("gmail.com");
add("janedoe@gmail.com");
add("person@outlook.com");
add("totallylegit@president@gmail.com");
add("google@yahoo");
add("person1st@outlook,com");
add("google+scam@gmail.com");
}};
Files.write(tempFile, emails);
// Add argument for filepath
arguments[0] = tempFile.toString();
// Checks for the fail from the email validation
assertThrowsExactly(IllegalArgumentException.class, () -> Main.main(arguments));
}
}