2

我正在使用 Jukito 测试 GWTP 演示器,并且在其中一个具有计时器作为重复任务的字段的情况下,我抛出了这个异常。

我正在运行 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$1.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$3.evaluate(ParentRunner.java:305)
at org.junit.runners.BlockJUnit4ClassRunner$1.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$4.run(ParentRunner.java:330)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:78)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:328)
at org.junit.runners.ParentRunner.access$100(ParentRunner.java:65)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:292)
at org.junit.runners.ParentRunner$3.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 仿真中),但这不会让我正确测试计时器安排后会发生什么。任何人都可以复制它或知道如何修复它?

4

1 回答 1

5

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

您可以Timer使用gwtmockito进行工作,但我已经在使用注入,您确实应该隐藏任何本机或GWT.create调用抽象,并使用替代实现进行测试。

于 2019-03-07T17:25:52.140 回答