Java Code Examples for org.junit.jupiter.api.extension.ExtensionContext#getRequiredTestInstance()

The following examples show how to use org.junit.jupiter.api.extension.ExtensionContext#getRequiredTestInstance() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SpringExtension.java    From java-technology-stack with MIT License 7 votes vote down vote up
/**
 * Delegates to {@link TestContextManager#beforeTestExecution}.
 */
@Override
public void beforeTestExecution(ExtensionContext context) throws Exception {
	Object testInstance = context.getRequiredTestInstance();
	Method testMethod = context.getRequiredTestMethod();
	getTestContextManager(context).beforeTestExecution(testInstance, testMethod);
}
 
Example 2
Source File: SpringExtension.java    From java-technology-stack with MIT License 7 votes vote down vote up
/**
 * Delegates to {@link TestContextManager#afterTestExecution}.
 */
@Override
public void afterTestExecution(ExtensionContext context) throws Exception {
	Object testInstance = context.getRequiredTestInstance();
	Method testMethod = context.getRequiredTestMethod();
	Throwable testException = context.getExecutionException().orElse(null);
	getTestContextManager(context).afterTestExecution(testInstance, testMethod, testException);
}
 
Example 3
Source File: SpringExtension.java    From spring-analysis-note with MIT License 6 votes vote down vote up
/**
 * Delegates to {@link TestContextManager#afterTestMethod}.
 */
@Override
public void afterEach(ExtensionContext context) throws Exception {
	Object testInstance = context.getRequiredTestInstance();
	Method testMethod = context.getRequiredTestMethod();
	Throwable testException = context.getExecutionException().orElse(null);
	getTestContextManager(context).afterTestMethod(testInstance, testMethod, testException);
}
 
