使用Jukito进行测试时GWT Timer UnsatisfiedLinkError

问题描述 投票:2回答:1

我正在使用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$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仿真中),但这不能让我测试计划定时器后发生的事情。任何人都可以复制它或知道如何解决它?

java gwt junit gwtp jukito
1个回答
5
投票

避免使用Timer并使用com.google.gwt.core.client.Scheduler代替。然后,您可以在prod代码中绑定实际的调度程序,在测试中绑定StubScheduler

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

© www.soinside.com 2019 - 2024. All rights reserved.