IDE Tutorials CSE 219 Fall 2005


  TOC: AkA Tactical Operations Center
Table of Contents: NetBeans Eclipse
Basics Tutorial NetBeans Eclipse
Debugging Tutorial NetBeans Eclipse
JUnit Testing Tutorial NetBeans Eclipse
CVS Tutorial NetBeans Eclipse
JAR Files Tutorial NetBeans Eclipse
CSE 219 HomePage

JUnit Testing With Netbeans

The basic premise behind JUnit testing is to be able to test each component of our program independently from the rest of our program.

In the design phase of our project we specified what every method is supposed to do and have also come up with a set of tests to make
sure that our method is functioning properly.JUnit provides us with a frame work to take each one of the methods and perform individual
tests to ensure we are getting proper values.

http://junit.sourceforge.net/javadoc/index.html contains the API for the JUnit framework, the specification for Assert, JUnit, TestCase and many more classes can be found there.


http://www.netbeans.org/

  The Opening
This is our familiar netbeans environment. We will start the tutorial here by opening up a project we wish to write a test suite for.




  Adding Tests
The next step is to create our tests. By default they are going to be created in the test folder that you see above. To create the tests we goto Tools, JUnit Tests, Create Tests.




  Test Creation
We are now presented with a Create Tests window. You will notice several check boxes. If the methods that you are trying to test are not static meaning that they need an instance of the object to be called then you will probably want to specify a setUp method for the test case. This will be where you can instantiate your object before running the tests. Also if your tests allocate any special resources you may want to select tearDown so you can properly dispose of the resources. After we are done with this you may click the next button.




  Test Creation 2
An alternate way to create tests for individual classes is to right click on the file that you wish to create a test for then goto JUnit Tests and then Create Tests. This will allow you to create a single test for a particular class.




  Blank Tests?
At this point we now have our new test file opened in our editor. you look in the resource browser you will see a new file with "Test" at the end. This is the source file for our tests for that particular class. Since we are testing for a reason lets fill in some test cases. Note that netbeans does not allow you to select the methods that you wish to test so you will have to write the test functions in by hand. To do this you write the name of the function you wish to test with test added infront of it.




  The Difficult Job Of making Tests
The JUnit framework provides us with several ways to judge if a test has succeeded or failed. Notice that netbeans does not extend TestCase instead we must specify Assert before each of the assert types.
  • assertEquals: This provides a series of overloads that allows you to test if an actual value matches the expected one.
  • assertFalse: Use this if you know the function will always return false (fails if it receives true)
  • assertNotNull: If your method return null in the event of failure use this to check to see if it succeeds
  • assertNotSame: If your method is supposed to return an element from a list you can use this to check if the element returned is the one from the actual list
  • assertNull: If your method return null in the event of failure use this to check to see if it fails
  • fail: Will fail the test, use this in conjunction with conditionals
  • failNotEquals: Essentially the same as assertEquals but will fail the test if they arent equal instead of causing an error
  • failNotSame: Essentially the same as assertNotSame except instead of causing an error it will cause a failure




  Time to take the Test
We now want to run the test that we just created to do this we have several options. We can either execute test suite that was created for us, or we can select the file we wish to test. In this case EncrypDecrypt.java and then goto the Run menu and select Run Other, Test EncryptDecrypt.java.




  The Results are Back: Everyone Passed
You will notice in your output screen at the bottom that it will show which test suite was run, the number of tests run, and the number of failures and errors.





Happy Testing



(c) David Quigley