junit5 icon indicating copy to clipboard operation
junit5 copied to clipboard

Method order not adhering to @Order

Open ergunp opened this issue 7 years ago • 24 comments

Currently using 5.4.0 with Kotlin 1.3.21 and Java 8 and with Intellij 2018.3

When I add @TestMethodOrder(OrderAnnotation::class) with Order annotation the order doesn't seem to run in what I was expecting for example, I expect this method to run first

   @Test
    @Order(1)
    fun `add a session to cache`() {

        val header = HttpHeaders()
        header.contentType = MediaType.APPLICATION_JSON

        val json = "{\"protocolRestriction\":false}"

        val entity = HttpEntity(json, header)

        val request = restTemplate.postForEntity<String>("/session/10.36.55.1/create",entity)

        assertThat(request.statusCode).isEqualTo(HttpStatus.OK)
    }

but this method is the last one to run.

here is my full code,

package sky.wifi.sessionmanager

import org.assertj.core.api.Assertions.assertThat
import org.junit.jupiter.api.*
import org.junit.jupiter.api.MethodOrderer.OrderAnnotation;
import org.junit.jupiter.api.extension.ExtendWith
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.test.context.SpringBootTest
import org.springframework.boot.test.web.client.TestRestTemplate
import org.springframework.boot.test.web.client.getForEntity
import org.springframework.boot.test.web.client.postForEntity
import org.springframework.http.HttpEntity
import org.springframework.http.HttpHeaders
import org.springframework.http.HttpStatus
import org.springframework.http.MediaType
import org.springframework.test.context.ActiveProfiles
import org.springframework.test.context.junit.jupiter.SpringExtension

@ExtendWith(SpringExtension::class)
@ActiveProfiles("qa")
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
@TestMethodOrder(OrderAnnotation::class)
class ControllerTest(@Autowired var restTemplate: TestRestTemplate) {

    @Test
    @Order(1)
    fun `add a session to cache`() {

        val header = HttpHeaders()
        header.contentType = MediaType.APPLICATION_JSON

        val json = "{\"protocolRestriction\":false}"

        val entity = HttpEntity(json, header)

        val request = restTemplate.postForEntity<String>("/session/10.36.55.1/create",entity)

        assertThat(request.statusCode).isEqualTo(HttpStatus.OK)
    }

    @Test
    @Order(2)
    fun `get all sessions from empty cache`() {

        val entity = restTemplate.getForEntity<String>("/session/all")

        assertThat(entity.statusCode).isEqualTo(HttpStatus.OK)

        assertThat(entity.body).isNotEmpty()
    }

    @Test
    @Order(3)
    fun `update a session from cache`() {

        val header = HttpHeaders()
        header.contentType = MediaType.APPLICATION_JSON

        val entity = HttpEntity("", header)

        val request = restTemplate.postForEntity<String>("/session/10.36.55.1/update",entity)

        assertThat(request.statusCode).isEqualTo(HttpStatus.OK)
    }

    @Test
    @Order(4)
    fun `delete a session from cache`() {

        val header = HttpHeaders()
        header.contentType = MediaType.APPLICATION_JSON

        val entity = HttpEntity("", header)

        val request = restTemplate.postForEntity<String>("/session/10.36.55.1/delete",entity)

        assertThat(request.statusCode).isEqualTo(HttpStatus.OK)
    }

}

here is a screenshot of the test methods: https://cdn1.imggmi.com/uploads/2019/2/15/6465ce0507fcb3e6f3bd2e1340df0c7a-full.png

ergunp avatar Feb 15 '19 09:02 ergunp

Thanks for raising the issue.

We'll look into it.

sbrannen avatar Feb 15 '19 10:02 sbrannen

Tentatively slated for 5.5 M1 for investigation.

sbrannen avatar Feb 15 '19 10:02 sbrannen

@ergunp Are you using Spring Boot's BOM or the dependency management features of Spring Boot's Gradle plugin? Which version of the junit-jupiter-engine do you have on your classpath?

marcphilipp avatar Feb 15 '19 11:02 marcphilipp

@marcphilipp here is my build.gradle file

