2015-09-20 84 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

確定非常感謝! –