2013-04-22 60 views
3

它應該如何在Spring Data MongoDB中使用存儲庫方法構建一些測試?我想爲我的測試設置測試數據庫,因爲我不想爲此使用生產數據庫。這應該是可能的,但我不知道。這是我的應用程序上下文:春季數據MongoDB:存儲庫的單元測試

<?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" 
     xmlns:neo4j="http://www.springframework.org/schema/data/neo4j" 
     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 
       http://www.springframework.org/schema/data/neo4j 
       http://www.springframework.org/schema/data/neo4j/spring-neo4j.xsd"> 

     <!-- Default bean name is 'mongo' --> 
     <mongo:mongo host="${mongo.host}" port="${mongo.port}"> 
     <mongo:options connections-per-host="8" 
      threads-allowed-to-block-for-connection-multiplier="4" 
      connect-timeout="${mongo.connect-timeout}" 
      max-wait-time="${mongo.max-wait-time}" 
      auto-connect-retry="true" 
      socket-keep-alive="true" 
      socket-timeout="${mongo.socket-timeout}" 
      slave-ok="true" 
      write-number="1" 
      write-timeout="0" 
      write-fsync="true"/> 
     </mongo:mongo> 

     <bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate"> 
      <constructor-arg ref="mongo" /> 
      <constructor-arg name="databaseName" value="${mongo.db}" /> 
     </bean> 

     <context:component-scan base-package="domain.company.group.project.data.repositories"/> 

     <!-- MongoDB repositories --> 
     <mongo:repositories base-package="domain.company.group.project.data.repositories.mongodb"/> 

     <!-- some other stuff --> 

    </beans> 

而且,我們說,我有一個簡單的存儲庫,如下所示:

public interface LocationRepository extends MongoRepository<Location, String>, LocationRepositoryCustom { 

} 

其中LocationRepositoryImpl是實現一定位置我所有的自定義方法的類(域對象)類。我的測試類的樣子:

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

    @Autowired 
    private LocationRepository locationRepository; 

    /* Some tests... */ 
} 

我已經試過我的運行測試中嵌入一個MongoDB實例(如解釋here),但它不工作:到測試數據庫建立連接,但蒙戈模板似乎不能夠被覆蓋,因爲所有保存方法都會將數據插入「生產」數據庫。

我使用Spring 3.2.0和Spring Data Mongo 1.1.0.RELEASE。我正在使用Junit進行測試。

有什麼建議嗎?

預先感謝您。

回答

6

Jaranda,

我上週所面臨的同樣的問題,巧合的是我聽說過Fongo,「在內存中的Java實現蒙戈的。」

因此,我決定使用它來測試我的自定義存儲庫,並對我完美工作。以下是如何配置Spring以在JUnit測試中使用Fongo的示例。請注意,我沒有使用xml配置。

希望這會很有用!

@RunWith(SpringJUnit4ClassRunner.class) 
@ContextConfiguration 
public class LocationRepositoryTest { 

    private static final String PLAYER_ID = ObjectId.get().toString(); 

    @Autowired private LocationRepositoryCustom playerRepository; 
    @Autowired private MongoTemplate mongoTemplate; 

    /* Some tests... */ 

    @Configuration 
    static class LocationRepositoryTestConfiguration { 

     @Bean 
     public Mongo mongo() { 
      // Configure a Fongo instance 
      return new Fongo("mongo-test").getMongo(); 
     } 

     @Bean 
     public MongoTemplate mongoTemplate() { 
      return new MongoTemplate(mongo(), "collection-name"); 
     } 

     @Bean 
     public LocationRepositoryCustom playerRepository() { 
      // This is necessary if MongoTemplate is an argument of custom implementation constructor 
      return new LocationRepositoryCustomImpl(mongoTemplate()); 
     } 
    } 
} 
+0

嘿米格爾,感謝您的高擡頭!最後,我實際上在生產/測試環境中使用了不同的上下文,而我注意到這兩種環境似乎是要走的路。無論如何感謝您的回覆,我一定會看看Fongo! Hasta otra ;-) – jarandaf 2013-06-04 14:53:11

+0

我想提一下剛剛找到的這個庫,它看起來簡直太神奇了:https://github.com/lordofthejars/nosql-unit/ – jarandaf 2013-06-25 16:25:07

+0

看起來非常有趣@jaranda,感謝分享! – 2013-06-26 16:44:16