By default, Java disables assertions. (The test I'm using here is assert false; as the first line in main().)
I'd like to have them enabled while running my program with gradle run. What's the trick for doing this?
There is a specific flag enableAssertions that you can set to enable assertions. Such flags are often more readable than working with the jvm args directly. It comes down to personal preferences I think.
Instead of working with applicationDefaultJvmArgs you can add the following to your build.gradle file:
run {
enableAssertions = true
}
This configures the run task to enable assertions.
The run task is of type JavaExec, have a look at the documentation if you are interested in other parameters that you can set (e.g., workingDir or heapMaxSize).
tasks.withType(JavaExec) {
enableAssertions = true
}
Related
I'm trying to selectively deactive tests if there is a spring profile called "unit", which is working:
#DisabledIfEnvironmentVariable(named = "SPRING_PROFILES_ACTIVE", matches = "unit")
If I run SPRING_PROFILES_ACTIVE=unit ./gradlew clean test this works fine. The issue I'm having is that I don't want to have to write all that out, and instead I want a custom gradle task, also called "unit". It currently looks like this:
task unit(type: Test) {
useJUnitPlatform()
systemProperty "SPRING_PROFILES_ACTIVE", "unit"
}
This runs all tests, even if they are supposed to be disabled. I tried useJUnitPlatform { sysProp... }, I tried adding options { sysProp... }, I tried using doFirst, I tried switching "SPRING_PROFILES_ACTIVE" to "spring.profiles.active" in both the build.gradle and in the annotation, I tried systemProperties "SPRING_PROFILES_ACTIVE": "unit" but that doesn't work.
I found lots of StackOverflow questions where people just put systemProperties into the default test task, but I couldn't find anything with a custom task.
What do I need to call where to get the task to pass a spring profile system property?
#DisabledIfEnvironmentVariable annotation requires environment variable to be set. Try to set environment variable instead of system property in Your task:
task unit(type: Test) {
useJUnitPlatform()
environment "SPRING_PROFILES_ACTIVE", "unit"
}
After a long time of searching I was still not able to find any official documents for the gradle run task. I assume that is because it is actually JavaExec task type.
It also seems that the run task is only available with the application plugin. Its docs mention some of the available arguments such as --debug-jvm and --args (for passing command-line arguments to the application's main method).
What I actually wanted to find out how I can pass arguments to the JVM on the command-line, i.e. an equivalent of setting application { applicationDefaultJvmArgs = ".." }.
Help appreciated!
You're right, the run task comes from the application plugin and it is a JavaExec task.
A list of all configuration options is available in the documentation of the JavaExec task
You can configure options in your (groovy-)gradle file like so:
tasks.named('run', JavaExec) {
mainClassName = '...MainKt'
applicationDefaultJvmArgs = [ System.getProperty("jvmArgs") ]
classpath = sourceSets.netMain.runtimeClasspath
}
I have now written https://blog.jakubholy.net/2020/customizing-gradle-run-task/ which describes both how to customize the run task and how to customize it on the command line:
apply plugin: 'application'
mainClassName = "my.app.Main"
run {
debugOptions {
enabled = true
server = true
suspend = false
}
systemProperty("my.defaultLogLevel", "debug")
environment("OTEL_EXPORTER", "zipkin")
jvmArgs=["-javaagent:aws-opentelemetry-agent-0.9.0.jar"]
}
As the application plugin documentation states, you can also enable debugging with --debug-jvm or specify arguments with --args="foo --bar". And you can set application { applicationDefaultJvmArgs= []} to apply both to run and the generated start scripts of your distribution.
I would like to pass JVM parameters to my Gradle test task. I use the parameters in a Cucumber feature file: ${app.url}. In the build.gradle file, I put those lines:
test {
testLogging.showStandardStreams = true
systemProperties System.getProperties()
}
When I execute gradle test -Dapp.url=http://....., I don't see the parameter was passed to the application. I also tried the below, but the result is the same:
test {
testLogging.showStandardStreams = true
systemProperty "app.url", System.getProperty("app.url")
}
When I use Maven and pass the same parameters as Jvm arguments, it works fine. Now I would like to switch to the Gradle, but I am stuck with passing parameters.
gradle is having -P or --project-prop option. Try this one and see.
-P, --project-prop Set project property for the build script
I have a set of gradle projects and subprojects. I'm trying to change the JVM args for one single subproject, because it is a set of unit tests that require a large amount of memory - so I want to add '-Xms2g -Xmx4g' to the VM opts when I execute just that target.
Is there a way to do that? The only specific ways that I've found in the documentation are to set _JAVA_OPTIONS in the environment, or org.gradle.jvmargs="-Xms2g -Xmx4g" in the gradle.properties script, but both of those cause all of the targets to use those options.
I'm pretty new to gradle, so pointers to specific docs that cover per-task properties are particularly welcome.
Yes, this can be done with any task that implements JavaForkOptions.
test {
minHeapSize = '2g'
maxHeapSize = '4g'
}
If you are going to change timeZone only for specific sub project, setting like this solved problem.
test{
systemProperty "user.country", "US"
systemProperty "user.language", "en"
systemProperty "user.timezone", "Asia/Colombo"
useTestNG() {
suites "src/test/resources/testng.xml"
}
}
I want to enable the assertion facility in ant. In my ant build.xml, I put the follows, trying to enable assertions.
<project> ...
<assertions>
<enable/>
</assertions>
</project>
I put assertion in a junit file, which includes only one function,
testAssertions() {
assert false;
}
when running ant, assertion fails are not thrown.. How to enable assertion in this setting?
It looks like your <assertions> subelement is a child of <project>, is this correct?
I am assuming that you are running the test via the <junit> ant task. If this is correct, making the <assertions><enable/></assertions> subelement a child of <junit> should work.
To enable assertions, I edited nbproject/project.properties and changed
# Space-separated list of JVM arguments used when running the project.
# You may also define separate properties like run-sys-prop.name=value instead of -Dname=value.
# To set system properties for unit tests define test-sys-prop.name=value:
run.jvmargs=
to
run.jvmargs=\
-ea
After doing this, assertions were enabled when I executed ant run.