Test the runner is very primitive by using ANT to automate testing as described earlier. If you are running Ant to compile your project, the compilation file is a good way to run unit test. (For the introduction of Ant, please refer to my article "Ant Introduction" (Starting With Ant), published in Oracle Magazine from 11/12 months. Suppose your source file is in the src directory, the generated class is in the TMP directory, and the junit.jar library is in the libdirectory directory of the project, then you can compile the Java source file and use the compilation file shown in Listing 3 ( In the root of the project) running unit test. The core of this compilation file is the test target for running unit tests. Running these tests is the only task of this target JUnit. In order to run this optional task, you must first put the junit.jar library into the lib directory in the Ant installation directory, then download and install the ANT optional task libraries in the same directory. The example in Listing 3 nested a ClassPath class, which includes a JUnit library and a class; an example is also nested in the example, which uses a FileSet element that selects the appropriate source file defines the test that will run. This task also includes Haltonfilure and HaltonError properties, which tell ANT whether they should stop when encountering a failure or error. If you set their values to "true", Ant will stop when you encounter the first failure or error, and compile will fail (obviously, this means there is a problem during the running test). On the other hand, if the value is set to "false", the result is not very clear (even if the test fails, the compilation will succeed), but all tests will continue. The PRINTSUMMARY attribute indicates whether the ANT displays the output of the run test. Numerical withoutanderr can easily tell ANT display standard output and error output when developing testing. The value OFF indicates that no content is displayed, while the ON only displays the test report (no test class output). The JUnit Task has a lot of properties, please refer to the ANT documentation for details. To test this compilation file, you need to create a directory named SRC, TMP, and LIB. Place the junit.jar library into the lib directory and place the sample java source file as seen in the SRC directory. Open the terminal, enter the root directory of the project, and enter Ant, the result is: $ ANT
Buildfile: build.xml
Clean:
DELETING DIRECTORY
/ Users / Casa / Doc / Oracle
/ junit / prj / TMP
[mkdir] create Dir: / Users / Casa
/ DOC / ORACLE / JUnit / Prj / TMP
BIN:
[Javac] Compiling 4 Source Files
To / users / Casa / Doc / Oracle
/ junit / prj / TMP
Test:
[junit] Running isodatetest
[JUnit] Tests Run: 1, Failures:
0, Errors: 0, Time Elapsed:
0,005 sec
[junit] Running isodatetest2
[JUnit] Tests Run: 2, Failures: 0,
Errors: 0, Time Elapsed: 0,031 sec
[junit] Output:
[junit] setup ()
[junit] Testisodate ()
[junit] Teardown ()
[junit] setup ()
[junit] TestTString () [junit] Teardown ()
All:
Build Successful
Total Time: 8 seconds
Ant can also generate a very useful test report for HTML format. In order to generate such a report, use the previous test target instead: Description = "Run Junit Tests"> PRINTSUMMARY = "withoutanderr"> INCLUDES = "** / * test * .java" /> batchtest> junit> Includes = "Test - *. xml" /> Todir = "$ {TMP}" /> junitreport> target> This goal is the same as the front goals, just the target adds a new attribute in the BatchText element, which tells Ant to generate an Extensible Markup Language (XML) report in the TMP directory. This goal also adds a new JUnitReport element to generate an HTML report by an XML file. This element requires the installation of the Xalan library in the lib directory where Ant is installed (see the JunitReport section of the ANT document: ant.apache.org/manual/install.html). This element also defines the target directory of files generated using the Todir property. The XML file that needs to be processed by nested a Fileset element to generate this report. The desired output format is implemented using nested report elements. This object will generate a report such as the report shown in Figure 4. Such reports are particularly useful when the unit is automatically run (such as during the night compilation). In these cases, errors or failures do not interrupt the test, so you must set the HaltonFailure and HaltonError properties of the JUnit tasks mentioned earlier to "false". This report is also very useful for measuring the implementation process (such as when you have to rewrite the code, or before the implementation has been prepared). Ant is also very useful to start JUnit graphics runners. The following object will start SWING test runner: Description = "Run Graphical Junit"> ClasspathRef = "cp" Fork = "true" /> target> You should run this object in the terminal and compile it using Ant in another terminal or in the IDE you like. This way allows you to start the graphic runner every time you want to test the code. JUnit integration in Oracle9i JDeveloper Oracle9i JDeveloper is not based on network integration JUnit, but only a few minutes of downloading and installing this plugin. In order to complete this process, select the "Check for Updates" item under the "Help" menu of JDeveloper. This will open the IDE update wizard to connect to the Oracle technology website, download the plugin and install it. When the plugin is installed, you need to turn off and restart Oracle9i JDeveloper. Note that the wizard also downloads the relevant documentation. By providing a wizard for each task, this plugin greatly improves the work efficiency of developers writing test instances, test packages, and fixture. To call these wizards, click the "New" item under the "File" menu, then select the "General / Unit Tests" class and select the appropriate wizard from the right form. You can also start the test suite from the interface. When you are ready to test the project, you should first use the dedicated wizard to write FixTure, and then the test instance will be used to integrate into the test instance. In addition, some wizards used to generate custom test fixture and the wizard of generating business components and database connections to test fixture. These two wizards generate a dedicated code to use the setup () and Teardown () methods to set up and publish business components or database connections. When FixTure is completed, the next step should use the appropriate wizard to generate a test instance, which allows you to select the classes and methods to test. You can also choose FixTure used in this test. This will generate a code framework that uses the body of the test method. Finally, the kit should be generated to run your test. This dedicated wizard allows you to choose the test to be included in the suite and generate the entire class for you. To launch a test suite, click the file in the browser and select Run. This will start the graphical interface and run the kit. Select "Help Topics" in the "Help" menu, you will find a detailed tutorial for how to use these wizards in the JDeveloper documentation. This will open the help system window. Click the "Unit Testing with Junit" item and select the appropriate tutorial. This integration between JUnit and JDeveloper enables you to write only the code you interested in the unit test, and let the tool write repeated code for you. Next step Visit the JUnit website www.junit.org Download Oracle9i JDeveloperotn.racle.com/software/products/JDev / Oracle9i Application Server OTN.Oracle.com/software/products/ias/ Learn Oracle9i JDeveloper Extension Otn.racle.com/products/JDEV / HTDOCS / Partners / Addins Read Oracle9i JDeveloper Document Otn.Orcle.com/docs/products/jdev/ JUnit Best Practice Before using JUnit, you should pay attention to: Write test code before implementation. This is an implementation of a contract-driven implementation. Only methods that may be interrupted (that is, in most cases, the setter and getter methods should not be tested. Test as much as possible to avoid regression tests. When testing a larger application, you can run all tests at night. Be sure to use the correct JUnit extension to test special applications (such as using the Castus test J2EE application). It is worth spending to now, you should clearly know how fast the unit testing uses the JUnit framework and the appropriate tool implementation unit test. About the next goal of the unit test is to make your CTO believe that you must spend more time in implementing tests. However, when you consider checking the old code, correcting errors, and publishing a debugged version (it might cost the whole day), the code error in the early stage of the development process is undoubtedly a very good. Good investment. Here is not considered when the error code is no longer located at the top of the block, the developer must follow the "Black Magic" step, including: tag code, making a branch, correcting code error, release, and incorporate code correction into blocks in. All of these steps are very time consuming, and it is easy to generate errors. To start using unit testing and junit, visit the JUnit website: www.junit.org. You will find a large number of useful documents (including detailed manuals using JUnit implementation test), a list of IDE integrated with JUnit, and details about the JUnit extension tool. Michel Casabianca (CASA@sweetohm.net) is a software engineer in IN-FUSIO (a French company that provides game service for mobile users), as well as the computing of XML Pocket Reference (O'Reilly Publishing, 2001) . Table 1: Writing the determination method used in the test example Assertequals (desired prototype, actual prototype) Check if the two prototypes are equal, assertequals (desired object, actual object) use the object's equals () method to check if the two objects are equal assertsame (desired object, actual object) check two of the same memory address. Whether an object is equal assertnotsame (desired object, actual object) Check if two objects with different memory addresses Assertnull (Object Object) Check if an object is empty AssertNotnull (Object object) Check if an object is non-empty ASSERTRUE (Boole Conditions) Check the conditions for true Assertfalse (Boolean conditions) inspection conditions are false