micronaut-test
micronaut-test copied to clipboard
@MicronautTest on any of the superclasses
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 ApplicationContext
s, 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 ApplicationContext
s 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
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
https://github.com/micronaut-projects/micronaut-test/pull/585