我正在为我的GAE托管服务构建一个jUnit测试用例,其中一种方法存在问题。
这是资源界面:
public interface BasketItemResource {
@Delete
public void removeBasketItem(String id);
@Get
public Representation listBasketItems();
@Put
public void addBasketItems(Representation items);
}
以下是我的测试用例:
@Test
public void testListItems() {
Gson gson = new Gson();
List<BasketItem> items = null;
JsonRepresentation jRep = null;
Type collectionType = new TypeToken<List<BasketItem>>(){}.getType();
try {
jRep = new JsonRepresentation(resource.listBasketItems());
items = gson.fromJson(jRep.getJsonArray().toString(), collectionType);
} catch (JsonSyntaxException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
assertEquals(items.size(), 5);
}
@Test
public void testAddItem() {
Gson gson = new Gson();
BasketItem b = new BasketItem();
b.basketItemID = 123;
b.brandName = "Pepsi";
b.description = "Cola Beverage";
b.upc = "123123123";
List<BasketItem> items = new ArrayList<BasketItem>();
items.add(b);
JsonRepresentation jRep = null;
try {
jRep = new JsonRepresentation(new JSONArray(gson.toJson(items)));
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
resource.addBasketItems(jRep);
}
listItems测试执行正常,但是当我运行testAddItems()时,我首先收到此警告:
INFO: Unable to register the helper org.restlet.client.ext.json.JsonConverter
java.lang.ClassNotFoundException: org.restlet.client.ext.json.JsonConverter
at org.restlet.engine.util.EngineClassLoader.findClass(EngineClassLoader.java:101)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at org.restlet.engine.Engine.registerHelper(Engine.java:723)
at org.restlet.engine.Engine.registerHelpers(Engine.java:761)
at org.restlet.engine.Engine.registerHelpers(Engine.java:801)
at org.restlet.engine.Engine.discoverConverters(Engine.java:509)
at org.restlet.engine.Engine.<init>(Engine.java:337)
at org.restlet.engine.Engine.register(Engine.java:248)
at org.restlet.engine.Engine.register(Engine.java:237)
at org.restlet.engine.Engine.getInstance(Engine.java:149)
at org.restlet.data.Method.<init>(Method.java:331)
at org.restlet.data.Method.<init>(Method.java:390)
at org.restlet.data.Method.<init>(Method.java:355)
at org.restlet.data.Method.<clinit>(Method.java:54)
at org.restlet.engine.resource.AnnotationUtils.addAnnotationDescriptors(AnnotationUtils.java:82)
at org.restlet.engine.resource.AnnotationUtils.addAnnotations(AnnotationUtils.java:132)
at org.restlet.engine.resource.AnnotationUtils.getAnnotations(AnnotationUtils.java:231)
at org.restlet.resource.ClientResource.wrap(ClientResource.java:1548)
at com.tests.tests.BasketItemUploadTests.setUp(BasketItemUploadTests.java:43)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:45)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:27)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:68)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
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)
后面是(resource.addBasketItems(jRep);)行的堆栈跟踪:
Internal Server Error (500) - Internal Server Error
at org.restlet.resource.ClientResource$1.invoke(ClientResource.java:1655)
at $Proxy9.addBasketItems(Unknown Source)
at com.tests.tests.BasketItemUploadTests.testAddItem(BasketItemUploadTests.java:84)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:45)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:68)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
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)
答案 0 :(得分:1)
根据您发布的堆栈跟踪,无法找到类org.restlet.client.ext.json.JsonConverter
。在运行junit测试时,确保包含类org.restlet.client.ext.json.JsonConverter
的jar文件位于类路径中。将项目的/WEB-INF/lib
文件夹添加到类路径中可能会解决您的问题。