I'm writing unit tests and trying to use ExecutionCondition
for enabling the test only when specific profile activated exclusively.
I created my ExecutionCondition
.
class EnabledWithH2ExclusiveExecutionCondition implements ExecutionCondition {
@Override
public ConditionEvaluationResult evaluateExecutionCondition(
final ExtensionContext context) {
// check the environment
}
@Autowired
private Environment environment;
}
But the environment
is not injected.
How can I do that?
Because your ExecutionCondition
is created by JUnit5 itself using reflection .It is not managed by Spring and so the @Autowired
will not work.
You can call SpringExtension.getApplicationContext()
to get Spring Context and then get the Environment
from it :
@Override
public ConditionEvaluationResult evaluateExecutionCondition(final ExtensionContext context){
Environment env = SpringExtension.getApplicationContext(context).getEnvironment();
// check the environment
}