soapui + groovy + mongo 数据库断言问题
soapui + groovy + mongo db assert issue
使用给定的代码,我尝试连接到 mongo 数据库,而不是按事件类型 select 事件,而不是断言我刚刚 selected
@Grab('org.mongodb:mongodb-driver:3.4.1')
import com.mongodb.MongoClient
import com.mongodb.MongoClientURI
import com.mongodb.DBCollection
import com.mongodb.DB
import com.mongodb.DBCursor;
import com.mongodb.BasicDBObject
import com.mongodb.DBObject;
import groovy.json.JsonSlurper;
class MongoService {
private MongoClient mongoClient
def login = "user"
def password = "pass"
def host = "host"
def port = port
def databaseName = 'mongodb'
public MongoClient client() {
mongoClient = new MongoClient(new MongoClientURI(
new StringBuilder("mongodb://").
append(login).append(":").
append(password).append("@").
append(host).append(":").
append(port).append("/").
append(databaseName).toString()))
;
return mongoClient
}
public DBCollection collection(collectionName) {
DB db = client().getDB(databaseName)
return db.getCollection(collectionName)
}
}
def service = new MongoService(databaseName: 'mongodb')
def foo = service.collection('events')
BasicDBObject whereQuery = new BasicDBObject();
whereQuery.put("EventType", "test");
DBCursor cursor = foo.find(whereQuery);
while (cursor.hasNext()) {
log.info(cursor.next())
}
def json = cursor.next()
def slurper = new groovy.json.JsonSlurper()
def result = slurper.parseText(json)
assert result.EventType == "test"
I return soapui returns java.util.NoSuchElementException 错误 line:52
使用相同的查询 mongodb 手动检查 db.getCollection('events').find({"EventType": "test"})
returns 1 个对象。我不知道如何让它工作...:/
问题在于您访问光标的方式。
您在调用 log.info(cursor.next())
时已经耗尽了游标,而当您调用 cursor.next()
时,没有可访问的元素,这就是该异常的原因。
DBCursor cursor = foo.find(whereQuery);
while (cursor.hasNext()) {
log.info(cursor.next())
}
def json = cursor.next()
因此解决方法是更改您的代码以在 while 循环中每次迭代调用 cursor.next()
。
使用给定的代码,我尝试连接到 mongo 数据库,而不是按事件类型 select 事件,而不是断言我刚刚 selected
@Grab('org.mongodb:mongodb-driver:3.4.1')
import com.mongodb.MongoClient
import com.mongodb.MongoClientURI
import com.mongodb.DBCollection
import com.mongodb.DB
import com.mongodb.DBCursor;
import com.mongodb.BasicDBObject
import com.mongodb.DBObject;
import groovy.json.JsonSlurper;
class MongoService {
private MongoClient mongoClient
def login = "user"
def password = "pass"
def host = "host"
def port = port
def databaseName = 'mongodb'
public MongoClient client() {
mongoClient = new MongoClient(new MongoClientURI(
new StringBuilder("mongodb://").
append(login).append(":").
append(password).append("@").
append(host).append(":").
append(port).append("/").
append(databaseName).toString()))
;
return mongoClient
}
public DBCollection collection(collectionName) {
DB db = client().getDB(databaseName)
return db.getCollection(collectionName)
}
}
def service = new MongoService(databaseName: 'mongodb')
def foo = service.collection('events')
BasicDBObject whereQuery = new BasicDBObject();
whereQuery.put("EventType", "test");
DBCursor cursor = foo.find(whereQuery);
while (cursor.hasNext()) {
log.info(cursor.next())
}
def json = cursor.next()
def slurper = new groovy.json.JsonSlurper()
def result = slurper.parseText(json)
assert result.EventType == "test"
I return soapui returns java.util.NoSuchElementException 错误 line:52
使用相同的查询 mongodb 手动检查 db.getCollection('events').find({"EventType": "test"})
returns 1 个对象。我不知道如何让它工作...:/
问题在于您访问光标的方式。
您在调用 log.info(cursor.next())
时已经耗尽了游标,而当您调用 cursor.next()
时,没有可访问的元素,这就是该异常的原因。
DBCursor cursor = foo.find(whereQuery);
while (cursor.hasNext()) {
log.info(cursor.next())
}
def json = cursor.next()
因此解决方法是更改您的代码以在 while 循环中每次迭代调用 cursor.next()
。