简体   繁体   中英

JUnit 5 fails to find Tests from the command line

Similar stackoverflow posting: "JUnit 5 ConsoleLauncher doesn't work" answered Marc Philipp 13 March 2018.

I tried to duplicate the example posted in the above posting. The JUnit Test class is "DisplayNameDemo.java" shown below:

DisplayNameDemo.java

   import org.junit.jupiter.api.DisplayName;
   import org.junit.jupiter.api.Test;

   import static org.junit.jupiter.api.Assertions.*;

   @DisplayName("A special test case")
   public class DisplayNameDemo {
 
      @Test
      @DisplayName("Custom test name containing spaces")
      void testWithDisplayNameContainingSpaces() {
      }

      @Test
      @DisplayName("╯°□°)╯")
      void testWithDisplayNameContainingSpecialCharacters() {
      }

      @Test
      @DisplayName("😱 ")
      void testWithDisplayNameContainingEmoji() {
      }
  }

Here is my JUnit 5 command line command: "java -jar junit-platform-console-standalone-1.6.2.jar --classpath. --select-class DisplayNameDemo --include-classname '.*'"

This failed when I executed it, but passed when executed in the post. Here a partial of my failed result:

~/junit5.6.2/console$ java -jar junit-platform-console-standalone-1.6.2.jar --classpath .    --select-class DisplayNameDemo --include-classname '.*'

Thanks for using JUnit! Support its development at https://junit.org/sponsoring

Usage: ConsoleLauncher [-h] [--disable-ansi-colors] [--disable-banner]
                   [--fail-if-no-tests] [--scan-modules] [--scan-classpath[=PATH[;|:
                   PATH...]]]... [--details=MODE] [--details-theme=THEME]
                   [--reports-dir=DIR] [-c=CLASS]... [--config=KEY=VALUE]... [-cp=PATH

I have all classes in the same directory as the junit-platform-console-standalone-1.6.2.jar file. I also have my classpath set to the local directory "." as shown above.

I cannot understand why this example fails. I would very much appreciate some help - especially from Marc Philipp or Sormuras on stackoverflow. Thanks for any help !

I guess you don't point with --classpath to your compiled Java classes (where the .class files are), but to your raw .java files.

If you are using Maven to build your project, your compiled test classes are inside target/test-classes . You can now use the standalone console launcher from the root of your project like:

java -jar junit-platform-console-standalone-1.6.2.jar --class-path 'target/test-classes' -c your.package.YourTest

If you are not using Maven and want to still run it with the console launcher with --class-path. then make sure your .class files are there. Eg use javac MyJavaTest.java to compile them (a little bit cumbersome without Maven/Gradle as you would have to include all your libraries to the compile step).

For more information and practical examples, consider following this tutorial .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM