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

1

6

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

Turnaround answered 21/8, 2019 at 6:6 Comment(0)
T
15

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).

Thessa answered 21/8, 2019 at 16:12 Comment(3)
Sam, Thanks for reply. I tried using vintage test engine, but now gradle is only executing spock test cases and junit test cases are not being executed. I need to run them both when I run "gradlew clean test" command.Turnaround
You also need the junit-jupiter-engine. I'm sorry if my wording was unclear. I'll improve that now.Thessa
It worked... thank you so much... I will try to run some more scenarios...Turnaround

© 2022 - 2024 — McMap. All rights reserved.