2015-09-20 60 views
0

我正在使用Mongodb的Spring数据处理Spring MVC项目。 我刚刚创建了实体并进行了配置。然后,一旦用JUnit进行测试,我得到了一个我无法修复的错误。任何人都可以帮助我请这个。 这里是我的Spring配置文件:Spring数据Mongodb JUnit错误

<beans xmlns="http://www.springframework.org/schema/beans" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns:context="http://www.springframework.org/schema/context" 
xmlns:mongo="http://www.springframework.org/schema/data/mongo" 
xsi:schemaLocation="http://www.springframework.org/schema/context 
     http://www.springframework.org/schema/context/spring-context-3.0.xsd 
     http://www.springframework.org/schema/data/mongo 
     http://www.springframework.org/schema/data/mongo/spring-mongo-1.0.xsd 
     http://www.springframework.org/schema/beans 
     http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> 

<mongo:mongo host="127.0.0.1" port="27017" /> 
<mongo:db-factory dbname="AgenceAviationDB" /> 

<bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate"> 
    <constructor-arg name="mongoDbFactory" ref="mongoDbFactory" /> 
</bean> 

这些都是我的Maven的依赖关系:

spring-core 4.2.1.RELEASE 
    mongo-java-driver 2.12.1 
    spring-data-mongodb 1.4.2.RELEASE 
    spring-context 4.2.1.RELEASE 
    junit 4.7 

这里是我的实体之一:

package com.pfa.agenceDaviation.entities; 
import java.io.Serializable; 
import org.springframework.data.annotation.Id; 
import org.springframework.data.mongodb.core.mapping.Document; 
@Document 
public class Passager implements Serializable{ 
@Id 
private String id; 
private String nomP; 
private String adresseP; 
private String nTelephoneP; 
//setters getters and constructors 

这是我的测试类:

package com.pfa.agenceDaviation; 
import static org.junit.Assert.*; 
import org.junit.Before; 
import org.junit.Test; 
import org.springframework.context.ApplicationContext; 
import org.springframework.context.support.ClassPathXmlApplicationContext; 
import org.springframework.context.support.GenericXmlApplicationContext; 
import org.springframework.data.mongodb.core.MongoOperations; 
public class testDAO { 
@Before 
public void setUp() throws Exception { 
} 

@Test 
public void test() { 
    try { 
     ApplicationContext ctx = new GenericXmlApplicationContext("ApplicationContext.xml"); 
      MongoOperations mongoOperation = (MongoOperations)ctx.getBean("mongoTemplate"); 
      assertTrue(true); 

    } 
    catch (Exception e) { 
    assertTrue(e.getMessage(),false); 
    } 
    } 
    } 

这是我得到的错误:

java.lang.NoSuchMethodError: org.springframework.expression.spel.SpelParserConfiguration.<init>(Lorg/springframework/expression/spel/SpelCompilerMode;Ljava/lang/ClassLoader;)V 
at org.springframework.context.expression.StandardBeanExpressionResolver.<init>(StandardBeanExpressionResolver.java:98) 
at org.springframework.context.support.AbstractApplicationContext.prepareBeanFactory(AbstractApplicationContext.java:553) 
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:455) 
at org.springframework.context.support.GenericXmlApplicationContext.<init>(GenericXmlApplicationContext.java:70) 
at com.pfa.agenceDaviation.testDAO.test(testDAO.java:21) 
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: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.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28) 
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:50) 
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

莫非你是否正确地编写代码?有错误! –

+0

什么是我的代码不好? –

回答

0
  • 跑MVN依赖性:树
  • 注意你的弹簧数据MongoDB的依赖性

你会发现,弹簧数据MongoDB是使用Spring版本 - 4.2.1.RELEASE 这里是证明 - http://mvnrepository.com/artifact/org.springframework.data/spring-data-mongodb/1.4.2.RELEASE

所以,可能的解决方案 - 更新你的春季依赖判断4.2.1

<dependency> 
    <groupId>org.springframework</groupId> 
    <artifactId>spring-core</artifactId> 
    <version>4.2.1.RELEASE</version> 
</dependency> 
<dependency> 
    <groupId>org.springframework</groupId> 
    <artifactId>spring-context</artifactId> 
    <version>4.2.1.RELEASE</version> 
</dependency> 

下一步,你需要检查的是你真的需要存储弹簧核心春天上下文。如果你真的需要它们 - 只要排除弹簧数据MongoDB的那些依赖

+0

我刚刚做过,仍然收到相同的错误。 –

+0

让我们想想。有一瞬间 –

+0

确定非常感谢! –