Automated component Testing
Component testing or subsystem testing verifies all parts of the system. You may need to install the entire system or some external dependencies, such as databases, file systems, or network terminals. These tests verify that the interaction between components can produce the expected combination behavior. Typical component tests require the support of the underlying database, and may even span the architectural boundary. Because each test case executes a larger amount of code and the code coverage of each test is also greater, these tests generally take longer to execute than the unit test.
Code List 6-3 shows an example of component testing. It uses the dbunit framework to generate the base data in the database and then tries to locate the data based on the content in the database. Dbunit uses XML files to read these files and insert the corresponding data into the matched database table.
Code List 6-3 test using dbunit Components
Public class defaultworddaoimpltestextends databasetestcase {
Protected idataset getdataset () throws exception {
Return new flatxmldataset (newfile ("test/CONF/wseed. xml "));
}
Protected idatabaseconnectiongetconnection () throws exception {
Final class driverclass =
Class. forname ("org. gjt. Mm. MySQL. Driver ");
Final connection jdbcconnection =
Drivermanager. getconnection (
"JDBC: mysql: // localhost/words ",
"Words", "Words ");
Return newdatabaseconnection (jdbcconnection );
}
Public voidtestfindverifydefinition () throws exception {
Final worddaoimpl Dao = newworddaoimpl ();
Final iword WRD = Dao. findword ("pugnacious ");
For (iterator iter =
WRD. getdefinitions (). iterator ();
ITER. hasnext ();){
Idefinition def = (idefinition) ITER. Next ();
Testcase. assertequals (
"Def is not combative in nature; belligerent .",
"Combative in nature; belligerent .",
Def. getdefinition ());
}
}
Publicdefawordworddaoimpltest (string name ){
Super (name );
}
}
Component-level tests use more dependencies than unit tests, but not necessarily as much as more advanced system tests (defined later. Component-level tests use APIs to execute code, but these APIs may be exposed to or not exposed to customers. In code listing 6-3, an object in the DaO layer is tested through the exposed interface. Another example of component testing is to use the strutstestcase framework to test a sequence class in the struts architecture, as shown in code list 6-4. This test obviously requires a database to be executed, but the Web container is virtual, And the APIS executed do not need to be exposed to the customer.
Code List 6-4 Use the strutstest component for testing
Publicclass projectviewactiontest extends deftmeinmockstrutstestcase {
Public voidtestprojectviewaction () throws exception {
This. addrequestparameter ("projectid", "100 ");
This. setrequestpathinfo ("/viewprojecthistory ");
This. actionperform ();
This. verifyforward ("success ");
Project project = (Project) This. getrequest ()
. Getattribute ("project ");
Assertnotnull (project );
Assertequals (project. getname (), "ds ");
}
Protected stringgetdbunitdatasetfileforsetup (){
Return "dbunit-seed.xml ";
}
Publicprojectviewactiontest (string name ){
Super (name );
}
}
In code listing 6-4, the strutstestcase framework and dbunit provide the function of filling the base data into the database and a virtual container. The deftmeinmockstrutstestcase class is a template that requires the getdbunitdatasetfileforsetup method.
This type of test is also called "integration test ". This type of test differs from system test in that integration test (or component test or subsystem test) does not always execute the APIs that are expected to be made public. For example, a system test can be executed through the web page of the Web application, but a component test can be executed at the business layer under the web page of the application.
This article is excerpted from "continuous integration: the path to software quality improvement and risk reduction ".
Book details: http://blog.csdn.net/broadview2006/article/details/7660148
[Us] Paul M. Duvall
(Paul. M. Duval) Steve Matyas
(Steve mayyes) by Andrew GLOVER
Translated by Wang Haipeng
Published by Electronic Industry Publishing House