assertj / assertj-swing

Fluent assertions for Swing apps
Other
108 stars 52 forks source link

Add Junit-Jupiter support #265

Open wjbakker opened 2 years ago

wjbakker commented 2 years ago

Adds a Junit-Jupiter Extension for tests annotated with @GUITest.

Tests can now be run with the @GUITestExtension.

A new module is added:

<dependency>
  <groupId>org.assertj</groupId>
  <artifactId>assertj-swing-junit-jupiter</artifactId>
</dependency>

Example usage:

@ExtendWith(GUITestExtension.class)
class MyGuiTest {
  (...)
}

Solves !259

gschrader commented 2 years ago

I'm not sure if you're still using AssertJSwingTestCaseTemplate for your tests but I found that tests would hang if you had an exception in any @BeforeEach or @AfterEach. I worked around that (I think) using this extension:

@RegisterExtension
InvocationInterceptor invocationInterceptor = new InvocationInterceptor() {
    @Override
    public void interceptBeforeEachMethod(InvocationInterceptor.Invocation<Void> invocation, ReflectiveInvocationContext<Method> invocationContext, ExtensionContext extensionContext) throws Throwable {
        proceedWithRobotCleanup(invocation);
    }

  @Override
    public void interceptAfterEachMethod(Invocation<Void> invocation, ReflectiveInvocationContext<Method> invocationContext, ExtensionContext extensionContext) throws Throwable {
        proceedWithRobotCleanup(invocation);
    }

    private void proceedWithRobotCleanup(Invocation<Void> invocation) throws Throwable {
        try {
            invocation.proceed();
        } catch (Throwable e) {
            if (robot() != null) {
                cleanUp();
            }
            throw e;
        }
    }
};