1

我正在尝试使用AutobeansGWT中将POJO序列化为JSON 我不断收到一个和,两者都在寻找。这是在标记Place的上下文中。NoClassDefFoundErrorClassNotFoundExceptionorg.json.JSONObject

我创建了一个 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,但我似乎仍然缺少一些东西。

4

2 回答 2

2

你是如何启动测试运行器的?这些类源自gwt/tools/redist/json/r2_20080312/json-1.5.jarGWT 存储库,应打包到gwt-servlet-deps.jar. 尝试将该 jar 添加到测试运行配置的类路径中,看看是否可以清除它。

于 2011-06-16T21:42:38.080 回答
0

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

如果你想在你的测试中使用 AutoBeans,其实很简单。拥有代理和工厂接口后,您可以使用

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

这是一个快速教程,您可以从中复制有关在 JUnit 测试中使用 GWT Autobeans 的代码。

于 2012-07-17T14:36:27.150 回答