buildscript {
    ext {
        kotlinVersion = '1.3.21'
        springBootVersion = '2.1.2.RELEASE'
    }
    repositories {
        mavenCentral()
        jcenter()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
        classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:${kotlinVersion}")
        classpath("org.jetbrains.kotlin:kotlin-allopen:${kotlinVersion}")
        classpath("org.jetbrains.kotlin:kotlin-noarg:${kotlinVersion}")
        classpath("com.bmuschko:gradle-docker-plugin:4.4.1")
        classpath("se.transmode.gradle:gradle-docker:1.2")
    }
}

apply plugin: 'kotlin'
apply plugin: 'kotlin-spring'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
apply plugin: 'kotlin-kapt'
apply plugin: "kotlin-jpa"
apply plugin: "kotlin-noarg"
apply plugin: "idea"
apply plugin: "docker"


group = 'sky.wifi'

bootJar {
    baseName = 'sessionmanager'
    version = '0.0.0-SNAPSHOT'
}

sourceCompatibility = '1.8'

repositories {
    mavenCentral()
}

task buildDocker(type: Docker, dependsOn: build) {
    baseImage "openjdk:8-jdk-alpine"
    maintainer ""
    addFile "build/libs/sessionmanager-0.0.0-SNAPSHOT.jar", "sessionmanager.jar"
    exposePort 8282
    volume "/tmp"
    tag "tc-te.thecloud.net:5443/sessionmanager"
    entryPoint(["java","-Djava.security.egd=file:/dev/./urandom","-jar","/sessionmanager.jar"])
    
}


dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
    implementation 'org.springframework.boot:spring-boot-starter-data-redis'
    implementation 'org.springframework.boot:spring-boot-starter-data-rest'
    implementation 'org.springframework.boot:spring-boot-starter-web'
    implementation 'org.springframework.boot:spring-boot-starter-actuator'
    implementation 'org.springframework.cloud:spring-cloud-starter-netflix-hystrix:2.1.0.RELEASE'
    implementation 'org.springframework.cloud:spring-cloud-netflix-hystrix-dashboard:2.1.0.RELEASE'



    implementation 'com.fasterxml.jackson.module:jackson-module-kotlin'
    implementation 'org.jetbrains.kotlin:kotlin-stdlib-jdk8'
    implementation 'org.jetbrains.kotlin:kotlin-reflect'

    implementation 'com.google.guava:guava:27.0.1-jre'

    implementation "org.postgresql:postgresql:42.2.5"

    implementation 'com.github.seancfoley:ipaddress:5.0.0'
    implementation 'commons-net:commons-net:3.6'

    implementation 'redis.clients:jedis:2.10.2'

    implementation 'io.micrometer:micrometer-registry-graphite:latest.release'
    implementation 'com.fasterxml.jackson.module:jackson-module-kotlin:2.9.8'


    kapt("org.springframework.boot:spring-boot-configuration-processor")

    runtimeOnly 'org.springframework.boot:spring-boot-devtools'

    runtimeOnly 'org.postgresql:postgresql'

    testCompile 'org.postgresql:postgresql'

    testCompile('org.springframework.boot:spring-boot-starter-test') {
        exclude module: 'junit'
    }
    testImplementation('org.junit.jupiter:junit-jupiter-api:5.4.0')
    testRuntimeOnly('org.junit.jupiter:junit-jupiter-engine')
    testCompile("com.nhaarman.mockitokotlin2:mockito-kotlin:2.1.0")
    testCompile('it.ozimov:embedded-redis:0.7.2')
    testCompile('org.mockito:mockito-junit-jupiter:2.23.4')
    testCompile('org.mockito:mockito-inline:2.23.4')
}

test {
    useJUnitPlatform()
}

compileJava.dependsOn(processResources)

compileKotlin {
    kotlinOptions {
        freeCompilerArgs = ['-Xjsr305=strict']
        jvmTarget = '1.8'
    }
}

compileTestKotlin {
    kotlinOptions {
        freeCompilerArgs = ['-Xjsr305=strict']
        jvmTarget = '1.8'
    }
}


