If you want to make an gmongo
connection alive for an entire test suite and then close it in a tear down operation after the entire test suite is executed then, How could we do that?
Currently what am I doing is, I am creating an connection for an particular test step and then after the test step is executed, I close the connection by using the code mongoClient.close()
But now there is a requirement where I need to create the connection before the test suite starts executing, use the same connection throughout the test suite inside the test cases/test steps and then close the connection the connection after the entire test suite gets executed.
Could anyone please tell me how could I do this using Ready!API?
I may sound retard cause I am new to Ready API so please bear with me This is the code that I use to create an Connection to mongo
def dbUser = context.expand( '${#Project#MongoUser}' )
def dbPassword = context.expand( '${#Project#MongoPassword}' )
def dbServer = context.expand( '${#Project#MongoServer}' )
def dbDatabase = context.expand( '${#Project#MongoDatabase}' )
def credentials = MongoCredential.createCredential(dbUser,dbDatabase,dbPassword as char[])
def mongoClient = new MongoClient( new ServerAddress(dbServer),Arrays.asList(credentials) )
context.gmongo = new GMongo( mongoClient )
context.mongoDB = context.gmongo.getDB(dbDatabase)
So i have been using the current code in order to create the connection. Actually I want this as three test suites. The First Test Suite would contain the groovy script to create the connection, The Second Test Suite would contain all of my Test Cases and the Third test suite would contain the mongo close connection script.
We use the Environment values from the properties file. Here the MongoServer has the values of the environment in which the connection is laid
I could not understand @Rao, how did you call the conn variable inside the test cases. Especially the context.testCase.testSuite.db?.connection part. What does the "?" denote and could you please tell me in the above context, how could carry out the process
Below script address how you achieve what you are looking for in
ReadyAPI / SoapUI
. Note that you already know how to connect togmongo
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.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.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.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 theSetup 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 ofgetConnection()
andcloseConnection()
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
As mentioned earlier, to get the connection, use below code and explaining it down.
Groovy has great feature called ExpandoMetaclass.
db
is injected totestSuite
class anddb
is object ofDatabaseDetails
class that we created and instantiated inSetup Script
of test suite.And
db
containsgetConnection()
i.e.,db.getConnection()
which can also same asdb.connection
. That is how connection is available in the above statement.