org.junit.jupiter.api.extension.ExtensionContext#getRequiredTestInstance()源码实例Demo

下面列出了org.junit.jupiter.api.extension.ExtensionContext#getRequiredTestInstance() 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

@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();
    }
  }
}
 
源代码2 项目: spring-analysis-note   文件: SpringExtension.java
/**
 * 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);
}
 
源代码3 项目: spring-analysis-note   文件: SpringExtension.java
/**
 * 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);
}
 
源代码4 项目: spring-analysis-note   文件: SpringExtension.java
/**
 * 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);
}
 
源代码5 项目: spring-analysis-note   文件: SpringExtension.java
/**
 * 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);
}
 
源代码6 项目: java-technology-stack   文件: SpringExtension.java
/**
 * 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);
}
 
源代码7 项目: java-technology-stack   文件: SpringExtension.java
/**
 * 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);
}
 
源代码8 项目: java-technology-stack   文件: SpringExtension.java
/**
 * 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);
}
 
源代码9 项目: java-technology-stack   文件: SpringExtension.java
/**
 * 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);
}
 
@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);
}
 
源代码11 项目: webtester2-core   文件: EntryPointExtension.java
@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));
}
 
源代码12 项目: xtext-core   文件: InjectionExtension.java
@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
			}
		}
	}
}
 
源代码13 项目: spring-test-junit5   文件: SpringExtension.java
/**
 * 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);
}
 
源代码14 项目: spring-test-junit5   文件: SpringExtension.java
/**
 * 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);
}
 
源代码15 项目: junit-servers   文件: JunitServerExtension.java
@Override
public void afterEach(ExtensionContext context) {
	try {
		Object target = context.getRequiredTestInstance();
		AnnotationsHandlerRunner annotationsAdapter = findAnnotationsHandlerAdapterInStore(context);
		annotationsAdapter.afterEach(target);
	}
	finally {
		unregisterEmbeddedServer(context, false);
		removeAnnotationsHandlerAdapterFromStore(context);
	}
}