Add white box test for randomWalk.

This commit is contained in:
ulic-youthlic 2025-06-01 02:22:14 +08:00
parent 3f6c19ed28
commit 35d68d884e
Signed by: youthlic
GPG key ID: 63E86C3C14A0D721
4 changed files with 145 additions and 97 deletions

View file

@ -36,6 +36,8 @@ dependencies {
implementation("org.jline:jline:3.29.0")
compileOnly("com.github.spotbugs:spotbugs-annotations:3.1.3")
testImplementation("org.mockito:mockito-core:5.+")
}
// Apply a specific Java toolchain to ease working on different environments.

View file

@ -331,7 +331,7 @@ class GraphCLIHelper {
String randomWalk() {
final var text = String.join(" ", graph.randomWalk());
final Path path = Paths.get("output.txt");
final Path path = Paths.get(System.getProperty("user.dir"), "output.txt");
try {
Files.write(path, text.getBytes());
} catch (final IOException e) {

View file

@ -1,8 +1,19 @@
package fun.youthlic;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
import org.mockito.MockedStatic;
import static org.junit.jupiter.api.Assertions.assertEquals;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.nio.file.AccessDeniedException;
import java.nio.file.Files;
import java.nio.file.Path;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mockStatic;
public class GraphCLIHelperTest {
private final GraphCLIHelper graph = new GraphCLIHelper("B D E H A C D H A F D H A B G H A C G H A D G H");
@ -12,6 +23,7 @@ public class GraphCLIHelperTest {
var result = graph.queryBridgeWords("x", "a");
assertEquals("No x or a in the graph!", result);
}
@Test
void testWord2NotInGraph() {
String result = graph.queryBridgeWords("a", "x");
@ -45,7 +57,7 @@ public class GraphCLIHelperTest {
@Test
void testThreeBridgeWords() {
String result = graph.queryBridgeWords("a", "g");
assertEquals("The bridge words from a to g are: c, b, and d.", result);
assertEquals("The bridge words from a to g are: d, c, and b.", result);
}
@Test
@ -77,4 +89,38 @@ public class GraphCLIHelperTest {
String result = graph.queryBridgeWords("B", "E");
assertEquals("The bridge words from B to E are: d.", result);
}
@Test
void testNormalExecution(@TempDir Path tempDir) throws IOException {
System.setProperty("user.dir", tempDir.toString());
String result = graph.randomWalk();
assertNotNull(result);
assertFalse(result.isEmpty());
Path outputPath = tempDir.resolve("output.txt");
System.out.println(outputPath);
assertTrue(Files.exists(outputPath));
String fileContent = Files.readString(outputPath);
assertEquals(fileContent, result);
}
@Test
void testWritePermissionDenied() {
try (MockedStatic<Files> mockedFiles = mockStatic(Files.class)) {
mockedFiles.when(() -> Files.write(any(Path.class), any(byte[].class)))
.thenThrow(new AccessDeniedException("Permission denied"));
ByteArrayOutputStream errContent = new ByteArrayOutputStream();
System.setErr(new PrintStream(errContent));
String result = graph.randomWalk();
assertNotNull(result);
String errorOutput = errContent.toString();
assertTrue(errorOutput.contains("Failed to write to file"));
assertTrue(errorOutput.contains("AccessDeniedException"));
}
}
}