我写了一个测试方法。在这个项目中,我使用Morphia for MongoDB。但是,当我启动方法时,我得到以下错误:
java.lang.NoSuchMethodError: org.apache.log4j.Logger.log(Ljava/lang/String;Lorg/apache/log4j/Level;Ljava/lang/Object;Ljava/lang/Throwable;)V
at org.slf4j.impl.Log4jLoggerAdapter.info(Log4jLoggerAdapter.java:166)
at com.mongodb.diagnostics.logging.SLF4JLogger.info(SLF4JLogger.java:71)
at com.mongodb.connection.SingleServerCluster.<init>(SingleServerCluster.java:45)
at com.mongodb.connection.DefaultClusterFactory.create(DefaultClusterFactory.java:85)
at com.mongodb.Mongo.createCluster(Mongo.java:670)
at com.mongodb.Mongo.createCluster(Mongo.java:656)
at com.mongodb.Mongo.<init>(Mongo.java:278)
at com.mongodb.Mongo.<init>(Mongo.java:274)
at com.mongodb.MongoClient.<init>(MongoClient.java:174)
at com.mongodb.MongoClient.<init>(MongoClient.java:151)
at com.mongodb.MongoClient.<init>(MongoClient.java:141)
at de.meinTellerchen.utils.mongoDB.connection.MongoDBCon.<init>(MongoDBCon.java:65)
at de.meinTellerchen.ingredient.service.IngredientRestService.dataBaseConnection(IngredientRestService.java:34)
at de.meinTellerchen.ingredient.service.IngredientRestService.<init>(IngredientRestService.java:22)
at de.meinTellerchen.ingredient.service.IngredientRestServiceTest.test001_WriteIngredient(IngredientRestServiceTest.java:21)
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:497)
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.runNotIgnored(BlockJUnit4ClassRunner.java:79)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:71)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:49)
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:86)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
此测试中的方法创建一个Object并将其保存在带有Morphia的MongoDB中。
@Test
public void test001_WriteIngredient() {
Ingredient ingredient = generateIngredient();
IngredientRestService ingredientRestService = new IngredientRestService();
assertNotNull(ingredient);
Response response = ingredientRestService.writeIngredient(ingredient);
assertNotNull(response);
}
我不知道为什么它不起作用。我不使用Logger。
答案 0 :(得分:0)
问题在于log4j和slf4j的版本
在类路径中添加以下提到的jar:
log4j的
SLF4J-简单
JCL-过SLF4J
SLF4J的API
具有适当版本的slf4j-log4j12
或使用maven来处理你的依赖。
罐子的兼容版本示例:
1)log4j-1.2.15.jar
2)slf4j-api-1.7.7.jar
3)commons-logging-1.0.4.jar
4)logback-classic-1.0.0.jar
5)logback-core-1.0.0.jar
答案 1 :(得分:0)
类路径中缺少slf4j的依赖jar。
请打开系统中可用的mongodriver jar并查看meta-inf / MANIFEST.MF文件,并添加Import-Package:部分中提到的所需jar文件。
示例:强> Mongodb java驱动程序3.2.1 需要以下依赖的jar。
Import-Package: javax.xml.bind,javax.crypto,javax.crypto.spec,javax.ma
nagement,javax.net,javax.net.ssl,javax.security.sasl,javax.security.a
uth.callback,org.ietf.jgss,io.netty.bootstrap;resolution:=optional;ve
rsion="[4.0,5)",io.netty.buffer;resolution:=optional;version="[4.0,5)
",io.netty.channel;resolution:=optional;version="[4.0,5)",io.netty.ch
annel.nio;resolution:=optional;version="[4.0,5)",io.netty.channel.soc
ket;resolution:=optional;version="[4.0,5)",io.netty.channel.socket.ni
o;resolution:=optional;version="[4.0,5)",io.netty.handler.ssl;resolut
ion:=optional;version="[4.0,5)",io.netty.handler.timeout;resolution:=
optional;version="[4.0,5)",io.netty.util.concurrent;resolution:=optio
nal;version="[4.0,5)",org.slf4j;resolution:=optional;version="[1.7,2)
"