📄 bestpractices.html
字号:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html> <head> <title> DbUnit - The Dbunit Framework - Best Practices </title> <style type="text/css"> @import url("./style/tigris.css"); @import url("./style/maven.css"); </style> <link rel="stylesheet" href="./style/print.css" type="text/css" media="print"></link> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"></meta> </head> <body class="composite"> <div id="banner"> <table border="0" width="100%" cellpadding="8" cellspacing="0"> <tr> <td> <a href="http://www.sourceforge.net/projects/dbunit"> <img border="0" alt="DbUnit.org" src="http://sourceforge.net/sflogo.php?group_id=47439&type=5" align="left"></img> </a> </td> <td> <div id="login" align="right"> <a href="http://www.dbunit.org"> <img border="0" alt="DbUnit" src="./images/dbunit-logo.jpg" align="right"></img> </a> </div> </td> </tr> </table> </div> <div id="breadcrumbs"> <table border="0" width="100%" cellpadding="4" cellspacing="0"> <tr> <td>Last published: 31 May 2004 | Doc for 2.1 </td> <td> <div align="right"> <a href="http://www.dbunit.org/wiki/"><img class="handle" src="./images/none.png" alt=""></img>Wiki</a> | <a href="http://www.sf.net/projects/dbunit"><img class="handle" src="./images/none.png" alt=""></img>SF.net Project Page</a> | <a href="http://maven-plugins.sourceforge.net/maven-dbunit-plugin/index.html"><img class="handle" src="./images/none.png" alt=""></img>Maven Plugin</a> </div> </td> </tr> </table> </div> <table border="0" width="100%" cellpadding="8" cellspacing="0"> <tr valign="top"> <td width="20%" id="leftcol"> <div id="navcolumn"> <div> <strong>Quick Links</strong> <div> <small> <a href="http://sourceforge.net/project/showfiles.php?group_id=47439&release_id=211888"><img class="handle" src="./images/none.png" alt=""></img>Download</a> </small> </div> <div> <small> <a href="changes-report.html"><img class="handle" src="./images/none.png" alt=""></img>Changes</a> </small> </div> <div> <small> <a href="faq.html"><img class="handle" src="./images/none.png" alt=""></img>FAQ</a> </small> </div> <div> <small> <a href="https://sourceforge.net/support/getsupport.php?group_id=47439"><img class="handle" src="./images/none.png" alt=""></img>Get Support</a> </small> </div> <div> <small> <a href="http://www.dbunit.org/wiki/"><img class="handle" src="./images/none.png" alt=""></img>Wiki</a> </small> </div> <div> <small> <a href="cvs-usage.html"><img class="handle" src="./images/none.png" alt=""></img>Source</a> </small> </div> <div> <small> <a href="apidocs/index.html"><img class="handle" src="./images/none.png" alt=""></img>JavaDocs</a> </small> </div> </div> <div> <strong>Overview</strong> <div> <small> <a href="index.html"><img class="handle" src="./images/none.png" alt=""></img>About DbUnit</a> </small> </div> <div> <small> <a href="intro.html"><img class="handle" src="./images/none.png" alt=""></img>Database Testing</a> </small> </div> <div> <small> <a href="howto.html"><img class="handle" src="./images/none.png" alt=""></img>Getting Started</a> </small> </div> <div> <small> <b><a href="bestpractices.html"><img class="handle" src="./images/none.png" alt=""></img>Best Practices</a></b> </small> </div> <div> <small> <a href="components.html"><img class="handle" src="./images/none.png" alt=""></img>Core Components</a> </small> </div> <div> <small> <a href="properties.html"><img class="handle" src="./images/none.png" alt=""></img>Properties</a> </small> </div> <div> <small> <a href="anttask.html"><img class="handle" src="./images/none.png" alt=""></img>Ant Task</a> </small> </div> <div> <small> <a href="resources.html"><img class="handle" src="./images/none.png" alt=""></img>Resources</a> </small> </div> </div> <div> <strong>Project Documentation</strong> <div> <small> <a href="index.html"><img class="handle" src="./images/none.png" alt=""></img>About DbUnit</a> </small> </div> <div> <small> <a href="project-info.html"><img class="handle" src="./images/collapsed.png" alt=""></img>Project Info</a> </small> </div> <div> <small> <a href="maven-reports.html"><img class="handle" src="./images/collapsed.png" alt=""></img>Project Reports</a> </small> </div> <div> <small> <a href="http://maven.apache.org/development-process.html"><img class="handle" src="./images/none.png" alt=""></img>Development Process</a> </small> </div> </div> <div style="margin-top: 20px; width: 100%; text-align: center;"> <a href="http://maven.apache.org/" title="Powered by Maven"><img style="border: 1px solid black" alt="Powered by Maven" src="./images/logos/maven-propaganda.png"></img></a> </div> </div> </td> <td rowspan="2"> <div id="bodycol"> <div class="app"> <div class="h3"> <h3> Best Practices </h3> <ol> <li> <a href="#onedbperdev"> Use one database instance per developer </a> </li> <li> <a href="#nocleanup"> Good setup don't need cleanup! </a> </li> <li> <a href="#smalldatasets"> Use multiple small datasets </a> </li> <li> <a href="#staledata"> Perform setup of stale data once for entire test class or test suite </a> </li> <li> <a href="#connections"> Connection management strategies </a> </li> </ol> <h3> <a name="onedbperdev"> Use one database instance per developer </a> </h3> <p> Testing can be simplified if you can get your database in a known state before a test is run. A database should only be used for one test at a time; otherwise the database state cannot be guarantied. </p> <p> So multiple developers working on the same project should have their own database instance to prevent data corruption. This also simplifies database cleanup, as you don't necessarily need needs to revert it to its initial state. </p> <h3> <a name="nocleanup"> Good setup don't need cleanup! </a> </h3> <p> You should always avoid creating tests that depends on results of preceding tests; thankfully this is the main purpose of DbUnit. </p> <p> Don't be afraid to leave your trace after a test; principally if you are using <a href="#onedbperdev"> one database instance per developer </a> . If you always put your database in a known state before a test execution, you usually don't need to clean it up.. This simplifies your tests maintenance and reduces the overhead taken by the cleanup procedure. And sometimes, this is very helpful to manually verify the database state after executing a test that fails. </p> <h3> <a name="smalldatasets"> Use multiple small datasets </a> </h3> <p> Most of your tests do n ot require the entire database to be re-initialized. So, instead of putting your entire database data in one large dataset, try to break it into many smaller chunks. </p> <p> These chunks could roughly corresponding to logical units, or components. This reduces the overhead caused by initializing your database for each test. This also facilitates team development since many developers working on different components can modify datasets independently. </p> <p> For integrated testing, you can still use the CompositeDataSet class to logically combine multiple datasets into a large one at run time. </p> <h3> <a name="staledata"> Perform setup of stale data once for entire test class or test suite </a> </h3> <p> If several tests are using the same read-only data, this data could be initialized once for an entire test class or test suite. You need to be cautious and ensure you never modify this data. This can reduce the time required to run your tests but also introduces more risk. <br> </br> </p> <h3> <a name="connections"> Connection management strategies </a> </h3> <p> Here are the recommended connection management strategies depending whether you test from a remote client or an in-container strategy: </p> <h4> Remote client with DatabaseTestCase </h4> <p> You should try to reuse the same connection for the entire test suite to reduce the overhead of creating a new connection for each test. Since version 1.1, DatabaseTestCase is closing every connection in setUp() and tearDown(). Override the closeConnection() method with an empty body to modify this behavior. </p> <h4> In-container with Cactus or JUnitEE </h4> <p> If you use the in-container strategy you should use the DatabaseDataSourceConnection class to access the DataSource you configured for yo ur application server. JDBC connections are requested on demand from the DataSource. So you can rely on the built-in connection pooling capability of your application server to achieve good performance. <div id="source"> <pre>IDatabaseConnection connection = new DatabaseDataSourceConnection( new InitialContext(), "jdbc/myDataSource");</pre> </div> </p> </div> </div> </div> </td> </tr> </table> <div id="footer"> <table border="0" style="width:100%" cellpadding="4" cellspacing="0"> <tr> <td>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -