Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/373.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java nosqlunitmongodb测试:Spring数据MongoDB方法_Java_Junit_Nosql_Spring Data Mongodb - Fatal编程技术网

Java nosqlunitmongodb测试:Spring数据MongoDB方法

Java nosqlunitmongodb测试:Spring数据MongoDB方法,java,junit,nosql,spring-data-mongodb,Java,Junit,Nosql,Spring Data Mongodb,我试图在我的一些Spring应用程序测试中使用这个库,但没有太多成功。SpringDataMongoDB似乎很有用,但我并没有设法让它工作。该方法不完全遵循@Repository方法 假设我有一个DummyClass POJO: @Document(collection = DummyClass.ITEMS_COLLECTION_NAME) public class DummyClass { public static final String ITEMS_COLLECTION_NAM

我试图在我的一些Spring应用程序测试中使用这个库,但没有太多成功。SpringDataMongoDB似乎很有用,但我并没有设法让它工作。该方法不完全遵循@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 */
}
.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扩展,它可以为我提供一些想法,说明为什么指定的数据集没有加载到数据库中

提前谢谢你

雅兰达

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

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


如果您想要一个关于如何做到这一点的好例子。在jaranda上查看johnathan mark Smith的git示例,在您对nosqlunit和本文发表评论之后,我已经测试了nosqlunit,对我来说效果很好。可能发生在我身上的是MongoDbRule和DummyClassRepository正在连接到不同的数据库。MongoDbRule正在使用“测试”数据库,不清楚DummyClassRepository正在使用哪个数据库。在我的MongoTemplate配置中,我显式地设置了应该使用的数据库。正如我在另一个问题中所说的,至少在我的测试中,我没有使用xml来配置Spring。尝试将“测试”指定为dbname@MiguelCartagena我错过了!现在可以用了,谢谢!如果你加上你的答复,我将欣然接受:)谢谢!感谢您提供此资源:)
{
    "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"
        }
    ]
}
 <mongo:db-factory id="mongoDbFactory" mongo-ref="mongo" dbname="test" />