2013-06-26 40 views
2

我想在我的一些Spring應用程序測試,但沒有成功使用this庫。看來春季數據MongoDB是supported,但我沒有設法讓它工作。 examples沒有完全遵循@Repository方法。NoSQLUnit MongoDB的測試:春季數據MongoDB的方法

比方說,我有一個DummyClass POJO:

@Document(collection = DummyClass.ITEMS_COLLECTION_NAME) 
public class DummyClass { 

    public static final String ITEMS_COLLECTION_NAME = "dummy"; 

    //Maps _id mongodb internal field 
    private BigInteger id; 

    private String a; 
    private String b; 

    public DummyClass() { 
     super(); 
    } 

    public DummyClass(String a, String b) { 
     this.a = a; 
     this.b = b; 
    } 

    public String getA() { 
     return a; 
    } 

    public void setA(String a) { 
     this.a = a; 
    } 

    public String getB() { 
     return b; 
    } 

    public void setB(String b) { 
     this.b = b; 
    } 

} 

和我有各自的倉庫接口和自定義接口/實現:

@Repository 
public interface DummyClassRepository extends MongoRepository<DummyClass, Long>, DummyClassRepositoryCustom { 
} 

public interface DummyClassRepositoryCustom { 
    /* My dummy methods declaration */ 
} 

public class DummyClassRepositoryImpl implements DummyClassRepositoryCustom{ 
    /* My dummy methods implementation */ 
} 

這是我的測試情況下的樣子(簡化):

<?xml version="1.0" encoding="UTF-8"?> 
<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"> 

    <context:component-scan base-package="org.company.project.data.*"/> 

    <!-- Fongo config (used by NoSQL-Unit) --> 
    <bean name="fongo" class="com.foursquare.fongo.Fongo"> 
     <constructor-arg value="InMemoryMongo" /> 
    </bean> 
    <bean id="mongo" factory-bean="fongo" factory-method="getMongo" /> 

    <mongo:db-factory id="mongoDbFactory" mongo-ref="mongo" /> 

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

    <mongo:repositories base-package="org.company.project.data.repositories.mongodb"/> 

    <!-- Some other beans declaration --> 

</beans> 

顯然我的MongoDB版本庫在org.company.project.data.repositories.mongodb

問題是我無法將某個.json加載到內存中的MongoDB實例。我有什麼至今:

@RunWith(SpringJUnit4ClassRunner.class) 
@ContextConfiguration(locations={"/test-context.xml"}) 
public class MyTest { 

    private static Logger logger = Logger.getLogger(MyTest.class); 

    @Autowired 
    private ApplicationContext applicationContext; 

    @Rule 
    public MongoDbRule mongoDbRule = newMongoDbRule().defaultSpringMongoDb("test"); 

    @Autowired 
    private DummyClassRepository dummyClassRepository; 

    @Test 
    @UsingDataSet(locations = {"/dummy.json"}, loadStrategy = LoadStrategyEnum.CLEAN_INSERT) 
    public void myTest(){ 
     logger.info(dummyClassRepository.findAll().size()); 
     assert(false); 
    } 

} 

的以.json文件的內容遵循在文檔中指定的格式,但不管是什麼我指定,在開始測試方法時集合總是空的。例如,這個以.json文件的一個例子是:

{ 
    "dummy": [ 
     { 
      "_class": "org.company.project.data.model.DummyClass", 
      "_id": "51557362e4b083f9e619f99c", 
      "a": "a", 
      "b": "b" 
     }, 
     { 
      "_class": "org.company.project.data.model.DummyClass", 
      "_id": "51557362e4b083f9e619f99d", 
      "a": "c", 
      "b": "d" 
     } 
    ] 
} 

令我震驚的是,有一次,同樣的測試方法中,我能到我的虛擬實例添加到數據庫沒有問題,這主要是告訴我的存儲庫實際上工作正常。

沒有人用這個JUnit擴展爲什麼特定數據沒有被加載到數據庫中,可以提供我一些想法的工作?

預先感謝您!

+1

jaranda,以後你對nosqlunit和這個職位,我測試過nosqlunit意見,併爲我工作得很好。可能發生的事情是,MongoDbRule和DummyClassRepository連接到不同的數據庫。 MongoDbRule使用「test」數據庫,並且不清楚DummyClassRepository使用的是哪個數據庫。在我的MongoTemplate配置中,我明確地設置了應該使用的數據庫。正如我在另一個問題中所說的,我不使用xml來配置Spring,至少在我的測試中。 –

+1

在彈簧數據文檔的所有示例中,他們在db-factory上使用dbname參數:。嘗試指定「測試」作爲dbname –

+0

@MiguelCartagena我怎麼錯過了!它現在有用,謝謝!如果您將答覆添加爲答案,我會高興地接受:)格拉西亞斯! – jarandaf

回答

2

Jaranda,

看來你的春天庫和nosqlunit在不同的數據庫工作。嘗試修改你的spring配置以使用相同的nosqlunit數據庫。

<mongo:db-factory id="mongoDbFactory" mongo-ref="mongo" dbname="test" />