From 7259a15f8a6916499acab2bf4fd3a1f6e1bcb34f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=81lvaro=20S=C3=A1nchez-Mariscal?= Date: Mon, 24 Aug 2020 10:16:23 +0200 Subject: [PATCH] Look for all fields. Fixes #138 (#252) --- .../spock/MicronautSpockExtension.java | 4 +- .../test/spock/SharedInjectionSpec.groovy | 51 +++++++++++++++++++ 2 files changed, 53 insertions(+), 2 deletions(-) create mode 100644 test-spock/src/test/groovy/io/micronaut/test/spock/SharedInjectionSpec.groovy diff --git a/test-spock/src/main/java/io/micronaut/test/extensions/spock/MicronautSpockExtension.java b/test-spock/src/main/java/io/micronaut/test/extensions/spock/MicronautSpockExtension.java index 173c7ff74..09a837590 100644 --- a/test-spock/src/main/java/io/micronaut/test/extensions/spock/MicronautSpockExtension.java +++ b/test-spock/src/main/java/io/micronaut/test/extensions/spock/MicronautSpockExtension.java @@ -92,7 +92,7 @@ public void visitSpecAnnotation(MicronautTest annotation, SpecInfo spec) { } } } else { - List fields = spec.getFields(); + List fields = spec.getAllFields(); for (FieldInfo field : fields) { if (field.isShared() && field.getAnnotation(Inject.class) != null) { applicationContext.inject(invocation.getSharedInstance()); @@ -217,7 +217,7 @@ protected void alignMocks(IMethodInvocation context, Object instance) { for (MethodInjectionPoint injectedMethod : specDefinition.getInjectedMethods()) { final Argument[] args = injectedMethod.getArguments(); if (args.length == 1) { - final Optional fld = context.getSpec().getFields().stream().filter(f -> f.getName().equals(args[0].getName())).findFirst(); + final Optional fld = context.getSpec().getAllFields().stream().filter(f -> f.getName().equals(args[0].getName())).findFirst(); if (fld.isPresent()) { final FieldInfo fieldInfo = fld.get(); final Object fieldInstance = fieldInfo.readValue( diff --git a/test-spock/src/test/groovy/io/micronaut/test/spock/SharedInjectionSpec.groovy b/test-spock/src/test/groovy/io/micronaut/test/spock/SharedInjectionSpec.groovy new file mode 100644 index 000000000..d823700df --- /dev/null +++ b/test-spock/src/test/groovy/io/micronaut/test/spock/SharedInjectionSpec.groovy @@ -0,0 +1,51 @@ +package io.micronaut.test.spock + +import io.micronaut.context.ApplicationContext +import io.micronaut.test.annotation.MicronautTest +import spock.lang.Shared +import spock.lang.Specification + +import javax.inject.Inject + +class AbstractExample extends Specification { + + @Inject + @Shared + ApplicationContext sharedCtx + + @Inject + ApplicationContext ctx + +} + +@MicronautTest +class FailingTest extends AbstractExample { + + def 'injection is not null'() { + expect: + ctx != null + } + + def 'shared injection is not null'() { + expect: + sharedCtx != null + } +} + +@MicronautTest +class SuccessfulTest extends AbstractExample { + + @Shared + @Inject + ApplicationContext dummy + + def 'injection is not null'() { + expect: + ctx != null + } + + def 'shared injection is not null'() { + expect: + sharedCtx != null + } +} \ No newline at end of file