Spock tests failing with org.junit.runners.model.InvalidTestClassError: - gradle

After upgrading to spock-core 2.0 , my tests are failing with below error:
org.junit.runners.model.InvalidTestClassError: Invalid test class 'com.spock.test.TestSpockSpecification':
1. No runnable methods
at org.junit.runners.ParentRunner.validate(ParentRunner.java:525)
at org.junit.runners.ParentRunner.<init>(ParentRunner.java:102)
at org.junit.runners.BlockJUnit4ClassRunner.<init>(BlockJUnit4ClassRunner.java:84)
at org.junit.runners.JUnit4.<init>(JUnit4.java:23)
at org.junit.internal.builders.JUnit4Builder.runnerForClass(JUnit4Builder.java:10)
at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:70)
at org.junit.internal.builders.AllDefaultPossibilitiesBuilder.runnerForClass(AllDefaultPossibilitiesBuilder.java:37)
at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:70)
at org.junit.runners.model.RunnerBuilder.runners(RunnerBuilder.java:125)
at org.junit.runners.model.RunnerBuilder.runners(RunnerBuilder.java:111)
I am using below configuration in my build.gradle file :
testImplementation platform("org.spockframework:spock-bom:2.0-M5-groovy-3.0") //copied from spock example
testImplementation "org.spockframework:spock-core:2.0-M5-groovy-3.0"
implementation localGroovy() //3.0.7 as I am using gradle 7
I am running these test with junit 4 ,Can someone please help

Spock 2.0 builds on top of JUnit 5 and does not work with legacy JUnit 4. Also, it has been out for some time now, and there are no reasons to use milestones.
See this for a full example. Pay attention to the useJUnitPlatform part. E.g.:
dependencies {
testImplementation platform("org.codehaus.groovy:groovy-all:3.0.9")
testImplementation platform("org.spockframework:spock-bom:2.0-groovy-3.0")
testImplementation "org.spockframework:spock-core"
testRuntimeOnly "net.bytebuddy:byte-buddy:1.11.18" // allows mocking of classes (in addition to interfaces)
testRuntimeOnly "org.objenesis:objenesis:3.2" // allows mocking of classes without default constructor (together with ByteBuddy or CGLIB)
}
test {
useJUnitPlatform()
}

Related

Avoid duplicating dependencies in a Gradle testing suite?

If I define a new Gradle testing suite as per the doco, I have to manually duplicate dependencies from the main project into the test suite.
The version catalogue stuff can help eliminate the hardcoded version numbers.
But how can I just say "give dev the same dependencies as test"?
dependencies{
// don't really care, whever version Gradle is using will do
implementation localGroovy()
// look in settings.gradle for version catalog specs
implementation libs.jooq
runtimeOnly libs.pgjdbc
jooqGenerator libs.pgjdbc
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.8.1'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.8.1'
}
testing{
suites{
/* "dev" code/sql, etc. is "scratch" stuff that is committed, but is not
production code, not real tests. Just WIP/temporary stuff, where the
usual standards don't apply (but still - no credentials!) */
dev(JvmTestSuite){
testType = TestSuiteType.INTEGRATION_TEST
dependencies{
implementation project
// doesn't work: No signature of method: build_999.testing() is applicable for argument types
// implementation localGroovy()
// copy/pasted from dependencies block - yuck :(
implementation 'org.codehaus.groovy:groovy-all:3.0.9'
implementation libs.jooq
runtimeOnly libs.pgjdbc
}
}
}
}
dev{
useJUnitPlatform()
systemProperties defaultSysProps
}
Gradle 7.4.2

How to configure the hamcrest dependency for gradle?

