Skip to content

Using @InjectSpy from a JUnit5 @Nested inner class leads to unreliable test result #32383

@lbilger

Description

@lbilger

Describe the bug

When I structure my tests in @Nested inner classes, the @InjectSpy annotation from the quarkus-junit5-mockito extension does not work as expected. It seems the annotation is processed twice, creating two spy instances. Then it becomes non-deterministic which of these two instances is injected into the application code. If the instance that the application gets is not the one that the test gets, mocking and/or verification will obviously fail.

Expected behavior

Only one spy is created.

Actual behavior

Multiple spies are created and it is non-deterministic which one is used. The test becomes flaky.

How to Reproduce?

@Path("/hello")
class ExampleResource(val exampleService: ExampleService) {
    @GET fun hello() = exampleService.hello()
}

@ApplicationScoped
class ExampleService {
    fun hello() = "Hello"
}

@QuarkusTest
class ExampleResourceTest {
    @InjectSpy
    private lateinit var exampleService: ExampleService

    @Nested
    inner class HelloTests {
        @Test
        fun `returns Hello`() {
            When { get("/hello") } Then { body(`is`("Hello")) }
        }

        @Test
        fun `returns Bonjour`() {
            doReturn("Bonjour").whenever(exampleService).hello()
            When { get("/hello") } Then { body(`is`("Bonjour")) }
        }
    }

    @Test
    fun `returns Hola`() {
        doReturn("Hola").whenever(exampleService).hello()
        When { get("/hello") } Then { body(`is`("Hola")) }
    }
}

When running the tests repeatedly, the Bonjour test is sometimes red, sometimes green. The others stay green.

Output of uname -a or ver

Darwin *** 21.6.0 Darwin Kernel Version 21.6.0: Mon Dec 19 20:44:01 PST 2022; root:xnu-8020.240.18~2/RELEASE_X86_64 x86_64

Output of java -version

openjdk version "17.0.5" 2022-10-18 OpenJDK Runtime Environment GraalVM CE 22.3.0 (build 17.0.5+8-jvmci-22.3-b08) OpenJDK 64-Bit Server VM GraalVM CE 22.3.0 (build 17.0.5+8-jvmci-22.3-b08, mixed mode, sharing)

GraalVM version (if different from Java)

No response

Quarkus version or git rev

2.13.7.Final-redhat-00003, also tried on 2.16.5.Final

Build tool (ie. output of mvnw --version or gradlew --version)

Apache Maven 3.8.6

Additional information

No response

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions