Skip to content

Commit bcdd3be

Browse files
author
Pierre-Yves Fourmond
committed
Ajout de tests pour Application.java
1 parent bbaa3d0 commit bcdd3be

4 files changed

Lines changed: 45 additions & 1 deletion

File tree

.idea/misc.xml

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ run-installed-app: .check-install-dir ## Lancer l'application installée
3333

3434
test: ## Exécuter les tests
3535
./src/LanguageTest.java
36+
./src/ApplicationTest.java
3637

3738
.check-install-dir:
3839
ifndef DEST_DIR

src/ApplicationTest.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
//usr/bin/env java -enableassertions --class-path ${APP_DIR:-.}/lib/'*' "$0" "$@"; exit $?
2+
3+
void main() {
4+
TestRunner.runTests(getClass());
5+
}
6+
7+
void sayHelloFrenchTest() {
8+
String output = runApplication("--language", "French");
9+
assert output.contains("Bonjour 🇫🇷") : "Expected 'Bonjour 🇫🇷' but got: " + output;
10+
}
11+
12+
void sayHelloEnglishTest() {
13+
String output = runApplication("--language", "English");
14+
assert output.contains("Hello 🇬🇧") : "Expected 'Hello 🇬🇧' but got: " + output;
15+
}
16+
17+
void helpFlagTest() {
18+
String output = runApplication("--help");
19+
assert output.contains("Usage:") : "Help should start with 'Usage:'";
20+
}
21+
22+
String runApplication(String... args) {
23+
return TestRunner.runApplication("./src/Application.java", args);
24+
}

src/TestRunner.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,31 @@
11
import module java.base;
22

3+
import static java.util.stream.Collectors.joining;
4+
35
class TestRunner {
6+
47
static void runTests(Class<?>... classes) {
58
for (Class<?> clazz : classes) {
69
runTests(getInstance(clazz));
710
}
811
}
912

13+
static String runApplication(String name, String... args) {
14+
try {
15+
var command = new ArrayList<String>();
16+
command.add(name);
17+
command.addAll(List.of(args));
18+
var process = new ProcessBuilder(command).start();
19+
try (var reader = new BufferedReader(new InputStreamReader(process.getInputStream()))) {
20+
String output = reader.lines().collect(joining("\n"));
21+
process.waitFor();
22+
return output;
23+
}
24+
} catch (Exception exception) {
25+
throw new RuntimeException("Application has failed to start", exception);
26+
}
27+
}
28+
1029
private static void runTests(Object instance) {
1130
Arrays.stream(instance.getClass().getDeclaredMethods())
1231
.forEach(method -> runTest(instance, method));

0 commit comments

Comments
 (0)