noArg {
    annotation("sky.wifi.sessionmanager.model")
}

allOpen {
    annotation("javax.persistence.Entity")
    annotation("javax.persistence.MappedSuperclass")
    annotation("javax.persistence.Embeddable")
}

ergunp avatar Feb 15 '19 16:02 ergunp

You don't have 5.4.0 set as the version for the junit-jupiter-engine.

Try adding the following to your build script.

ext['junit-jupiter.version'] = '5.4.0'

That should override the JUnit Jupiter version managed by Spring Boot.

You should then be able to remove the 5.4.0 declaration for the junit-jupiter-api dependency as well.

Let us know if that helps!

sbrannen avatar Feb 15 '19 16:02 sbrannen

@sbrannen I can verify that this has resolved the issue. Thanks for the help guys.

ergunp avatar Feb 15 '19 17:02 ergunp

You're welcome, and thanks for letting us know it works.

sbrannen avatar Feb 16 '19 10:02 sbrannen

Hello,

Removed the below, org.junit.jupiter junit-jupiter-api 5.4.0 test

Added the below --> org.junit.jupiter junit-jupiter-engine ${junit-jupiter.version}

The actual order problem in Integration tests (using Arquillian library) is resolved but the junit tests are not being discovered during the build.

ghost avatar Jun 11 '20 10:06 ghost

I have this problem. I am using <junit-jupiter.version>5.7.0</junit-jupiter.version>. Anyone still willing to work on this issue?

Danc2050 avatar Oct 13 '20 02:10 Danc2050

I use version 5.7.0 of junit with maven and the @Order(number) annotation is not working; my maven dependency:

<dependency>
  <groupId>software.amazon.qldb</groupId>
  <artifactId>amazon-qldb-driver-java</artifactId>
  <version>2.0.0</version>
</dependency>

<dependency>
  <groupId>com.amazonaws</groupId>
  <artifactId>aws-java-sdk-qldb</artifactId>
  <version>1.11.785</version>
</dependency>

<dependency>
  <groupId>com.fasterxml.jackson.dataformat</groupId>
  <artifactId>jackson-dataformat-ion</artifactId>
  <version>2.11.3</version>
</dependency>

<dependency>
<groupId>org.apache.logging.log4j</groupId>
  <artifactId>log4j-slf4j-impl</artifactId>
  <version>2.13.3</version>
  <scope>test</scope>
</dependency>

<dependency>
  <groupId>org.junit.jupiter</groupId>
  <artifactId>junit-jupiter-engine</artifactId>
  <version>5.7.0</version>
  <scope>test</scope>
</dependency>

deionut avatar Nov 02 '20 14:11 deionut

@Danc2050 and @deionut, which @Order annotation are you using?

Can you verify that it is org.junit.jupiter.api.Order?

sbrannen avatar Nov 02 '20 14:11 sbrannen

Same issue for me on IntelliJ 2020.3.1. I have annotated my class with @TestMethodOrder(OrderAnnotation.class) and methods with org.junit.jupiter.api.Order

imavroukakis avatar Jan 27 '21 10:01 imavroukakis

@sbrannen I have the same issue. I have used org.junit.jupiter.api.Order and my pom file is

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>testrest</groupId>
  <artifactId>Junit-test-rest</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <name>Junit-test-rest</name>
  <properties>
  	<maven.compiler.target>15</maven.compiler.target>
  	<maven.compiler.source>15</maven.compiler.source>
  	<junit.jupiter.version>5.4.0</junit.jupiter.version>
  </properties>
  <dependencies>
  	<dependency>
  		<groupId>org.junit.jupiter</groupId>
  		<artifactId>junit-jupiter-engine</artifactId>
  		<version>${junit.jupiter.version}</version>
  		<scope>test</scope>
  	</dependency>
  	<dependency>
  		<groupId>org.junit.jupiter</groupId>
  		<artifactId>junit-jupiter-api</artifactId>
  		<version>${junit.jupiter.version}</version>
  		<scope>test</scope>
  	</dependency>   
  </dependencies>
  
  <build>
  	<plugins>
  		<plugin>
			<artifactId>maven-surefire-plugin</artifactId>
			<version>2.22.1</version>
  		</plugin>
  	</plugins>
  </build>
