Testing Java applications with Jdbcproxy

Source: Internet
Author: User
Tags db2 prepare stmt

When we test a Java application, we often need to connect to the database and get accurate test data from the database to test the correctness of the application. However, the task of preparing test data is complex, and once the data in the database changes, it can be time-consuming to revert to the previous version. For those testers who do not have the conditions to connect to the database, the test is not going to work. Therefore, if you can prepare a complete set of test data for a pending application, it is very convenient for program development and testers to test the application without relying on a specific database.

Brief introduction

Jdbcproxy is a SourceForge open source Java project, written in the Java language, followed by the LGPL and MPL1.1 protocols, developed by Frans van Gool, and supports JDBC2.0 specifications. By inheriting and overriding the JDBC2.0 interface, the process of accessing a Java application to the database is recorded in an XML file, which is reproduced from the database to reproduce the call process. Jdbcproxy can be used in tests of Java applications to prepare data and simulate the process of database invocation. Readers can obtain the latest program source code and documentation from the Jdbcproxy home page. The latest version is currently 1.1.

Using Jdbcproxy instead of ordinary database calls can satisfy many requirements of program development and testers, making it easier to prepare test data. Take the article query system for example, some test cases need to test the page when there is no data in the database-display pages without corresponding data; some test cases need to test the display of the page when there is only one piece of data in the database--Display the content of the article instead of the list of articles , and some test cases need to test the page's paging effect, you need to prepare different test data for your program. If you use a direct connection test database for testing, different test cases need to be prepared to test the database, the operation is more complex, and can not simultaneously test different test scenarios. If you use Jdbcproxy, you can prepare different test data files for the same Java program, and testers can get out of the back-end database and only need access to the data file to complete the test. The process of preparing a test environment is greatly simplified by testing the application at the same time that different testers can be unaffected.

Using Jdbcproxy

Jdbcproxy provides two ways to record the JDBC invocation process, one that is easy for developers and testers to read, and one that is used to replay the JDBC call process.

The first approach is primarily to allow developers and testers to understand the details of Java application invocation JDBC, when the application is more complex-for example, a multiple-table query, which allows developers and testers to see the application's access to the database at a glance.

The second approach is one of the key steps in preparing the data for the test program. To replay the JDBC call process, Jdbcproxy processes each application's request and response (response) to the database and generates a series of XML files in the specified directory, which are the basis for replaying the JDBC call process. These request/response files cannot be invoked directly by Jdbcproxy, and Jdbcproxy also provides a Stubtracemerger tool to integrate these request/response files into a single file. This consolidated file is the data file we need to test the Java application.

Jdbcproxy provides a mechanism for sending requests through HTTP servers and receiving responses that are recorded in the consolidated file in the order of the application's JDBC invocation, and jdbcproxy through the HTTP service when the Java application attempts to connect to the database via JDBC The device reads the corresponding data from the consolidated file, simulates the process of invoking the database, and allows the user to test the Java application from the database.

So to test a Java application with Jdbcproxy, you first need to generate a Request/response file to play back the JDBC call process, then consolidate the request/response files in one file, and finally provide the consolidated data file To the HTTP server, so that jdbcproxy can access the data files through the HTTP server to achieve the goal of testing out of the database.

The following is a detailed introduction to each of the features provided by Jdbcproxy.

Documenting the JDBC invocation process that facilitates the reading of developers and testers

Jdbcproxy can track the JDBC call process and generate file formats that are easy for developers and testers to read. In normal database calls, if developers and testers want to know the calling process for JDBC, or if they need to view a full SQL statement, they need to print the information manually or log in. This function of Jdbcproxy is like automatically logging a log for the invocation of JDBC, where developers and testers can obtain each method invoked from the log, as well as the parameters and return values of the calling method. This makes it easy for developers and testers to find database access failures due to misspelled SQL statements or null-pointer exceptions due to the absence of data in the database, making the application's database access process more intuitive. You can generate the JDBC call procedure based on the sample code in Listing 1.

Listing 1

Import java.sql.Statement
public class Jdbcproxydemo {
public static void Main (String args[]) throws      Exception {
String driver = "Nl.griffelservices.proxy.jdbc.oracle.StubTracerDriver";
String url = "Jdbc:tracer::com.ibm.db2.jdbc.app.db2driver:jdbc:db2:sample";
Class.forName (driver);
Connection Connection = drivermanager.getconnection (URL);
Statement stmt = Connection.createstatement ();
Stmt.executeupdate ("DELETE from Greetings WHERE greeting= ' good Night '");//delete
Stmt.executeupdate ("in SERT into Greetings VALUES (' Good Evening ') "); Insert
Stmt.executeupdate ("UPDATE greetings SET greeting = ' good Night '
WHERE greeting = ' Good Eveni Ng ' ");        Update
ResultSet rs = stmt.executequery ("SELECT * from Greetings"),//select
while (Rs.next ()) {
System.out.println (rs.getstring ("greeting"));     
}
Rs.close ();
Stmt.close ();
Connection. Close ();
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.