SimpleTest: Making the Tools Work Together


Assuming we have the JDK, Ant, and JUnit set up correctly, we should be able to now write a sample JUnit Test and try it out.

Regardless of which flavor of JUnit we use, we can either pass it our test class name or type it into the UI runner. For example, if we wanted to write a very simple test case to test the fact that 2 + 3 = 5, we would do the following:

  • Develop a JUnit test classfor example, SimpleTest.java.

  • Run the JUnit class using one of the JUnit runners.

SimpleTest.java

This book's code file (available on the book's website) shows the complete code for SimpleTest.java. The code should be fairly straightforward to follow. There are two test methods: testAddSuccess and testAddFail, as shown here:

public void testAddSuccess() {     assertTrue(value1 + value2 == expectedResult); } public void testAddFail() {     assertTrue(value1 - value2 == expectedResult); }


The testAddSuccess method will be successful, whereas the testAddFail method will fail (because 2 minus 3 does not equal 5). The success or failure is determined by the JUnit assert methods, which throw an exception if the test failed.

JUnit Assert Methods

We saw an example of JUnit's assertTrue method in our example previously; JUnit also provides several other flavors of assert methods, as shown next:

  • assertEquals

  • assertFalse

  • assertNotNull

  • assertNotSame

  • assertNull

  • assertSame

  • assertTrue

Running SimpleTest (a Single JUnit TestCase)

To try out the code we saw for SimpleTest, we need to create the SimpleTest.java file, compile it, and try running it. So, let's create the SimpleTest.java file under our src/java/com/visualpatterns/timex/test/ directory. Then we can simply type the ant command from the timex/ directory to compile our unit test source code.

Now, let's try running our SimpleTest test case (from the top-level timex/ directory) using the JUnit test runner, as demonstrated here:

C:\anil\rapidjava\timex>java  -cp \junit3.8.1\junit.jar;build/timex/WEB-INF/classes  junit.textui. TestRunner com.visualpatterns.timex.test. SimpleTest


We should see something similar to what is shown in Figure 4.7.

Figure 4.7. Running SimpleTest in the text runner.


We could also run SimpleTest.class in the JUnit swing runner, as shown next (Figure 4.8 shows the result of this command):

java -cp \junit3.8.1\junit.jar;build/timex/WEB-INF/classes   junit.swingui. TestRunner com.visualpatterns.timex.test. SimpleTest


Figure 4.8. Running SimpleTest in the Swing-based JUnit runner.


The junit.framework. AssertionFailedError related messages you see in both the test runners is actually a good thing; these are valid JUnit errors because our testAddFail method failed.

Running JUnit Tests in a Batch

There is one more way we can run JUnitthat is, as an Ant task. Next we'll take a look at how we do that.

First, let's copy the junit.jar file from the JUnit install directory to the <ant-home>/lib directory; for example, on Microsoft Windows you would type something like copy \junit3.8.1\junit.jar \apache-ant-1.6.5\lib. This enables us to use the junit Ant task.

Second, we need to copy the same junit.jar to our timex/lib directory; this also will assist with our builds using Ant.

Now let's revisit our build.xml file. This file contains a target named test, which uses the junit task, as shown in the following excerpt:

<target name="test" depends="compile">     <junit printsummary="true" showoutput="yes" filtertrace="false">          <classpath ref/>       <batchtest fork="yes">                 <formatter type="plain"/>         <fileset dir="${class.dir}">


If we run the command ant test, the results of the batch test would appear in a file named TEST-com.visualpatterns.timex.test. SimpleTest.txt in the current directory, an excerpt of which is shown here:

Testsuite: com.visualpatterns.timex.test. SimpleTest Tests run: 2, Failures: 1, Errors: 0, Time elapsed: 0.021 sec Testcase: testAddSuccess took 0.004 sec Testcase: testAddFail took 0.003 sec FAILED


That's pretty much all there is to JUnit! Although JUnit is a simple framework, it is powerful because you can have several test methods within each JUnit TestCase subclass (a suite). Furthermore, you can roll up the individual suites within other suites (with no limit). For example, you can create a class named AllTests, which calls the suites of all other Test classes in the test package.



Agile Java Development with Spring, Hibernate and Eclipse
Agile Java Development with Spring, Hibernate and Eclipse
ISBN: 0672328968
EAN: 2147483647
Year: 2006
Pages: 219

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net