Example 4
Source File: ScreenshotOnFailureExtension.java    From blog-tutorials with MIT License 6 votes vote down vote up
@Override
public void afterEach(ExtensionContext extensionContext) throws Exception {
  if (extensionContext.getExecutionException().isPresent()) {
    Object testInstance = extensionContext.getRequiredTestInstance();
    Field containerField = testInstance.getClass().getDeclaredField("container");
    containerField.setAccessible(true);
    BrowserWebDriverContainer browserContainer = (BrowserWebDriverContainer) containerField.get(testInstance);
    byte[] screenshot = browserContainer.getWebDriver().getScreenshotAs(OutputType.BYTES);

    try {
      Path path = Paths
        .get("target/selenium-screenshots")
        .resolve(String.format("%s-%s-%s.png",
          LocalDateTime.now(),
          extensionContext.getRequiredTestClass().getName(),
          extensionContext.getRequiredTestMethod().getName()));

      Files.createDirectories(path.getParent());
      Files.write(path, screenshot);
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
}
 
Example 5
Source File: SpringExtension.java    From spring-analysis-note with MIT License 5 votes vote down vote up
/**
 * Delegates to {@link TestContextManager#beforeTestMethod}.
 */
@Override
public void beforeEach(ExtensionContext context) throws Exception {
	Object testInstance = context.getRequiredTestInstance();
	Method testMethod = context.getRequiredTestMethod();
	getTestContextManager(context).beforeTestMethod(testInstance, testMethod);
}
 
Example 6
Source File: SpringExtension.java    From spring-analysis-note with MIT License 5 votes vote down vote up
/**
 * Delegates to {@link TestContextManager#beforeTestExecution}.
 */
@Override
public void beforeTestExecution(ExtensionContext context) throws Exception {
	Object testInstance = context.getRequiredTestInstance();
	Method testMethod = context.getRequiredTestMethod();
	getTestContextManager(context).beforeTestExecution(testInstance, testMethod);
}
 
Example 7
Source File: SpringExtension.java    From spring-analysis-note with MIT License 5 votes vote down vote up
/**
 * Delegates to {@link TestContextManager#afterTestExecution}.
 */
@Override
public void afterTestExecution(ExtensionContext context) throws Exception {
	Object testInstance = context.getRequiredTestInstance();
	Method testMethod = context.getRequiredTestMethod();
	Throwable testException = context.getExecutionException().orElse(null);
	getTestContextManager(context).afterTestExecution(testInstance, testMethod, testException);
}
 
Example 8
Source File: SpringExtension.java    From java-technology-stack with MIT License 5 votes vote down vote up
/**
 * Delegates to {@link TestContextManager#beforeTestMethod}.
 */
@Override
public void beforeEach(ExtensionContext context) throws Exception {
	Object testInstance = context.getRequiredTestInstance();
	Method testMethod = context.getRequiredTestMethod();
	getTestContextManager(context).beforeTestMethod(testInstance, testMethod);
}
 
Example 9
Source File: SpringExtension.java    From java-technology-stack with MIT License 5 votes vote down vote up
/**
 * Delegates to {@link TestContextManager#afterTestMethod}.
 */
@Override
public void afterEach(ExtensionContext context) throws Exception {
	Object testInstance = context.getRequiredTestInstance();
	Method testMethod = context.getRequiredTestMethod();
	Throwable testException = context.getExecutionException().orElse(null);
	getTestContextManager(context).afterTestMethod(testInstance, testMethod, testException);
}
 
Example 10
Source File: RegisteredEventListenerExtension.java    From webtester2-core with Apache License 2.0 5 votes vote down vote up
@Override
@SuppressWarnings("PMD.SignatureDeclareThrowsException")
public void beforeEach(ExtensionContext context) {
    TestClassModel model = getModel(context);
    Object testInstance = context.getRequiredTestInstance();
    Multimap<EventListener, Browser> eventListeners = initializeEventListener(model, testInstance);
    context.getStore(BaseExtension.NAMESPACE).put("registered-eventlisteners", eventListeners);
}
 
Example 11
Source File: EntryPointExtension.java    From webtester2-core with Apache License 2.0 5 votes vote down vote up
@Override
public void beforeEach(ExtensionContext context) {
    Object testInstance = context.getRequiredTestInstance();
    getModel(context).getBrowserFields()
        .stream()
        .filter(browserField -> browserField.isAnnotationPresent(EntryPoint.class))
        .forEach(browserField -> openEntryPoint(browserField, testInstance));
}
 
Example 12
Source File: InjectionExtension.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public void beforeEach(ExtensionContext context) throws Exception {
	IInjectorProvider injectorProvider = getOrCreateInjectorProvider(context);
	if (injectorProvider instanceof IRegistryConfigurator) {
		final IRegistryConfigurator registryConfigurator = (IRegistryConfigurator) injectorProvider;
		registryConfigurator.setupRegistry();
	}
	if (injectorProvider != null) {
		Injector injector = injectorProvider.getInjector();
		if (injector != null) {
			Object testInstance = context.getRequiredTestInstance();
			injector.injectMembers(testInstance);
			try {
				TestInstances requiredTestInstances = context.getRequiredTestInstances();
				for (Object o : requiredTestInstances.getEnclosingInstances()) {
					injector.injectMembers(o);
				}
			} catch (NoSuchMethodError e) {
				if (!Modifier.isStatic(testInstance.getClass().getModifiers())) {
					if (testInstance.getClass().getDeclaringClass() != null) {
						throw new ExtensionConfigurationException("Injection of nested classes needs Junit5 >= 5.4", e);
					}
				}
				// OK, getRequiredTestInstances is not there in Junit5 < 5.4
			}
		}
	}
}
 
Example 13
Source File: SpringExtension.java    From spring-test-junit5 with Apache License 2.0 5 votes vote down vote up
/**
 * Delegates to {@link TestContextManager#beforeTestMethod}.
 */
@Override
public void beforeEach(ExtensionContext context) throws Exception {
	Object testInstance = context.getRequiredTestInstance();
	Method testMethod = context.getRequiredTestMethod();
	getTestContextManager(context).beforeTestMethod(testInstance, testMethod);
}
 
Example 14
Source File: SpringExtension.java    From spring-test-junit5 with Apache License 2.0 5 votes vote down vote up
/**
 * Delegates to {@link TestContextManager#afterTestMethod}.
 */
@Override
public void afterEach(ExtensionContext context) throws Exception {
	Object testInstance = context.getRequiredTestInstance();
	Method testMethod = context.getRequiredTestMethod();
	Throwable testException = context.getExecutionException().orElse(null);
	getTestContextManager(context).afterTestMethod(testInstance, testMethod, testException);
}
 
Example 15
Source File: JunitServerExtension.java    From junit-servers with MIT License 5 votes vote down vote up
@Override
public void afterEach(ExtensionContext context) {
	try {
		Object target = context.getRequiredTestInstance();
		AnnotationsHandlerRunner annotationsAdapter = findAnnotationsHandlerAdapterInStore(context);
		annotationsAdapter.afterEach(target);
	}
	finally {
		unregisterEmbeddedServer(context, false);
		removeAnnotationsHandlerAdapterFromStore(context);
	}
}