GWT Autobeans / NoClassDefFoundError:我做错了什么?

时间:2011-06-16 21:32:53

标签: gwt noclassdeffounderror classnotfoundexception autobean

我正在尝试serialize POJO JSON GWT Autobeans NoClassDefFoundError使用ClassNotFoundException,我一直在接收org.json.JSONObject和{{} 3}},都在寻找Place。这是在标记{{3}}。

的上下文中

我创建了一个JUnit测试来隔离问题。测试是:

@Test public void testGetToken() {
    SearchCriterion searchCriterion = new JavaSearchCriterion("a", "b", ImmutableList.of("c"));
    ResourcesPlace place = new ResourcesPlace(ImmutableList.of(searchCriterion));
    String token = new Tokenizer().getToken(place);
    assertNotNull(token);
}

我的堆栈跟踪是

java.lang.NoClassDefFoundError: org/json/JSONObject
    at com.google.web.bindery.autobean.shared.impl.StringQuoter.quote(StringQuoter.java:69)
    at com.google.web.bindery.autobean.shared.impl.AutoBeanCodexImpl$PropertyGetter.encodeProperty(AutoBeanCodexImpl.java:411)
    at com.google.web.bindery.autobean.shared.impl.AutoBeanCodexImpl$PropertyGetter.visitValueProperty(AutoBeanCodexImpl.java:397)
    at com.google.web.bindery.autobean.vm.impl.ProxyAutoBean.traverseProperties(ProxyAutoBean.java:260)
    at com.google.web.bindery.autobean.shared.impl.AbstractAutoBean.traverse(AbstractAutoBean.java:166)
    at com.google.web.bindery.autobean.shared.impl.AbstractAutoBean.accept(AbstractAutoBean.java:101)
    at com.google.web.bindery.autobean.shared.impl.AutoBeanCodexImpl.doEncode(AutoBeanCodexImpl.java:558)
    at com.google.web.bindery.autobean.shared.AutoBeanCodex.encode(AutoBeanCodex.java:83)
    at com.redacted.client.place.ResourcesPlace$Tokenizer.getToken(ResourcesPlace.java:62)
    at com.redacted.client.place.ResourcesPlaceTokenizerTest.testGetToken(ResourcesPlaceTokenizerTest.java:19)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:76)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:49)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Caused by: java.lang.ClassNotFoundException: org.json.JSONObject
    at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
    ... 32 more

以下是Tokenizer的相关位:

    private MyFactory factory = AutoBeanFactorySource.create(MyFactory.class);

    interface MyFactory extends AutoBeanFactory {
          // Factory method for a simple AutoBean
          AutoBean<SearchCriterion> person();

          // Factory method for a non-simple type or to wrap an existing instance
          AutoBean<SearchCriterion> person(SearchCriterion toWrap);
        }

    @Override
    public String getToken(ResourcesPlace place) {

        AutoBean<SearchCriterion> placeBean = factory.create(SearchCriterion.class, place.getSearchCriteria().get(0));
        return AutoBeanCodex.encode(placeBean).getPayload();
            }

我在我的.gwt.xml中添加了Autobeans include,但我似乎仍然遗漏了一些东西。

2 个答案:

答案 0 :(得分:2)

你是如何推出测试跑步者的?这些类来自GWT仓库中的gwt/tools/redist/json/r2_20080312/json-1.5.jar,应该打包到gwt-servlet-deps.jar。尝试将该jar添加到测试运行配置的类路径中,以查看是否将其清除。

答案 1 :(得分:0)

如果我是你,我实际上会尝试尽可能地在GWT测试用例之外编写我的测试。它需要花费大量的时间才能启动,最好它应该用于集成测试而不是单元测试(当然我们可以理解差异,但我离题了)。

如果要在测试中使用AutoBeans,实际上非常简单。拥有代理和工厂接口后,您可以使用

BeanFactory factory = AutoBeanFactorySource.create(BeanFactory.class);
model = factory.createMyBean().as();

以下是一个快速教程,您可以从约using GWT Autobeans in JUnit tests复制代码。