Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for JUnit 5 #259

Open
LorenzoBettini opened this issue Jan 18, 2021 · 3 comments
Open

Support for JUnit 5 #259

LorenzoBettini opened this issue Jan 18, 2021 · 3 comments

Comments

@LorenzoBettini
Copy link

Hi

is there any plan to provide something similar to the GUITestRunner and AssertJSwingJUnitTestCase for JUnit 5? I guess that the corresponding mechanism of GUITestRunner would be a Jupiter extension?

@wjbakker
Copy link

wjbakker commented Apr 5, 2021

Hi @LorenzoBettini ,

You can define a GUITestExtension as following:

import org.assertj.swing.junit.runner.FailureScreenshotTaker;
import org.assertj.swing.junit.runner.ImageFolderCreator;
import org.junit.jupiter.api.extension.Extension;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.junit.jupiter.api.extension.InvocationInterceptor;
import org.junit.jupiter.api.extension.ReflectiveInvocationContext;

import java.lang.reflect.Method;

import static org.assertj.swing.annotation.GUITestFinder.isGUITest;
import static org.assertj.swing.junit.runner.Formatter.testNameFrom;

/**
 * Understands a JUnit 5 extension that takes a screenshot of a failed GUI test.
 * The Junit 4 runner is available in {@link org.assertj.swing.junit.runner.GUITestRunner}.
 *
 * @see <a href="https://github.com/assertj/assertj-swing/issues/259">assertj-swing #259</a>
 * @author William Bakker
 */
public class GUITestExtension implements Extension, InvocationInterceptor {
    private final FailureScreenshotTaker screenshotTaker;

    public GUITestExtension() {
        screenshotTaker = new FailureScreenshotTaker(new ImageFolderCreator().createImageFolder());
    }

    @Override
    public void interceptTestMethod(
            Invocation<Void> invocation,
            ReflectiveInvocationContext<Method> invocationContext,
            ExtensionContext extensionContext) 
            throws Throwable {
        try {
            invocation.proceed();
        } catch (Throwable t) {
            takeScreenshot(invocationContext.getExecutable());
            throw t;
        }
    }

    private void takeScreenshot(Method method) {
        final Class<?> testClass = method.getDeclaringClass();
        if (!(isGUITest(testClass, method)))
            return;
        screenshotTaker.saveScreenshot(testNameFrom(testClass, method));
    }
}

@hakanai
Copy link

hakanai commented Jun 1, 2021

Ah, that is interesting. We had made our own JUnit 5 extension for AssertJ-Swing but we didn't even think of putting in a screenshot taker, all ours does is spin up the Robot and tear it down at the end.

@scordio
Copy link
Member

scordio commented Jan 8, 2022

Unfortunately, the project lead no longer has time for AssertJ Swing and we are looking for committers (see #264).

Also, @wjbakker @hakanai in case your work could be generally usable, feel free to raise a PR and we will do our best to get it merged.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants