【问题标题】:NoSQLUnit MongoDB test: Spring Data MongoDB approachNoSQLUnit MongoDB 测试:Spring Data MongoDB 方法
【发布时间】:2013-06-26 16:56:18
【问题描述】:

我正在尝试在我的一些 Spring 应用程序测试中使用 this 库,但没有取得多大成功。 Spring Data MongoDB 似乎是supported,但我没有设法让它工作。 examples 不完全遵循@Repository 方法。

假设我有一个 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 扩展,可以告诉我为什么指定的数据集没有被加载到数据库中?

提前谢谢你!

【问题讨论】:

  • jaranda,在您对 nosqlunit 和这篇文章发表评论后,我已经测试了 nosqlunit 并且对我来说工作正常。可能发生在我身上的是 MongoDbRule 和 DummyClassRepository 正在连接到不同的数据库。 MongoDbRule 正在使用“测试”数据库,并且不清楚 DummyClassRepository 正在使用哪个数据库。在我的 MongoTemplate 配置中,我明确设置了应该使用的数据库。正如我在另一个问题中所说,我没有使用 xml 来配置 Spring,至少在我的测试中。
  • 在 spring 数据文档的所有示例中,他们在 db-factory 上使用 dbname 参数:。尝试将“test”指定为 dbname
  • @MiguelCartagena 我怎么错过了!现在可以用了,谢谢!如果您将您的回复添加为答案,我会欣然接受 :) Gracias!

标签: java junit nosql spring-data-mongodb


【解决方案1】:

如果你想要一个很好的例子来说明如何做到这一点。在https://github.com/JohnathanMarkSmith/spring-mongo-demo查看johnathan mark smiths git示例

【讨论】:

  • 感谢您提供此资源 :)
【解决方案2】:

贾兰达,

您的 spring 存储库和 nosqlunit 似乎在不同的数据库上工作。尝试修改你的spring配置以使用与nosqlunit相同的数据库。

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

【讨论】:

    猜你喜欢
    • 2015-06-17
    • 1970-01-01
    • 1970-01-01
    • 2017-11-12
    • 2019-10-26
    • 2012-09-25
    • 1970-01-01
    • 2015-06-19
    • 2015-07-02
    相关资源
    最近更新 更多