【问题标题】:Spring Data MongoDB: Unit tests with repositoriesSpring Data MongoDB:使用存储库进行单元测试
【发布时间】:2013-04-22 17:37:19
【问题描述】:

应该如何使用 Spring Data 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 是为某个 Location(域对象)类实现所有自定义方法的类。我的测试类看起来像:

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

    @Autowired
    private LocationRepository locationRepository;

    /* Some tests... */
}

我试图在我正在运行的测试中嵌入一个 MongoDB 实例(如 here 解释的那样)但它不起作用:与测试数据库的连接已建立,但 mongo 模板似乎无法被覆盖,因为所有保存方法继续将数据插入“生产”数据库。

我正在使用 Spring 3.2.0 和 Spring Data Mongo 1.1.0.RELEASE。我正在使用 Junit 进行测试。

有什么建议吗?

提前谢谢你。

【问题讨论】:

    标签: junit4 spring-data spring-data-mongodb


    【解决方案1】:

    贾兰达,

    上周我遇到了同样的问题,巧合的是,我听说了Fongo,“mongo 的内存中 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());
            }
        }
    }
    

    【讨论】:

    • 嗨,米格尔,感谢您的提醒!最后,我实际上为生产/测试环境使用了不同的上下文,我注意到这似乎是要走的路。无论如何感谢您的回复,我一定会看看 Fongo!哈斯塔奥特拉 ;-)
    • 我想提一下我刚刚发现的这个库,它看起来简直太棒了:github.com/lordofthejars/nosql-unit
    猜你喜欢
    • 2014-02-26
    • 2016-04-09
    • 2019-01-23
    • 1970-01-01
    • 2014-08-13
    • 1970-01-01
    • 2014-06-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多