File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -33,6 +33,7 @@ run-installed-app: .check-install-dir ## Lancer l'application installée
3333
3434test : # # Exécuter les tests
3535 ./src/LanguageTest.java
36+ ./src/ApplicationTest.java
3637
3738.check-install-dir :
3839ifndef DEST_DIR
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 11import module java .base ;
22
3+ import static java .util .stream .Collectors .joining ;
4+
35class 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 ));
You can’t perform that action at this time.
0 commit comments