繁体   English   中英

AssertJ-Swing 和 Junit 5 支持

[英]AssertJ-Swing and Junit 5 support

我目前正在使用 JUnit Jupiter 测试 Java 应用程序。 我开始在 GUI 上工作,我想知道是否可以将 AssertJ-Swing 与 JUnit 5 一起使用。事实上,我在 AssertJ-Swing GitHub 页面上发现了一个未解决的问题。

他们给出的建议是像这样定义一个 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));
    }
}

我对可以与 JUnit 4 一起使用的 GUITestRunner 有点熟悉,但我不知道如何用它来替换它。

注意:通常 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(); 
    }
}

任何帮助表示赞赏。

假设问题中提到的GUITestExtension是在您的代码库中定义的,它可以通过以下方式注册:

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

更多信息可以在 JUnit 5 文档的扩展 model部分中找到。

理想情况下,扩展应该直接来自 AssertJ Swing,但不幸的是,项目负责人已经没有时间了,因此我们正在寻找提交者。

如果有人感兴趣,请随时与 GitHub联系

我根据 Stefano 的回答更改了我的测试 class ,现在一切正常。 如果有人需要它,我的测试 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"));
    }
}

当然,您还必须将GUITestExtension (在我的问题中定义)导入您的测试 class。 测试也是成功的,因为在我看来,我有一个带有文本“标签”的label

暂无
暂无

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

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