jbang
jbang copied to clipboard
Simplify running unit tests
I have a jbang script.java and TestScript.java, which includes //SOURCES script.java.
build.gradle however doesn't contain
test {useJUnitPlatform()}
and this has to be added to each new project or when a project is regenerated. It would be nice, if the generated build.gradle would get it automatically.
After fixing build.gradle file, I can run the test from the IDE, but it would be also nice to be able to run it from the command line.
I've used jbang junit.java -f TestScript.java to run the test:
///usr/bin/env jbang "$0" "$@" ; exit $?
//DEPS org.junit.platform:junit-platform-console-standalone:1.6.2
import org.junit.platform.console.ConsoleLauncher;
import static java.lang.System.*;
public class junit { public static void main(String... args) { ConsoleLauncher.main(args); }}
but it doesn't pick up the tests
Have you tried using 'jbang -t junit@jbangdev yourscript.java'
?
It provides the scaffolding for running a junit test.
About the gradle part - that does seem to make sense to add albeit not really grokking what that does if you already have a dependency on junit in your java file ?
'jbang -t junit@jbangdev yourscript.java
Unknown option: '-t'
without test {useJUnitPlatform()} in gradle.build test are not found and not executed in IDE
Sorry - missing init.
jbang init -t junit@jbangdev yourscript.java
Used jbang init -t junit@jbangdev yourscript.java
- Problem one:
- on command line run the test all ok,
- then change yourscript to return wrong value,
- re-run the test and it does not fail: yourscript.java is not re-compiled if the test is re-executed
- Problem in IDE still remains
Execution failed for task ':test'.
> No tests found for given includes: [yourscriptTest](--tests filter)
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.
after adding test {useJUnitPlatform()} it works
Ah yes. We should include sources in dirty check.
Until then run with jbang --fresh to force compile.
I'll investigate why ide can't just run tests when everything is on the classpath.
how are you running the tests ? in intellij I can run them just fine, without changing build.gradle.
e.g. with a click on the line public class testTest { to execute all tests in the file. or with Alt+Enter -> run as in your screenshot
Weird. Just works for me.
Ah - you probably have "run tests via gradle" enabled.
I always turn that off as it's just too slow.
That must be the difference.
yes, I think these are the default settings in idea. I generated a fresh project (java 17, gradle 7.3.3) with jbang as above, and gradle settings in idea are as below. Do you change the settings every time?

well its a global setting apparently as its been over a year since I changed it as it was just too slow for me :)
I think we should be able to add that default if junit dependencies detected...
and maybe also detect current SDK? I've generated the project with JDK 17 and as you can see in the screenshot above 16 is set
Yeah. It's been a long standing issue since when I said it to 17 gradle starts failing. Maybe gradle 7 fixed that.