Gradle Tests inside main sources - java

I'm writing web service to run tests over junit using web endpoint, for this reason I have to keep all tests in sources.
The problem is that I want run tests dirctly from IDEA and command line, and with last I've stucked.
How can I run tests inside sources folder (src/main/java) through "gradle test"?
I'm not fammiliar with gradle, but I've tried to use
sourceSets {
test {
java.srcDir file('src/main/java')
resources.srcDir file('src/main/resources')
}
}
But it breaks project import using IDEA, my 2016.1.1 sometimes can't create two modules with same content root or expectedly marks sources as tests.
Can I solve this problem with customizing test {}, or somehow load test sources into SpringBoot launcher for web service?
Maybe I can extend gradle test task and customize it?

Try removing the file() calls
sourceSets {
test {
java.srcDir 'src/main/java'
resources.srcDir 'src/main/resources'
}
}
Tested on gradle 5.4.1 but should work on other versions too

I guess this post can help you to leanr how to start the tests inside the main sources:
How to run junit tests with gradle?.
I would specify one subfiolder in source which shall contain all your tests.
And you can specify the output directory, which should be different to the default output directory:
sourceSets {
test {
java.srcDir file('src/main/java/tests') // tests shall contain your tests
resources.srcDir file('src/main/resources')
output.resourcesDir = "build/classes/main"
}
}

Related

In a java maven or gradle project, can we write test cases in main package rather than in test package?

How do we make mvn build or Gradle build to take test cases from the main package if we have written test cases in the main package?
In gradle you just redefine the main and test sourceSets with filters in your build.gradle file to exclude / include the test files in the specific phase of the build.
For example the file named LibraryTest.java will be compiled and executed only during the test phase.
sourceSets {
main {
java {
srcDirs = ['src/main/java']
exclude '**/*Test.java'
}
}
test {
java {
srcDirs = ["src/main/java"]
include '**/*Test.java'
}
}
}
you can write your test cases where ever you want, BUT!
It is not a recommended practice, so if you are using maven/gradle - they will give you dedicated folder/path for writing test cases.
The reason why it is not recommended - is maven/gradle provides lot of plugins which will help you to run your test cases, generate reports for those test cases, control the build if test cases fails.
All these plugins will look up at the default path, so if you decide to use a different path rather than default - you need to change the path for test cases in all your plugin.
so if you choose to use your own path for test resources, you are just adding overhead of additional configuration changes.

How to run main file using gradle?

I have an application in java written in IntelliJ using gradle. Right now I can run tests but I'd like to run main file to check execution by myself. Does anyone know how to run main file?
Looks like you use Java. In this case:
Apply application plugin
Define mainClassName property
The build.gradle.kts fill be like:
plugins {
application
}
application {
mainClassName = "org.gradle.sample.Main"
}
Next you can start application via gradlew run

How to make Cucumber features run in an Android project as local unit tests?

I have an Android project written in Java that I'm working on in Android Studio.
I'd like to use Cucumber for integration testing of some internal components (note: I know this is not the BDD way, nonetheless useful to me). I want the tests to run as local unit tests (without Instrumentation) using gradlew test because the components under test do not interact with the Android SDK.
My problem is that the Cucumber features are not recognized by Gradle and do not run when I run gradlew test.
Here's how I set it up so far:
Added these dependencies to my app's build.gradle:
testImplementation 'io.cucumber:cucumber-java:3.0.2'
testImplementation 'io.cucumber:cucumber-junit:3.0.2'
testImplementation 'io.cucumber:cucumber-jvm:3.0.2'
Also there, I added the path to where I've put my Feature file:
android {
...
sourceSets {
test {
assets.srcDirs = ['src/test/java/integrationTest/assets']
}
}
}
This is based on this folder structure:
Added a class for the steps (Steps1.java) as can be seen above.
What am I missing here?
Your feature files are probably not getting picked up because you did not include a runner. You can either create a JUnit Runner or use the Gradle cucumber plugin. I am not sure if either would work in Android though.
Also you don't need io.cucumber:cucumber-jvm:3.0.2 as a dependency. It is only a pom.
I've found out how to configure my Android app to run my Cucumber tests as "local unit tests" when I run gradlew test from the command line. This is based on my SO question here and I've written a "HOWTO" blog post about it here: Android: Run Cucumber tests without a device or an emulator.
The key to getting it done is in the app's build.gradle file. By latching on to the unitTests.all hook in the testOptions section I am able to run Cucumber using javaexec like so:
android {
...
testOptions {
unitTests.all {
def classpath2 = getClasspath()
javaexec {
main = "cucumber.api.cli.Main"
classpath = classpath2
args = ['--plugin', 'pretty', '--glue', 'gradle.cucumber', 'src/test/java/cucumber/assets']
}
}
}
)

Tell Gradle to check two directories for main java source code

I have a project full of tests that we use to query our environments. We run these tests using Gradle. I would like to run these tests from a standalone application to get rid of the Gradle dependency. I am using the gradle 'application' plugin and trying to run the JUnit tests using JUnitCore and everything is fine except I can't access my test classes from main.
I have
--main
--smokeTest
--longRunningTest
When I tell Gradle this it doesn't work.
sourceSets {
main {
java { srcDirs ['src/main/java', 'src/smokeTest/java'] }
}
}
It says "main" is not a recognized function. The java plugin is installed because I already have entries to define smokeTest and longRunningTest.
Not to steal the flame from #david-m-karr, just refining a bit:
sourceSets.main.java.srcDirs = ['src/main/java', 'src/smokeTest/java']
might work

Gradle - running a junit test not finding my xml file

I'm having a simple junit test task.
task testSanity(type: Test, dependsOn: testClasses) {
def springInstrumentJarPath = project.configurations.compile.find { it.name.startsWith("spring- instrument") }
jvmArgs '-javaagent:' + springInstrumentJarPath
exclude '**/*TimeBaseTest*'
exclude '**/*SuiteTests*'
}
Problem is i added a new xml file for my ehcache at:
/src/main/webapp/WEB-INF/classes/ehcache.xml
And all my test fails since junit is not able to find the file.
How can I define this in the gradle task?
Thanks.
src/main/webapp should only contain files that aren't used from Java code (more precisely, files that aren't loaded by a class loader). Java resources should instead go into src/main/resources. Hence, moving the file to src/main/resources/ehcache.xml should solve the problem.

Categories