Create a mongo connection and make it alive for execution of an Entire Test Suite in Ready!API

被刻印的时光 ゝ 提交于 2019-12-06 03:40:20

Below script address how you achieve what you are looking for in ReadyAPI / SoapUI. Note that you already know how to connect to gmongo in Groovy which you need to add that logic in the place holder by following the comment inline.

Below is the test suite level Setup Script to create the db connection.

class DatabaseDetails {
    def server
    def user
    def password
    def log
    def getConnection() {
        log.info 'connection created'
        //Write logic to create connection
    }
    def closeConnection() {
        log.info 'Closing connection'
        //Write logic to close connection
    }
}
//Change server, user, password values according to your environment
def db = [ server:'localhost', user:'dbuser', password: 'dbuserpasswd', log: log] as DatabaseDetails
if (!db.connection) {
    db.connection
    testSuite.metaClass.db = db 
}

Below is the test suite level TearDown Script to close the db connection. Since this is in tear down script, connection gets closed automatically as soon the test suite execution is completed.

testSuite.db?.closeConnection()

Now, there is no need to have step to create the db connection again and again. You just need to use below script in Groovy Script test step to get the existing db connection.

def conn = context.testCase.testSuite.db?.connection

Using conn variable, you should be able to execute the queries.

Note : Since the db connection is done in Setup Script of test suite, if you just run the test case(i.e., test suite is not invoked or executed), you may not able to get the connection. In such cases, manually execute the Setup Script of the test suite.

EDIT: Based on OP's edit to the question and his code snippet, here is the updated test suite's Setup Script. This takes care of implementation of getConnection() and closeConnection() based on OP's edit. Please add / edit import statements for Mongo classes that are used as I am not really aware of those.

Updated Test Suite's Setup Script

import com.gmongo.*
import com.mongodb.*

class DatabaseDetails {
    def context
    def log
    def mongoClient
    def mongoDB
    def getConnection() {
        log.info 'Creating connection.'
        //Write logic to create connection
        if (!mongoDB){        
        def credentials = MongoCredential.createCredential(
           context.expand('${#Project#MongoUser}'),
           context.expand('${#Project#MongoDatabase}'),
           context.expand('${#Project#MongoPassword}') as char[])
        mongoClient = new MongoClient( new ServerAddress(context.expand('${#Project#MongoServer}')),Arrays.asList(credentials) ) 
        mongoDB = new GMongo( mongoClient ).getDB(context.expand('${#Project#MongoDatabase}'))          
      }
      mongoDB
    }

    def closeConnection() {
        log.info 'Closing connection'
        //Write logic to close connection
        mongoClient.close()
    }
}

def db = [ context: context, log: log] as DatabaseDetails
if (!db.connection) {
    db.connection
    testSuite.metaClass.db = db 
}

As mentioned earlier, to get the connection, use below code and explaining it down.

context.testCase.testSuite.db?.connection

Groovy has great feature called ExpandoMetaclass. db is injected to testSuite class and db is object of DatabaseDetails class that we created and instantiated in Setup Script of test suite.

And db contains getConnection() i.e., db.getConnection() which can also same as db.connection. That is how connection is available in the above statement.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!