⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 lib0120.html

📁 java外企软件工程师就业班 J2EE方向 《J2EE架构师手册》 电子书
💻 HTML
字号:
<html>
<META http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<head>
<title>Test Case Coding Overview and Examples</title>
<link rel="STYLESHEET" type="text/css" href="images/xpolecat.css">
<link rel="STYLESHEET" type="text/css" href="images/ie.content.css">
</head>
<body>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr><td><div STYLE="MARGIN-LEFT: 0.15in;"><a href="toc.html"><img src="images/teamlib.gif" width="62" height="15" border="0" align="absmiddle"  alt="Team LiB"></a></div></td>
<td align="right"><div STYLE="MARGIN-LEFT: 0.15in;">
<a href="LiB0119.html"><img src="images/previous.gif" width="62" height="15" border="0" align="absmiddle" alt="Previous Section"></a>
<a href="LiB0121.html"><img src="images/next.gif" width="41" height="15" border="0" align="absmiddle" alt="Next Section"></a>
</div></td></tr></table>
<br>
<div class="chapter">
<a name="ch18"></a>
<div class="section">
<h2 class="first-section-title"><a name="586"></a><a name="ch18lev1sec3"></a>Test Case Coding Overview and Examples</h2><a name="587"></a><a name="IDX-246"></a>
<p class="para">The mechanics of setting up a JUnit test case are relatively straightforward. All test cases extend <span class="fixed">junit.framework.TestCase</span>. You need to override the <span class="fixed">runTest()</span> method, which performs pretest processing, the test itself, and posttest processing. I usually code a <span class="fixed">main()</span> so the test case can be run and debugged outside the test suite. <a class="internaljump" href="#ch18list01">Listing 18.1</a> is an example of a test case from a ProjectTrak data access object.</p>
<div class="example">
<span class="example-title"><span class="example-titlelabel">Listing 18.1: </span>Sample Test Case for a Data Access Object</span><a name="588"></a><a name="ch18list01"></a>
<div class="formalbody">
<table class="BlueLine" border="0" cellspacing="0" cellpadding="0" width="100%">
<tr>
<td bgcolor="000080" class="bluecell"><font size="2" face="Arial" color="010100"><b><img src="_.gif" width="1" height="2" alt="Start example" border="0"></b></font></td>
</tr>
</table>
<pre class="literallayout">
    1:package test.dvt.app.project.dao;
    2:
    3:import com.dvt.app.project.dao.ProjectTaskDAO;
    4:import com.dvt.app.project.vo.ProjectTaskVO;
    5:
    6:import org.cementj.util.DatabaseUtility;
    7:import org.cementj.base.DataNotFoundException;
    8:import junit.framework.TestCase;
    9:
   10:import java.sql.Connection;
   11:
   12:public class TestProjectTaskDAO extends TestCase
   13:{
   14:
   15:  public TestProjectTaskDAO()
   16:  {
   17:      super("ProjectTaskDAO unit tests");
   18:  }
   19:
   20:  protected void setUp() throws java.lang.Exception
   21:  {
   22:    super.setUp();
   23:    _dbConnection =
   24:        DatabaseUtility.getOracleJDBCConnection
   25:            (    "localhost", 1521, "ORA92",
   26:                "scott", "tiger");
   27:  }
   28:
   29:  protected void runTest() throws java.lang.Throwable
   30:  {
   31:      try
   32:      {
   33:        this.setUp();
   34:        ProjectTaskDAO dao =
   35:            new ProjectTaskDAO(_dbConnection);
   36:<a name="589"></a><a name="IDX-247"></a>
   37:       ProjectTaskVO taskVO = null;
   38:
   39:       // Test for data not found.
   40:       boolean dataNotFound = false;
   41:       try      {taskVO = dao.getProjectTask(77777);}
   42:       catch    (DataNotFoundException d)
   43:       {
   44:           dataNotFound = true;
   45:       }
   46:       TestCase.assertTrue("Test 1: Not found test",
   47:                           dataNotFound);
   48:
   49:       // test for data found
   50:       taskVO = dao.getProjectTask(11111);
   51:       TestCase.assertTrue("Test 2: Select test",
   52:                           taskVO != null);
   53:
   54:       // test for task save
   55:       taskVO.setTaskId(77777);
   56:       dao.saveProjectTask(taskVO);
   57:       ProjectTaskVO newTaskVO =
   58:           dao.getProjectTask(77777);
   59:       TestCase.assertTrue("Test 3: Insert test",
   60:                       newTaskVO.equals(taskVO));
   61:
   62:       // test for delete
   63:       dao.deleteProjectTask(77777);
   64:       dataNotFound = false;
   65:       try      {taskVO = dao.getProjectTask(77777);}
   66:       catch    (DataNotFoundException d)
   67:       {
   68:           dataNotFound = true;
   69:       }
   70:       TestCase.assertTrue("Test 4: Delete test",
   71:                           dataNotFound);
   72:     }
   73:     finally
   74:     {
   75:         this.tearDown();
   76:     }
   77:  }
   78:
   79:  protected void tearDown() throws java.lang.Exception
   80:  {
   81:    super.tearDown();
   82:    DatabaseUtility.close(_dbConnection);
   83:  }
   84:
   85:  private Connection         _dbConnection = null;<a name="590"></a><a name="IDX-248"></a>
   86:
   87:  public static void main(String[] args)
   88:  {
   89:    TestProjectTaskDAO test = new TestProjectTaskDAO();
   90:
   91:    try
   92:    {
   93:      test.runTest();
   94:    }
   95:    catch (Throwable t)      {t.printStackTrace();}
   96:  }
   97:}