</project>

AdityaYadav29 avatar Feb 12 '21 08:02 AdityaYadav29

Same issue for me on IntelliJ 2020.3.1. I have annotated my class with @TestMethodOrder(OrderAnnotation.class) and methods with org.junit.jupiter.api.Order

@imavroukakis, that should work.

@marcphilipp and @sormuras, can one of you verify that a recent version of IDEA properly supports ordered test method execution?

sbrannen avatar Feb 12 '21 10:02 sbrannen

@sbrannen I have the same issue. I have used org.junit.jupiter.api.Order and my pom file is

@AdityaYadav29, what makes you think the tests are being executed in the wrong order?

sbrannen avatar Feb 12 '21 10:02 sbrannen

Same issue for me on IntelliJ 2020.3.1. I have annotated my class with @TestMethodOrder(OrderAnnotation.class) and methods with org.junit.jupiter.api.Order

@imavroukakis, that should work.

@marcphilipp and @sormuras, can one of you verify that a recent version of IDEA properly supports ordered test method execution?

On latest stable IntelliJ (2020.3.2) image

image

I think you are correct @sbrannen, this is an IntelliJ presentation issue. Sticking debug println statements in the tests shows that the order is respected

Condition provided in @EnabledIf evaluates to false
I am second

Environment variable [ENV] does not exist ==> Only runs on Jenkins

Environment variable [ENV] does not exist
I am fifth
I am sixth
I am seventh

Disabled on JRE version: 11.0.9.1

Disabled on operating system: Mac OS X

Disabled on operating system: Mac OS X
I am eleventh

imavroukakis avatar Feb 12 '21 11:02 imavroukakis

I think you are correct @sbrannen, this is an IntelliJ presentation issue.

Indeed. It appears that IDEA is simply displaying the tests in alphabetical order, disregarding the order in which they were executed.

I would consider that a bug in IDEA.

@akozlova, what do you think?

Sticking debug println statements in the tests shows that the order is respected

I was going to suggest such a technique for determining the actual execution order. Glad you figured it out on your own.

And... thanks for providing feedback!

sbrannen avatar Feb 12 '21 15:02 sbrannen

  1. IJ has toggle action Sort alphabetically, please check if it is not enabled for you (I don't see a toolbar on the screenshot...)
  2. IJ displays tests in order they were discovered because the tests are shown before they were actually executed. [I didn't check] but if the order is applied after test plan is ready, then presentation issues are expected as it would be visually strange to re-sort tests during run. I can disable pre-fill of tests in the tree but it would make support much less convenient, I believe

akozlova avatar Feb 12 '21 20:02 akozlova

Thanks for the feedback, @akozlova!

IJ has toggle action Sort alphabetically, please check if it is not enabled for you (I don't see a toolbar on the screenshot...)

@imavroukakis, does that work for you?

IJ displays tests in order they were discovered because the tests are shown before they were actually executed. [I didn't check] but if the order is applied after test plan is ready, then presentation issues are expected as it would be visually strange to re-sort tests during run. I can disable pre-fill of tests in the tree but it would make support much less convenient, I believe

The org.junit.jupiter.engine.discovery.MethodOrderingVisitor is applied by the org.junit.jupiter.engine.discovery.DiscoverySelectorResolver during the discovery phase. So the reported test method order should be correct before the execution phase.

sbrannen avatar Feb 13 '21 15:02 sbrannen

Thanks for the feedback, @akozlova!

IJ has toggle action Sort alphabetically, please check if it is not enabled for you (I don't see a toolbar on the screenshot...)

@imavroukakis, does that work for you?

🤦 why didn't I think of this in the first place. Yes, once I removed alpha sorting they come out in the right order. I might argue that, in the presence of strict ordering IJ should drop alphabetical sorting and notify the user - principle of least surprise..but at least I know now I should disable sorting permanently 😀

imavroukakis avatar Feb 13 '21 16:02 imavroukakis