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

The following examples show how to use org.junit.jupiter.api.extension.ExtensionContext#getTestClass() . 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: QuarkusUnitTest.java    From quarkus with Apache License 2.0 6 votes vote down vote up
@Override
public void beforeEach(ExtensionContext context) throws Exception {
    if (assertException != null) {
        // Build failed as expected - test methods are not invoked
        return;
    }
    if (runningQuarkusApplication != null) {
        runningQuarkusApplication.getClassLoader().loadClass(RestAssuredURLManager.class.getName())
                .getDeclaredMethod("setURL", boolean.class).invoke(null, useSecureConnection);
    } else {
        Optional<Class<?>> testClass = context.getTestClass();
        if (testClass.isPresent()) {
            Field extensionField = Arrays.stream(testClass.get().getDeclaredFields()).filter(
                    f -> f.isAnnotationPresent(RegisterExtension.class) && QuarkusUnitTest.class.equals(f.getType()))
                    .findAny().orElse(null);
            if (extensionField != null && !Modifier.isStatic(extensionField.getModifiers())) {
                throw new IllegalStateException(
                        "Test application not started - QuarkusUnitTest must be used with a static field: "
                                + extensionField);
            }
        }
        throw new IllegalStateException("Test application not started for an unknown reason");
    }
}
 
Example 2
Source File: SurefireReports.java    From selenium-jupiter with Apache License 2.0 6 votes vote down vote up
public static String getOutputFolder(ExtensionContext context,
        String outputFolder) {
    if (context == null) {
        return "";
    }
    Optional<Method> testMethod = context.getTestMethod();
    Optional<Class<?>> testInstance = context.getTestClass();
    if (testMethod.isPresent() && testInstance.isPresent()) {
        if (outputFolder.equalsIgnoreCase("surefire-reports")) {
            outputFolder = getSurefireOutputFolder(testInstance.get());
        } else if (outputFolder.isEmpty()) {
            outputFolder = ".";
        }
    }

    log.trace("Output folder {}", outputFolder);
    File outputFolderFile = new File(outputFolder);
    if (!outputFolderFile.exists()) {
        outputFolderFile.mkdirs();
    }
    return outputFolder;
}