Gradle systemProperty in custom task not setting Spring Profile - java

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"
}

Related

Passing jvm arguments to Gradle test task

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

Run specific junit4 categories using gradle

I am running some tests on an Android phone using gradle. However I would like to be able to select which tests to run. For the tests I am using jUnit4 and categories.
This is how the tests are build and executed from jenkins:
call gradle assembleDebug assembleDebugAndroidTest
call gradle connectedDebugAndroidTest
This is how a test looks like:
#Category(IncludeTest.class)
#Test
public void test_Test_06() throws Exception {
TestData.setUpTest("test_Test_06");
Log.d("Test: Test 6 included");
}
#Category(ExcludeTest.class)
#Test
public void test_Test_07() throws Exception {
TestData.setUpTest("test_Test_07");
Log.d("Test: Test 7 excluded");
}
In my gradle.build I have tried the following without success:
test {
useJUnit {
includeCategories 'com.abc.def.IncludeTest'
excludeCategories 'com.abc.def.ExcludeTest'
}
}
My structure is as follows:
/someFolder/gradle.build
/someFolder/app/src/android/java/
In java i have a package named com.abc and in that package there is another package, def where my IncludeTest and ExcludeTest interfaces are.
I have tried different paths to Include/ExludeTest in gradle.build but it just does not work, all test all always executed.
I have also tried putting the includeCategories/excludeCategories in a task and made sure the task was actually started. But still all test were executed. Just seems like includeCategories/excludeCategories does not do anything.
Is there anything basic I am doing wrong? Are there any other ways of selecting categories?
After some more research I found out that the includeCategories/excludeCategories does not work with Android.
I found a different solution when using AndroidJUnitRunner. In Gradle it is possible to filter tests on annotations. To include annotations:
call gradle connectedDebugAndroidTest -Pandroid.testInstrumentationRunnerArguments.annotation=com.abc.def.IncludeTest
There is also the possibility to exclude annotations using notAnnotation instead.
And of course there is syntax if you want to use multiple annotations with OR/AND combinations.
The following is also possible if you are not using connectedDebugAndroidTest:
adb shell am instrument -w -e annotation com.android.foo.MyAnnotation com.android.foo/android.support.test.runner.AndroidJUnitRunner
Some documentation: https://developer.android.com/reference/android/support/test/runner/AndroidJUnitRunner.html

How to run tests multiple times with different system property values in Gradle

I'd like to know how to run my test suite in Gradle twice, with different values set for a system property. Right now I can set a system property using e.g.:
test {
systemProperty "org.d2ab.sequence.strict", "true"
}
But how can I set the property to false and run the test suite again?
Just add another Test task.
test {
systemProperty "org.d2ab.sequence.strict", "true"
}
task test2(type: Test) {
systemProperty "org.d2ab.sequence.strict", "false"
}
check.dependsOn test2
See here to see where the default test task is added by the java plugin.

Jenkins - Selecting tests

I am currently working on a Maven Project, using JUnit for defining tests and Jenkins for CI and am looking into how I can group my tests.
Say I had a test class with 20 tests, but I don't want to run all 20 tests, I want to be able to configure which tests to run. For Example, in another standalone project using TestNG and Selenium you can create a test method with the following annotation:
#Test (groups = { "AllTest" })
public void myTestMethod()
{
.. do something
.. assert something
}
... and then I am able to call which group to run based on an XML configuration.
Is it possible to define such type of groupings using Jenkins? I have researched into this and came across the plugin "Tests Selector Plugin" however can't understand how to get started once I've installed the plugin. There is a Wiki Page for it but I can't understand what to do after installing.
I have copy pasted the example property file, and didn't really understand what I needed to manipulate in it. When building, I simply get that the property file cannot be found or Jenkins doesn't have permission; can't find a way around this either :(
It's possible via maven + maven-surefire-plugin
http://maven.apache.org/surefire/maven-surefire-plugin/examples/single-test.html
You can run a single test, set of tests or tests by regexp.

How do I change JVM Options for one Gradle Task/project?

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"
}
}

Categories