mirror of
https://github.com/smyalygames/kahoot-challenge-2025.git
synced 2025-07-15 14:21:00 +02:00
feat(test): test arguments in Main class
This commit is contained in:
parent
5d5c295ac4
commit
5041ac1f74
75
app/src/test/java/MainTest.java
Normal file
75
app/src/test/java/MainTest.java
Normal 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() {
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user