我目前正在使用 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();
}
}
任何幫助表示贊賞。
uj5u.com熱心網友回復:
假設GUITestExtension問題中提到的已在您的代碼庫中定義,則可以通過以下方式注冊:
@Test
@ExtendWith(GUITestExtension.class)
void test() {
...
}
更多可以在 JUnit 5 檔案的擴展模型部分找到。
理想情況下,擴展應該直接來自 AssertJ Swing,但不幸的是,專案負責人已經沒有時間了,因此我們正在尋找提交者。
如果有人有興趣,可隨時取得聯系在GitHub上。
uj5u.com熱心網友回復:
我根據 Stefano 的回答更改了我的測驗課程,現在一切正常。如果有人需要它,我的測驗類代碼是這樣的:
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(在我的問題中定義)匯入您的測驗類。測驗也是成功的,因為在我看來我有一個label帶有文本“標簽”的東西。
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/406411.html
標籤:
上一篇:發布方法后PHP頁面消失
