Skip to content

Commit

Permalink
Provide test instance to TestTemplate extensions
Browse files Browse the repository at this point in the history
Issue: #419
  • Loading branch information
sbrannen committed Jun 30, 2017
1 parent 4422e64 commit fa8dc48
Show file tree
Hide file tree
Showing 3 changed files with 231 additions and 99 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,13 @@
final class TestTemplateContainerExtensionContext extends AbstractExtensionContext<TestTemplateTestDescriptor>
implements ContainerExtensionContext {

private final Object testInstance;

TestTemplateContainerExtensionContext(ExtensionContext parent, EngineExecutionListener engineExecutionListener,
TestTemplateTestDescriptor testDescriptor) {
TestTemplateTestDescriptor testDescriptor, Object testInstance) {

super(parent, engineExecutionListener, testDescriptor);
this.testInstance = testInstance;
}

@Override
Expand All @@ -45,8 +49,7 @@ public Optional<Class<?>> getTestClass() {

@Override
public Optional<Object> getTestInstance() {
// TODO Determine how to access the test instance
return null;
return Optional.ofNullable(this.testInstance);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,17 @@ public boolean mayRegisterTests() {
public JupiterEngineExecutionContext prepare(JupiterEngineExecutionContext context) throws Exception {
ExtensionRegistry registry = populateNewExtensionRegistryFromExtendWith(getTestMethod(),
context.getExtensionRegistry());
ContainerExtensionContext testExtensionContext = new TestTemplateContainerExtensionContext(
context.getExtensionContext(), context.getExecutionListener(), this);

// The test instance should be properly maintained by the enclosing class's ExtensionContext.
Object testInstance = context.getExtensionContext().getTestInstance().orElse(null);

ContainerExtensionContext containerExtensionContext = new TestTemplateContainerExtensionContext(
context.getExtensionContext(), context.getExecutionListener(), this, testInstance);

// @formatter:off
return context.extend()
.withExtensionRegistry(registry)
.withExtensionContext(testExtensionContext)
.withExtensionContext(containerExtensionContext)
.build();
// @formatter:on
}
Expand Down
Loading

0 comments on commit fa8dc48

Please sign in to comment.