Package org.apache.sling.testing.mock.sling.junit5

# Class SlingContextExtension

java.lang.Object
org.apache.sling.testing.mock.sling.junit5.SlingContextExtension

All Implemented Interfaces:
  `org.junit.jupiter.api.extension.AfterEachCallback`, `org.junit.jupiter.api.extension.AfterTestExecutionCallback`, `org.junit.jupiter.api.extension.BeforeEachCallback`, `org.junit.jupiter.api.extension.Extension`, `org.junit.jupiter.api.extension.ParameterResolver`, `org.junit.jupiter.api.extension.TestInstancePostProcessor`

public final class SlingContextExtension extends Object implements org.junit.jupiter.api.extension.ParameterResolver, org.junit.jupiter.api.extension.TestInstancePostProcessor, org.junit.jupiter.api.extension.BeforeEachCallback, org.junit.jupiter.api.extension.AfterEachCallback, org.junit.jupiter.api.extension.AfterTestExecutionCallback
JUnit 5 extension that allows to inject `SlingContext` (or subclasses of it) parameters in test methods, and ensures that the context is set up and teared down properly for each test method.

## Constructor Summary

Constructors
Constructor
Description
`SlingContextExtension()`

## Method Summary

Modifier and Type
Method
Description
`void`
`afterEach(org.junit.jupiter.api.extension.ExtensionContext extensionContext)`
`void`
`afterTestExecution(org.junit.jupiter.api.extension.ExtensionContext extensionContext)`
`void`
`beforeEach(org.junit.jupiter.api.extension.ExtensionContext extensionContext)`
`void`
`postProcessTestInstance(Object testInstance, org.junit.jupiter.api.extension.ExtensionContext extensionContext)`
Checks if test class has a `SlingContext` or derived field.
`Object`
`resolveParameter(org.junit.jupiter.api.extension.ParameterContext parameterContext, org.junit.jupiter.api.extension.ExtensionContext extensionContext)`
Resolve (or create) `SlingContext` instance for test method parameter.
`boolean`
`supportsParameter(org.junit.jupiter.api.extension.ParameterContext parameterContext, org.junit.jupiter.api.extension.ExtensionContext extensionContext)`
Support parameter injection for test methods of parameter type is derived from `SlingContext`.

### Methods inherited from class java.lang.Object

`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

## Constructor Details

### SlingContextExtension

public SlingContextExtension()

## Method Details

### postProcessTestInstance

public void postProcessTestInstance(Object testInstance, org.junit.jupiter.api.extension.ExtensionContext extensionContext) throws Exception
Checks if test class has a `SlingContext` or derived field. If it has and is not instantiated, create an new `SlingContext` and store it in the field. If it is already instantiated reuse this instance and use it for all test methods.

Specified by:
  `postProcessTestInstance` in interface `org.junit.jupiter.api.extension.TestInstancePostProcessor`
Throws:
  `Exception`

### supportsParameter

public boolean supportsParameter(org.junit.jupiter.api.extension.ParameterContext parameterContext, org.junit.jupiter.api.extension.ExtensionContext extensionContext)
Support parameter injection for test methods of parameter type is derived from `SlingContext`.

Specified by:
  `supportsParameter` in interface `org.junit.jupiter.api.extension.ParameterResolver`

### resolveParameter

public Object resolveParameter(org.junit.jupiter.api.extension.ParameterContext parameterContext, org.junit.jupiter.api.extension.ExtensionContext extensionContext)
Resolve (or create) `SlingContext` instance for test method parameter.

Specified by:
  `resolveParameter` in interface `org.junit.jupiter.api.extension.ParameterResolver`

### beforeEach

public void beforeEach(org.junit.jupiter.api.extension.ExtensionContext extensionContext) throws Exception

Specified by:
  `beforeEach` in interface `org.junit.jupiter.api.extension.BeforeEachCallback`
Throws:
  `Exception`

### afterTestExecution

public void afterTestExecution(org.junit.jupiter.api.extension.ExtensionContext extensionContext) throws Exception

Specified by:
  `afterTestExecution` in interface `org.junit.jupiter.api.extension.AfterTestExecutionCallback`
Throws:
  `Exception`

### afterEach

public void afterEach(org.junit.jupiter.api.extension.ExtensionContext extensionContext)

Specified by:
  `afterEach` in interface `org.junit.jupiter.api.extension.AfterEachCallback`