简体   繁体   English

Java MongoDB collection.insert为spock单元测试返回null

[英]Java MongoDB collection.insert returns null for spock unit test

I am writing a unit test case to verify my method insertToCollection is working fine or not. 我正在编写一个单元测试用例,以验证我的方法insertToCollection是否正常工作。

InsertToMongoCollection.java InsertToMongoCollection.java

protected void insertToCollection() {
   collection.insert(recordList);
}

SPOCK: SPOCK:

def "InsertToMongoCollection"() {
   given:
   Record record = new Record()
   record.setId("111")
   Collection collection= Mock()
   InsertToMongoCollection.collection = collection

   when:
   InsertToMongoCollection.addToGroup(record) // this inserts into recordList
   List<Record> result = collection.insertToCollection()

   then:
   result.count() == 1
}

The mocking is done correctly and the recordList also has 1 record. 模拟正确完成,并且recordList也有1条记录。 If I debug and see. 如果我调试看看。 But when the collection.insert is called, the result is null. 但是,当调用collection.insert时,结果为null。

insertToCollection had a void return type, so will return null when you do insertToCollection的返回类型为void ,因此执行此操作时将返回null

List<Record> result = collection.insertToCollection()

Change the return type of the method so you can test it, or run up a fake mongo, and check the records are inserted 更改方法的返回类型,以便您可以对其进行测试,或者运行一个假的mongo,然后检查记录是否已插入

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM