micronaut-test icon indicating copy to clipboard operation
micronaut-test copied to clipboard

@MicronautTest on any of the superclasses

Open konrad-kaminski opened this issue 3 years ago • 2 comments

Expected Behavior

With Spock, if we have a base test class and a test class which inherits from it and @MicronautTest exists on the base class, the shared injected field values come from different ApplicationContexts, since two such contexts are created.

Actual Behaviour

Only one ApplicationContext should be created and used. The requirement to put @MicronautTest on the "outermost" superclass only is quite limiting. Furthermore I could not find any kind of documentation of it.

Steps To Reproduce

This is related to #370.

Let's say we have the following test classes:

@MicronautTest
class BaseSpec extends Specification {
    @Inject
    @Shared
    ApplicationContext baseCtx
}

class ConcreteSpec extends BaseSpec {
    @Inject
    @Shared
    ApplicationContext ctx

    void test() {
        expect:
        ctx == baseCtx
    }
}

This fails, because there are two separate ApplicationContexts created.

If we put @MicronautTest on ConcreteSpec it also fails. If we put @MicronautTest on ConcreteSpec AND remove @MicronautTest from BaseSpec it passes.

Environment Information

No response

Example Application

No response

Version

3.0.3

konrad-kaminski avatar Oct 29 '21 09:10 konrad-kaminski

There were several issues about this behavior but it's still there )) I've read about it cannot be solved by design (for now) in related issue but it's quite inconvenient. In our project we use next settings @MicronautTest(application = TestingApplication, rollback = false, transactional = false) and several common injects. So now we copy them in every test class. It isn't a big problem but it makes code a bit cluttering

katoquro avatar May 25 '22 15:05 katoquro

https://github.com/micronaut-projects/micronaut-test/pull/585

graemerocher avatar May 25 '22 15:05 graemerocher