Skip to content

Commit

Permalink
Look for all fields. Fixes #138 (#252)
Browse files Browse the repository at this point in the history
  • Loading branch information
alvarosanchez authored Aug 24, 2020
1 parent 8ac3daf commit 7259a15
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public void visitSpecAnnotation(MicronautTest annotation, SpecInfo spec) {
}
}
} else {
List<FieldInfo> fields = spec.getFields();
List<FieldInfo> fields = spec.getAllFields();
for (FieldInfo field : fields) {
if (field.isShared() && field.getAnnotation(Inject.class) != null) {
applicationContext.inject(invocation.getSharedInstance());
Expand Down Expand Up @@ -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<FieldInfo> fld = context.getSpec().getFields().stream().filter(f -> f.getName().equals(args[0].getName())).findFirst();
final Optional<FieldInfo> 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(
Expand Down
Original file line number Diff line number Diff line change
@@ -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
}
}

0 comments on commit 7259a15

Please sign in to comment.