</pre>
<table class="BlueLine" border="0" cellspacing="0" cellpadding="0" width="100%">
<tr>
<td bgcolor="000080" class="bluecell"><font size="2" face="Arial" color="010100"><b><img src="_.gif" width="1" height="2" alt="End example" border="0"></b></font></td>
</tr>
</table>
<table class="BlankSpace" border="0" cellspacing="0" cellpadding="0" width="100%">
<tr>
<td height="16"></td>
</tr>
</table>
</div>
</div>
<p class="para">I usually put the database connection creation in the setup() method override and the close in the teardown() method override.</p>
<p class="para">Here are a few things to note from the example in <a class="internaljump" href="#ch18list01">listing 18.1</a>:</p>
<ul class="itemizedlist">
<li class="first-listitem">
<p class="first-para">The test is self-contained for the most part.</p>
</li>
<li class="listitem">
<p class="first-para">All assertions and the test case are uniquely labeled so developers can find them easily if they fail in the test suite.</p>
</li>
<li class="listitem">
<p class="first-para">The test case is in a package that only contains test cases and supporting classes.</p>
</li>
<li class="listitem">
<p class="first-para">The test case is named so it's easy for other developers to find.</p>
</li>
</ul>
<div class="section">
<h3 class="sect3-title">
<a name="591"></a><a name="ch18lev2sec1"></a>Combining Test Cases into Suites</h3>
<p class="first-para">I've mentioned that it's fairly easy to combine <span class="fixed">TestCase</span> classes into test suites. JUnit implements this capability via its <span class="fixed">TestSuite</span> class. Once it's instantiated and loaded with <span class="fixed">TestCase</span>, the <span class="fixed">TestSuite</span> class can be run via JUnit's <span class="fixed">TestRunner</span> class, which is a GUI utility. <a class="internaljump" href="#ch18list02">Listing 18.2</a> is a sample test suite from one of my open source products.</p>
<div class="example">
<span class="example-title"><span class="example-titlelabel">Listing 18.2: </span>Sample Test Suite from ThreadWorks</span><a name="592"></a><a name="ch18list02"></a>
<div class="formalbody">
<table class="BlueLine" border="0" cellspacing="0" cellpadding="0" width="100%">
<tr>
<td bgcolor="000080" class="bluecell"><font size="2" face="Arial" color="010100"><b><img src="_.gif" width="1" height="2" alt="Start example" border="0"></b></font></td>
</tr>
</table>
<pre class="literallayout">
    1:public class ThreadworksTestSuite {
    2:
    3:  public ThreadworksTestSuite() {}
    4:
    5:  public static Test suite()
    6:  {
    7:    TestSuite suite= new TestSuite("Threadworks");
    8:
    9:    suite.addTest(new InstantiationTest() );
   10:    suite.addTest(new SingleTaskWithoutNotification() );
   11:    suite.addTest(new SingleTaskWithNotification() );<a name="593"></a><a name="IDX-249"></a>
   12:    suite.addTest(new TerminationTest() );
   13:    suite.addTest(new GroupTaskWithNotification() );
   14:    suite.addTest(new GroupTaskWithoutNotification() );
   15:    suite.addTest(
   16:    new TaskCollectionWithNotification() );
   17:    suite.addTest(
   18:    new TaskCollectionWithoutNotification() );
   19:    suite.addTest(new SuccessorTasksWithNotification() );
   20:    suite.addTest(
   21:    new ScheduleNonRecurringWithoutDependencies() );
   22:    suite.addTest(
   23:    new ScheduleNonRecurringWithDependencies() );
   24:
   25:    return suite;
   26:  }
   27:
   28:  public static void main(String[] argv)
   29:  {
   30:    junit.swingui.TestRunner.run(
   31:    ThreadworksTestSuite.class);
   32:
   33:  }
   34:}
</pre>
<table class="BlueLine" border="0" cellspacing="0" cellpadding="0" width="100%">
<tr>
<td bgcolor="000080" class="bluecell"><font size="2" face="Arial" color="010100"><b><img src="_.gif" width="1" height="2" alt="End example" border="0"></b></font></td>
</tr>
</table>
<table class="BlankSpace" border="0" cellspacing="0" cellpadding="0" width="100%">
<tr>
<td height="16"></td>
</tr>
</table>
</div>
</div>
</div>
</div>
</div><br>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr><td><div STYLE="MARGIN-LEFT: 0.15in;"><a href="toc.html"><img src="images/teamlib.gif" width="62" height="15" border="0" align="absmiddle"  alt="Team LiB"></a></div></td>
<td align="right"><div STYLE="MARGIN-LEFT: 0.15in;">
<a href="LiB0119.html"><img src="images/previous.gif" width="62" height="15" border="0" align="absmiddle" alt="Previous Section"></a>
<a href="LiB0121.html"><img src="images/next.gif" width="41" height="15" border="0" align="absmiddle" alt="Next Section"></a>
</div></td></tr></table>
</body></html>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -