使用 Jukito 进行测试时 GWT 计时器 UnsatisfiedLinkError

GWT Timer UnsatisfiedLinkError when testing with Jukito

我正在使用 Jukito 测试 GWTP Presenter,其中一个将计时器作为重复任务的字段,我抛出了这个异常。

我是 运行 GWT 2.8.2、GWTP 1.6、JUnit 4 和 Jukito 1.5

测试看起来像这样:

@RunWith(JukitoRunner.class)
public class MyPresenterTest {

    @Inject
    MyPresenter presenter;

    @Test
    public void prepareFromRequestTest(EventBus eventBus,
                                       MyServiceAsync MyService) {

        PlaceRequest placeRequest = new PlaceRequest.Builder()
                .nameToken(NameTokens.getMyPlace())
                .with("aParam", "0110")
                .build();

        Data data = DataTestFactory.createData();

        AsyncStubber.callSuccessWith(data)
                .when(myService)
                .requestDataToServer(eq("0110"), any());

        // the timer scheduleRepeating is called in here:
        presenter.prepareFromRequest(placeRequest);
    }
}

即使像下面这样的基本测试也会失败:

@RunWith(JukitoRunner.class)
public class GwtTimerTest {

    @Test
    public void testTimer() {
        Timer timer = new Timer() {
            @Override
            public void run() {
                System.out.println("timer");
            }
        };

        timer.schedule(1000);
    }
}

这是完整的例外:

java.lang.UnsatisfiedLinkError: com.google.gwt.user.client.Timer.createCallback(Lcom/google/gwt/user/client/Timer;I)Lcom/google/gwt/core/client/JavaScriptObject;

at com.google.gwt.user.client.Timer.createCallback(Native Method)
at com.google.gwt.user.client.Timer.schedule(Timer.java:98)
at my.test (Redacted.java:42)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod.runReflectiveCall(FrameworkMethod.java:59)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:56)
at org.jukito.InjectedStatement.evaluate(InjectedStatement.java:108)
at org.junit.runners.ParentRunner.evaluate(ParentRunner.java:305)
at org.junit.runners.BlockJUnit4ClassRunner.evaluate(BlockJUnit4ClassRunner.java:100)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:365)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:103)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:63)
at org.junit.runners.ParentRunner.run(ParentRunner.java:330)
at org.junit.runners.ParentRunner.schedule(ParentRunner.java:78)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:328)
at org.junit.runners.ParentRunner.access0(ParentRunner.java:65)
at org.junit.runners.ParentRunner.evaluate(ParentRunner.java:292)
at org.junit.runners.ParentRunner.evaluate(ParentRunner.java:305)
at org.junit.runners.ParentRunner.run(ParentRunner.java:412)
at org.jukito.JukitoRunner.run(JukitoRunner.java:227)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)

我想我可以捕捉到一个错误(UnsatisfiedLinkError 不在 GWT 的 JRE 仿真中)但是这不会让我正确地测试定时器被安排后会发生什么。任何人都可以复制它或知道如何修复它?

避免使用 Timer,而是使用 com.google.gwt.core.client.Scheduler。然后你可以在产品代码中绑定一个实际的调度程序,在测试中绑定 StubScheduler

您可以使用 gwtmockito 使 Timer 工作,但我因为您已经在使用注入,所以您真的应该隐藏任何本机或 GWT.create 抽象背后的调用,并使用替代实现用于测试。