Again an abslout beginer question :-(
My gradle version is: Gradle 6.3
I initilized a small gradle project using gradle init for java and junit5
to learn junit5 and jmockit :-)
I tried to add some tutorial classes but gradle cannot resolve the hamcrest dependency :-(
// https://mvnrepository.com/artifact/org.hamcrest/hamcrest
testImplementation 'org.hamcrest:hamcrest:2.2'
as well as
// https://mvnrepository.com/artifact/org.hamcrest/hamcrest-all
testImplementation 'org.hamcrest:hamcrest-all:1.3'
on the hamcrest web site, this hint is given
http://hamcrest.org/JavaHamcrest/distributables#using-hamcrest-in-a-gradle-project
here my build.gradle file:
/*
* This file was generated by the Gradle 'init' task.
*
* This generated file contains a sample Java project to get you started.
* For more details take a look at the Java Quickstart chapter in the Gradle
* User Manual available at https://docs.gradle.org/6.3/userguide/tutorial_java_projects.html
*/
plugins {
// Apply the java plugin to add support for Java
id 'java'
// Apply the application plugin to add support for building a CLI application.
id 'application'
}
repositories {
// Use jcenter for resolving dependencies.
// You can declare any Maven/Ivy/file repository here.
jcenter()
mavenCentral()
google()
}
dependencies {
// This dependency is used by the application.
implementation 'com.google.guava:guava:28.2-jre'
testImplementation 'org.hamcrest:hamcrest:2.2'
// Use JUnit Jupiter API for testing.
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.6.0'
// Use JUnit Jupiter Engine for testing.
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.6.0'
}
application {
// Define the main class for the application.
mainClassName = 'jmockit_examples.App'
}
test {
// Use junit platform for unit tests
useJUnitPlatform()
}

Junit 5 test cases are not executing in gradle 4.4

My junit 5 test cases are not executing.
Can anyone suggest some solution?
Gradle version is 4.4
You will need Gradle 4.6 or later to get support for JUnit 5.
Once you upgrade Gradle, be sure to configure it for JUnit 5. See the user guide for details. For example:
// build.gradle (Groovy DSL)
test {
useJUnitPlatform()
}
dependencies {
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.6.0'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine'
}

Starting with IntelliJ and Gradle : how to do jUnit tests?

Good evening,
because I want to initiate myself to LibGDX, I recently gave a try to IntelliJ Idea IDE and Gradle instead of my old Eclipse-Maven habits.
I have to recognize that such a change is not easy because I really don't find anything.
To start learning I created a project with a simple Pojo and also a unit test class.
I have no error in the editor, both Pojo and jUnit seem OK, but when I launch the unit test, I get such errors :
Can someone help me understand what's going wrong ?
EDIT : build.gradle file content :
plugins {
id 'java'
}
group 'com.citizenweb.training'
version '1.0-SNAPSHOT'
repositories {
mavenCentral()
}
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.12'
// https://mvnrepository.com/artifact/org.projectlombok/lombok
compile group: 'org.projectlombok', name: 'lombok', version: '1.18.16'
}
Thanx by advance.
It seems you did not configure lombok dependencies properly: your test classes cannot see lombok-generated stuff (getters, setters, build). Lombok is based on annotation processor so you need to declare following dependencies in your build.gradle :
ext {
lombokVersion = "1.18.6"
}
dependencies {
// Lombok
compileOnly ("org.projectlombok:lombok:${lombokVersion}")
annotationProcessor ("org.projectlombok:lombok:${lombokVersion}")
// to make lombok available for test classes
testCompileOnly ("org.projectlombok:lombok:${lombokVersion}")
testAnnotationProcessor ("org.projectlombok:lombok:${lombokVersion}")
testImplementation("junit:junit:4.12")
}

Spring Boot/Gradle/Querydsl project has the same dependency dependent on different versions of another dependency

I am having problems adding Querydsl to my Spring Boot project. I have the following dependencies in my build.gradle file:
dependencies {
annotationProcessor 'com.querydsl:querydsl-apt:4.1.3:jpa'
annotationProcessor 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'com.querydsl:querydsl-jpa:4.1.3'
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.springframework.boot:spring-boot-starter-security'
implementation 'org.springframework.cloud:spring-cloud-starter-netflix-eureka-client'
implementation 'org.springframework.security.oauth.boot:spring-security-oauth2-autoconfigure:2.1.3.RELEASE'
implementation 'org.springframework.cloud:spring-cloud-config-client:2.1.0.RELEASE'
implementation 'mysql:mysql-connector-java'
implementation 'io.springfox:springfox-swagger2:2.9.2'
implementation 'io.springfox:springfox-swagger-ui:2.9.2'
implementation 'io.springfox:springfox-bean-validators:2.9.2'
implementation group: 'org.latencyutils', name: 'LatencyUtils', version: '2.0.3'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testImplementation 'org.springframework.security:spring-security-test'
testImplementation 'junit:junit:4.12'
testImplementation 'org.mockito:mockito-inline:2.23.4'
}
The project builds fine, but when I run it, I get the following error:
An attempt was made to call the method com.google.common.collect.FluentIterable.concat(Ljava/lang/Iterable;Ljava/lang/Iterable;)Lcom/google/common/collect/FluentIterable; but it does not exist. Its class, com.google.common.collect.FluentIterable, is available from the following locations:
jar:file:/C:/Users/me/.gradle/caches/modules-2/files-2.1/com.google.guava/guava/18.0/cce0823396aa693798f8882e64213b1772032b09/guava-18.0.jar!/com/google/common/collect/FluentIterable.class
jar:file:/C:/Users/me/.gradle/caches/modules-2/files-2.1/com.google.guava/guava/20.0/89507701249388e1ed5ddcf8c41f4ce1be7831ef/guava-20.0.jar!/com/google/common/collect/FluentIterable.class
It was loaded from the following location:
file:/C:/Users/me/.gradle/caches/modules-2/files-2.1/com.google.guava/guava/18.0/cce0823396aa693798f8882e64213b1772032b09/guava-18.0.jar
Action:
Correct the classpath of your application so that it contains a single, compatible version of com.google.common.collect.FluentIterable
The Gradle window in IntelliJ shows the following:
Why does each instance of com.querydsl:querydsl-core:4.2.1 depend on a different version of guava?
The version 18.0 of Guava comes from querydsl and the 20.0 comes from spring-fox swagger.
spring-fox library expected that method of guava version 20.0 to be called. but called from 18.0. (because there are two guava library that has difference version in classpath)
you can use the following code to avoid conflict version of guava.
// QueryDsl
implementation("com.querydsl:querydsl-jpa:${querydslVersion}")
annotationProcessor("org.hibernate.javax.persistence:hibernate-jpa-2.1-api:1.0.2.Final")
annotationProcessor("com.querydsl:querydsl-apt:${querydslVersion}:jpa") {
exclude group: "come.google.guava"
}
annotationProcessor("com.google.guava:guava:20.0")

Resources