简体   繁体   English

AssertJ-Swing 和 Junit 5 支持

[英]AssertJ-Swing and Junit 5 support

I'm currently working at a Java application that I'm testing using JUnit Jupiter.我目前正在使用 JUnit Jupiter 测试 Java 应用程序。 I am starting to work at the GUI and I was wondering if it was possible to use AssertJ-Swing with JUnit 5. As a matter of fact I have found an open issue on AssertJ-Swing GitHub page.我开始在 GUI 上工作,我想知道是否可以将 AssertJ-Swing 与 JUnit 5 一起使用。事实上,我在 AssertJ-Swing GitHub 页面上发现了一个未解决的问题。

The suggestion they give is to define a GUITestExtension like so:他们给出的建议是像这样定义一个 GUITestExtension:

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));
    }
}

I am a bit familiar with the GUITestRunner that can be used with JUnit 4, but I have no idea on how to replace it with this.我对可以与 JUnit 4 一起使用的 GUITestRunner 有点熟悉,但我不知道如何用它来替换它。

Note: Usually GUITestRunner is used like so with JUnit 4:注意:通常 GUITestRunner 与 JUnit 4 一起使用:

import org.assertj.swing.junit.runner.GUITestRunner; 
import org.assertj.swing.junit.testcase.AssertJSwingJUnitTestCase;
import org.junit.runner.RunWith;
import org.assertj.swing.edt.GuiActionRunner; 
import org.assertj.swing.fixture.FrameFixture;

@RunWith(GUITestRunner.class) 
public class WelcomeSwingViewTest extends AssertJSwingJUnitTestCase{
    private FrameFixture window;
    private WelcomeSwingView welcomeSwingView;
    @Override
    protected void onSetUp() { 
        GuiActionRunner.execute(() -> {
         welcomeSwingView = new WelcomeSwingView();
         return welcomeSwingView; 
        });
        window = new FrameFixture(robot(), welcomeSwingView);
        window.show(); 
    }
}

Any help is appreciated.任何帮助表示赞赏。

Assuming that the GUITestExtension mentioned in the issue is defined in your codebase, it could be registered in the following way:假设问题中提到的GUITestExtension是在您的代码库中定义的,它可以通过以下方式注册:

@Test
@ExtendWith(GUITestExtension.class)
void test() {
    ...
}

More can be found in the extension model section of the JUnit 5 documentation.更多信息可以在 JUnit 5 文档的扩展 model部分中找到。

Ideally, the extension should come directly from AssertJ Swing, but the project lead has no longer time for it, unfortunately, therefore we are looking for committers.理想情况下,扩展应该直接来自 AssertJ Swing,但不幸的是,项目负责人已经没有时间了,因此我们正在寻找提交者。

If anyone is interested, feel free to get in touch on GitHub.如果有人感兴趣,请随时与 GitHub联系

I changed my test class according to Stefano's answer , and now everything is working.我根据 Stefano 的回答更改了我的测试 class ,现在一切正常。 In case anyone needs it my test class code is something like this:如果有人需要它,我的测试 class 代码是这样的:

import org.assertj.swing.core.matcher.JLabelMatcher;
import org.assertj.swing.edt.GuiActionRunner;
import org.assertj.swing.fixture.FrameFixture;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;

@ExtendWith(GUITestExtension.class)
class WelcomeSwingViewTest  {

    private FrameFixture window;
    private WelcomeSwingView welcomeSwingView;

    @BeforeEach
    void setup() {

        GuiActionRunner.execute(() -> {
            welcomeSwingView = new WelcomeSwingView();
            return welcomeSwingView;
        });
        window = new FrameFixture(welcomeSwingView);
        window.show();
    }

    @Test
    void testControlsInitialStates() {
        window.label(JLabelMatcher.withText("label"));
    }
}

Of course you also have to import the GUITestExtension (defined as in my question) to your test class.当然,您还必须将GUITestExtension (在我的问题中定义)导入您的测试 class。 Also the test is successful because in my view I have a label with text "label".测试也是成功的,因为在我看来,我有一个带有文本“标签”的label

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM