Search code examples
unit-testinggradlebuild.gradlespockjunit5

Can we run Spock Testcases and Junit 5 test cases together In one project?


We are not able to run test cases written with Junit 5 and Spock framework together in one gardle project?

We tried add dependencies given in https://www.baeldung.com/junit-5-gradle to our gradle file. Gradle version is 4.10.3 and Junit 5. Below is my build.gradle file

apply plugin: 'groovy'
apply plugin: 'java'

repositories {
  mavenCentral()
  maven {
    url "http://repo.fusesource.com/nexus/content/groups/public/"
  }
  maven {
    url "https://repository.jboss.org/nexus/content/groups/public"
  }
  jcenter()
}

dependencies {

  compile group: 'com.google.inject', name: 'guice', version: '4.2.2'
  compile group: 'javax.servlet', name: 'javax.servlet-api', version: '3.0.1'

  testCompile(
    'org.codehaus.groovy:groovy-all:2.4.8',
    'org.spockframework:spock-core:1.0-groovy-2.4',
    'org.jmockit:jmockit:1.8',
    'junit:junit:4.12'
  )
  testRuntime(
    'cglib:cglib:2.2.2',
    'com.athaydes:spock-reports:1.2.7'
  )

  testImplementation 'org.junit.jupiter:junit-jupiter-api:5.3.1'
  testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.3.1'

  testCompileOnly 'junit:junit:4.12'

}

test {
  useJUnitPlatform()
  testLogging { showStandardStreams = true }
}

I have created two test cases, one is using spock framework and other is using junit 5. But when I do gradlew -test, it runs only test cases written with Junit 5. Below is build path.

enter image description here


Solution

  • You need the Vintage test engine to execute Spock tests since they are based on JUnit 4, and you need the Jupiter test engine to execute the JUnit Jupiter tests.

    So you need dependencies on both engines.

    testRuntimeOnly 'org.junit.vintage:junit-vintage-engine:5.3.1'
    testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.3.1'
    

    I'd also recommend that you upgrade to JUnit 5.5.1 (i.e., the latest and greatest).