1

This question has been asked before (e.g. here), but my observation was not the same as those previously reported.

I noticed that to get JUnit 5 to work, I must include the overall JUnit 5 artifact

testImplementation('org.junit.jupiter:junit-jupiter:5.5.1')

If I, instead, included the individual artifacts, then the JUnit test would not be picked up

testImplementation('org.junit.platform:junit-platform-runner:1.3.1')
testImplementation('org.junit.platform:junit-platform-launcher:1.0.0')
testImplementation('org.junit.jupiter:junit-jupiter-engine:5.5.1')
testImplementation('org.junit.jupiter:junit-jupiter-api:5.5.1')
testImplementation('org.junit.jupiter:junit-jupiter-params:5.5.1')
testImplementation('org.junit.vintage:junit-vintage-engine:5.5.1')

Has anyone seen something similar before?

(I also tried this with a non-Spring-Boot project -- in that case it was okay to include the individual artifacts. This was causing a lot of confusion.)

Here I am showing the result with gradle, but I have had similar result with maven too.

I am using Gradle 5.4.1, Spring Boot 2.1.7.RELEASE, and JUnit 5.5.1

I am including the full build.gradle and the test class below

build.gradle

plugins {
    id 'org.springframework.boot' version '2.1.7.RELEASE'
    id 'java'
}

apply plugin: 'io.spring.dependency-management'

group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'

repositories {
    mavenCentral()
}

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'

    testImplementation('org.junit.jupiter:junit-jupiter:5.5.1')

//  testImplementation('org.junit.platform:junit-platform-runner:1.3.1')
//  testImplementation('org.junit.platform:junit-platform-launcher:1.0.0')
//  testImplementation('org.junit.jupiter:junit-jupiter-engine:5.5.1')
//  testImplementation('org.junit.jupiter:junit-jupiter-api:5.5.1')
//  testImplementation('org.junit.jupiter:junit-jupiter-params:5.5.1')
//  testImplementation('org.junit.vintage:junit-vintage-engine:5.5.1')

}

test {
    useJUnitPlatform()
}

DemoApplicationTest.java

package com.example.demo;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

public class DemoApplicationTests {

    @Test
    public void failMe() {
        Assertions.assertTrue(Boolean.FALSE);
    }

}

Note that in this case I was expecting an exception be thrown in the test method failMe() -- so as to prove that the test method had been picked up by the runner and was not silently ignored.

leeyuiwah
  • 6,562
  • 8
  • 41
  • 71
  • 1
    Don’t know if that’s the reason but your versions don’t fit. With Jupiter 5.5.1 you should use platform runner and launcher 1.5.1. – johanneslink Aug 09 '19 at 19:41
  • Thanks! That explains it. I think I now understand better and I provided my own answer below. Please see if you agree. By the way, I think what is needed is the launcher. The runner is not needed in this case. Do you agree? – leeyuiwah Aug 09 '19 at 21:59

1 Answers1

2

Thanks for the hint from @johanneslink (in the comment of the opening question), now I think I understand better the issues:

It is better to use the aggregate artifact

testImplementation('org.junit.jupiter:junit-jupiter:5.5.1')

If you really want to use the individual artifacts, make sure their versions are compatible

This combination would work

testImplementation('org.junit.platform:junit-platform-launcher:1.5.1')
testImplementation('org.junit.jupiter:junit-jupiter-engine:5.5.1')
testImplementation('org.junit.jupiter:junit-jupiter-api:5.5.1')

But not this

testImplementation('org.junit.platform:junit-platform-launcher:1.3.1')
testImplementation('org.junit.jupiter:junit-jupiter-engine:5.5.1')
testImplementation('org.junit.jupiter:junit-jupiter-api:5.5.1')

(The other three artifacts are not relevant so I am omitting them here. For example, according to the JUnit 5 User Guide

junit-platform-runner

Runner for executing tests and test suites on the JUnit Platform in a JUnit 4 environment.

and is not relevant here.)

Community
  • 1
  • 1
leeyuiwah
  • 6,562
  • 8
  • 41
  • 71
  • 1
    The problem you’re facing is one of compatibility. Platform and Jupiter versions upgrade side by side with 1.x.y of platform corresponding to 5.x.y of Jupiter. The JUnit platform is (mostly) backwards compatible, that means you can run test engines build for older versions of the platform on newer versions. Making a platform forwards compatible is a much harder task, often not worth the effort. – johanneslink Aug 10 '19 at 09:19
  • 1
    I was having a similar issue with SpringBoot and using the aggregate artifact solved the problem for me. In my case I only used the engine and api artifacts (same version) as I saw on a baeldung tutorial. I still have not fully understood what the problem was. – marcellorvalle Jan 22 '20 at 17:56