简体   繁体   中英

Build file in Sublime Text 3 to invoke JUnit5 tests

I am trying to configure Sublime Text 3 's RunJava.sublime-build file to add an option to execute JUnit 5 test on Mac OS X .

The following works for me from the Mac OS X terminal window:

$ java -jar /Users/pavlomaistrenko/Documents/Junit1.6/junit-platform-console-standalone-1.6.2.jar -cp /Users/pavlomaistrenko/Documents/GitHub/SvitlanaMo/Java01/ --scan-classpath

My RunJava.sublime-build configuration file is:

{
    "selector": "source.java",
    "file_patterns": "*.java",
    "working_dir": "$file_path",
    "shell_cmd": "javac -cp .:/Users/pavlomaistrenko/Documents/GitHub/SvitlanaMo/Java01/:/Users/pavlomaistrenko/Documents/Junit1.6/junit-platform-console-standalone-1.6.2.jar $file_name",
    "variants": [
        {
            "name": "Run class.jar",
            "shell_cmd": "java -cp .:/Users/pavlomaistrenko/Documents/GitHub/SvitlanaMo/Java01/:/Users/pavlomaistrenko/Documents/Junit1.6/junit-platform-console-standalone-1.6.2.jar $file_base_name"
        },

        {
            "name": "JavaDoc",
            "shell_cmd": "mkdir -p documentation && javadoc -header \"<script type='text/javascript' src='http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML'></script>\" -d documentation *.java"
        },

        {
            "name": "JAR",
            "cmd": "javac '$realpath$file' && echo \"Main-Class: $file_base_name\" > Manifest.txt && jar cfm $file_base_name.jar Manifest.txt *.class && rm *.class && java -jar $file_base_name.jar"
        },

        {
            "name": "JUnit5",
            "cmd": "java -jar /Users/pavlomaistrenko/Documents/Junit1.6/junit-platform-console-standalone-1.6.2.jar -cp $file_path --scan-classpath"
        }

    ]
}

Instead of full JUnit 5 output I just get [Finished in X.Xs] .

Does JUnit get executed? If so, where is the output?

PS I have read Sublime is not a perfect environment for Java, but still.

I suspect what you are doing to trigger the unit tests is either using the B keyboard shortcut or selecting Tools → Build from the menu. The way Sublime's build systems work, this will only trigger the initial "shell_cmd" command. The way to run one of the variants is to use B or select Tools → Build With… . This will display a dropdown with all the different variants available. Selecting JUnit5 should run your unit tests as specified.

Finally found out a working build configuration for JUnit5. Here is the snippet:

{
    "name": "JUnit5 the folder",
    "env": { "user_path": "/Users/pavlomaistrenko/Documents",
     "junit5":  "Junit1.6/junit-platform-console-standalone-1.6.2.jar",
     "project_path": "GitHub/SvitlanaMo/Java01/"
   },
    "shell_cmd": "java -jar \\$user_path/\\$junit5 -cp .:$file_path --scan-classpath"
},

Note the double escaping of $ with two backslashes \\ for Mac OS. The env variables are not expanded by Sublime, rather they are set as environment variables for this particular call of java loader (and then expanded by the shell.) See Use custom variables in Sublime Text build system for explanation.

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