Now that the virtual service is connected to the database and is running, let's send some requests to it.
From ReadyAPI
-
Create a test suite and a test case. Then, add the JDBC Request test step. For more information on how you do this, see the Creating Your First Functional Test tutorial.
-
In the test step editor, configure the database connection:
-
Make sure Connection is set to <None>.
-
In the Driver field, enter
com.smartbear.servicev.jdbc.driver.JdbcVirtDriver
. This commands ReadyAPI to use the included JDBC driver to connect to the virtual service.You do not need to install the JdbcVirtDriver
driver when working with JDBC virtual services from ReadyAPI.
To use a different JDBC client, download the driver from the SmartBear website and install it in the client application. -
Specify the service’s Connection String. By default, virtual services that are run locally have the
jdbc:servicev://localhost:10080
address. If you use a different port, or run the virtual service on a different computer, the address will be different.
To make sure your connection parameters are correct, click .
ReadyAPI will test the connection to the virtual service without sending a request.
-
-
Enter an SQL Query for your request. For example:
Select city.*
From cityDo not use the Build Query button to create a query. ReadyAPI sends requests to the database to get table information and these requests will also be recorded. This may make it hard to find the real request. -
Click to run the test step or the entire test. The virtual service will route the sent request to the real database and then return the response from it.
From Java code
-
Add the JDBC driver to your project. You can do this in two ways:
-
If you use Maven, add the service-v-jdbc-driver dependency to your .pom file:
<dependencies>
<dependency>
<groupId>com.smartbear.readyapi</groupId>
<artifactId>servicev-jdbc-virt-driver</artifactId>
<version>1.0.0</version>
</dependency>
</dependencies> -
Otherwise, download the driver manually and place it to the
<your-project>/lib
folder. Make sure your IDE loads the driver.
-
-
Then, connect to the virtual service from your code and send requests. Here is an example that sets up the connection and sends the simple Select query:
Java
import java.sql.*;
public class JDBCClient
{
public static void main(String[] args) throws SQLException {
// The JDBC service connection string
String connString = "jdbc:servicev://localhost:10080";
// Create the connection instance
JDBCClient connServer = new JDBCClient();
// Call the method below to execute a Select query
connServer.JDBCExecuteSelect(connString,
"",
"",
"Select table1.*" +
"From table1");
}
public void JDBCExecuteSelect(String db_connect_string,
String db_userid,
String db_password,
String queryString) throws SQLException {
// Create a connection to the database
Connection conn = connectToJDBC(db_connect_string, db_userid, db_password);
try {
// Create an SQL statement object
Statement statement = conn.createStatement();
// Get the results from the database
ResultSet rs = statement.executeQuery(queryString);
// Loop through the database results and print out the data
while (rs.next()) {
for (int i = 1; i < rs.getMetaData().getColumnCount() + 1; i++)
System.out.print(rs.getString(i) + " ");
System.out.print("\r\n");
}
}
// Catch the possible exception and print out its text
catch (SQLException e) {
e.printStackTrace();
}
// Close the connection
finally {
conn.close();
}
}
public Connection connectToJDBC(String db_connect_string, String db_userid, String db_password)
{
try {
// Register the VirtServer JDBC driver
Class.forName("com.smartbear.servicev.jdbc.driver.JdbcVirtDriver");
// Open the connection to the database
Connection conn = DriverManager.getConnection(db_connect_string);
System.out.println("connected");
return conn;
}
// Catch the possible exception and print out its text
catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
After the virtual service has recorded at least one transaction with the database it is ready to simulate responses. Next, we will modify the response returned by the virtual service.
Note: | The virtual service will record the transaction and respond to the same request instead of the database. |