Gradle separate test classpath dependencies for scala and java tests - java

I use Mockito in my Java tests, and have some Scala tests. If I include Scala testCompile dependency then Mockito fail to create a spy when running in IntelliJ, and I get ClassCastException exception.
Can I separate the scala and the java tests, so the java tests have some dependencies and the scala has other dependencies?

If you don't have to run all your tests in the same build, you can use dependency profiles. Lets say scalaTests and javaTests.
And you can group test dependencies like below
dependencies {
....
if (project.hasProperty('scalaTests')) {
//scala test dependencies here
}
else if (project.hasProperty('javaTests'))
//java test dependencies here
}
}
You also have to run only Scala tests when you are using Scala profile(same for java test)
You can run gradle with scalaTests profile like this gradle -PscalaTests .. (same for java case)
For running specific tests
How to run only one test class on gradle
Gradle run only one test suite
For profile details.
https://gist.github.com/szpak/1499336
Maven profiles equivalent of Gradle

Related

How to run JUnit5 and JUnit4 in same Gradle build?

I read an answer about Maven but I was wondering how I would achieve this task in Gradle - Executing JUnit 4 and JUnit 5 tests in a same build.
Currently my Gradle build only picks up tests with:
import org.junit.jupiter.api.Test;
My problem is that I'm using #RunWith which needs JUnit4 to run but I want to execute it on a JUnit5 Vintage Engine.
How do I make my build such that I can run JUnit4 and JUnit5 together. Thanks.
Update:
Now there is a native Mockito Junit Jupiter for JUnit 5 - https://mvnrepository.com/artifact/org.mockito/mockito-junit-jupiter
The junit5-migration-gradle project demonstrates how to execute tests based on JUnit 5 using Gradle. In addition, it showcases that existing JUnit 4 based tests can be executed in the same test suite as JUnit Jupiter based tests or any other tests supported on the JUnit Platform.
Basically it boils down to having both engines, Jupiter and Vintage, on the runtime class-path:
dependencies {
testCompile("org.junit.jupiter:junit-jupiter-api:5.2.0")
testRuntime("org.junit.jupiter:junit-jupiter-engine:5.2.0")
}
dependencies {
testCompile("junit:junit:4.12")
testRuntime("org.junit.vintage:junit-vintage-engine:5.2.0")
}

Cucumber tests are being skipped

I am using cucumber framework in my project. So for the reporting part, i need have started building the cucumber reports with jenkins.
So during the setup for the trail build i am facing the below issues.
All the tests are skipped, but not executing.
Can anyone have solution. I am happy to provide any other inputs required.
When using maven and cucumber you have two options to run the tests.
With maven: mvn clean install
With cucumber:-Dtest=Runnerclass test
Where Runnerclass is the class name of runner.
Include surefire plugin in your POM.xml to pick your test. if you want to run test as well, then use mvn clean install.
Refer
How to run Cucumber JVM Test in Jenkins

IntelliJ: Specifying different groovyc version for source and test within same module

I am using IntelliJ 11.1.3, IvyIDEA Plugin for IntelliJ, Spock 0.5 (which depends on Groovy 1.8.4), and Groovy 1.5.6.
I have an IntelliJ module that follows Maven Standard Directory Layout. The output is a WAR file.
When doing a build of this module in IntelliJ, is there any way that I can tell IntelliJ to use the following:
Groovy 1.5.6 to compile src/main/groovy
Groovy 1.8.4 to compile src/test/groovy
I need to do this because the actual application code needs to be compiled against Groovy 1.5.6 (long story short: the WAR that is generated is deployed with an EAR that must stay on Groovy 1.5.6 for now).
The Test directory, of course, does not get deployed with the WAR. The Test directory contains Spock Tests. I am using Spock 0.5, which requires Groovy 1.8.x
ivy.xml
<dependency conf="compile->default" name="groovy-all"
org="org.codehaus.groovy" rev="1.5.6"/>
<dependency conf="test->default" name="groovy-all"
org="org.codehaus.groovy" rev="1.8.4"/>
<dependency conf="test->default" name="spock-core"
org="org.spockframework" rev="0.5-groovy-1.8"/>
I need to be able to run tests from IntelliJ. If I build from IntelliJ and attempt to run a Spock Test, I get the following:
org.spockframework.runtime.InvalidSpecException:
Specification 'org.myorg.test.ProcessCaseTest' was not compiled properly
(Spock AST transform was not run); try to do a clean build
at org.spockframework.runtime.SpecUtil.checkIsSpec(SpecUtil.java:53)
If I remove this line:
<dependency conf="compile->default" name="groovy-all"
org="org.codehaus.groovy" rev="1.5.6"/>
then I can build and run Spock Tests, but I lose Groovy from my module.
I think what I need to do is tell IntelliJ to use one version of Groovy to compile the application code and another version of Groovy to compile the tests, but I have not been able to find a way to do this.
I would be extremely grateful for any help or insight that anyone could provide. Thank you very much.
IDEA follows the Maven dependency model where compile dependencies automatically make it to the test class path. What you can try is to reorder dependencies in the IDEA module so that Groovy 1.8 (which should have scope test) comes before Groovy 1.5 (which should have scope compile). Anyway, it's a hairy affair.
PS: I recommend to use Spock 0.6.

How to include only certain Java classes when executing "gradle test"

I have JUnit test classes in both the default Java package and my specific package. My Gradle build-file contains
test {
include 'edu/ucar/unidata/sruth/'
}
in order to exclude the JUnit test classes in the default Java package. It's not working: a gradle test always executes the default Java package tests as well as the package-specific tests (according to the contents of build/reports/test).
How do I execute only the package-specific tests?
I'm using Gradle 1.0.
ADDENDUM: Executing gradle cleanTest fixed the problem.
I extracted following segment from Gradle official doc, hope it helps http://www.gradle.org/docs/current/userguide/java_plugin.html
Some examples of using the command line option:
gradle test --tests org.gradle.SomeTest.someSpecificFeature
gradle test --tests *SomeTest.someSpecificFeature
gradle test --tests *SomeSpecificTest
gradle test --tests all.in.specific.package*
gradle test --tests *IntegTest
gradle test --tests *IntegTest*ui*
gradle someTestTask --tests *UiTest someOtherTestTask --tests
*WebTest*ui
not sure what JUNIt is but if you have two classes with the same name you have to specify which one you are talking about like... import java.util.Scanner; and to use it you would declare it as java.util.Scanner foo=new java.util.Scanner();

how to configure java junit with Teamcity?

Can somebody please tell me how to integrate java JUnit with TeamCity?
You can use the jUnit task in your Ant script that you have TeamCity run.
Current Teamcity version doesn't provide separate JUnit build step.
I guess it's because of test classpath determination problem.
You may provide classpath explicitly with ant junit task.
Or Teamcity can fetch test classpath from IDEA or IPR project. These two links explain how to run junit tests as part of compile build step.

Categories