Overview

Unit testing is a procedure used to validate that individual units of source code are working properly. Unit testing is a type of white box testing. The procedure that are to be validated would usually be methods on classes. In project.net there are over 600 unit tests written.

Unit testing framework in project.net

Project.net uses JUnit unit testing framework as the library to write automated unit tests.

Writing a unit test

Here is a small example on how to write a unit test:
Say a class net.project.schedule.calc.AssignmentAdder is to be tested.
You would add a java class AssignmentAdderTest in the same package under the java source folder <pnet-root>/core/src.

public class AssignmentAdderTest extends TestCase {

}

note: The test class name has to be <class name to be tested>Test.
Under class you can add test methods as test<some name>.
note: For more details refer to any existing test class file.

Testing framework directory structure

1. The test class files would go under core/src folder under the same package as the class to be tested.
2. The mock objects written would go under test/unit-test/mockobject/src
3. The test suits (that groups all the test classes together) and any other such classes that should not be part of core build would go under test/unit-test/src

Executing unit tests in project.net

This section would explain what all you have to do to setup and execute unit tests, in various ways. This is a one time task.

Setup

There is a property file under test/unit-test called mockobjects.properties.example. This file should be copied to a file with name mockobjects.properties, and property values should be modified as per your local settings.
Example:

[SessionManager]
# for a root deployment this would be / and for a non root deployment it would be say /pnet
JSPRootURL=/
# would be localhost in almost all cases as tests are executed locally
SiteHost=localhost
#db user name
username=pnet
#db user password
password=pnet
#db connect uri
connectString=jdbc:oracle:thin:@localhost:1521:sachin
vendor=oracle
[Test]
#path to xsl files
xslRootPath=c:\\apache-tomcat-5.5.20\\webapps\\ROOT\\WEB-INF\\classes\\config\\xsl
#path to properties
filePath=c:\\apache-tomcat-5.5.20\\webapps\\ROOT\\WEB-INF\\classes\\config\\etc
#path to where some additional testing resources reside
testFilePath=e:\\trunk\\test\\unit-test\\resources
#path to the directory where all you test cases reside
sourceFilePath=e:\\trunk\\core\\src

Further:
In the java file net.project.test.util.TestProperties under folder test/unit-test/mockobject/src you would notice that it picks up the location for mockobjects.properties from a system variable mockobject.properties.location. This variable is set while running the test cases from ant targets, but if you want to run a single test case of a test suite from command prompt or eclipse you would have to set this property before. Alternately you can modify the path to the mockobjects.properties in the java file itself. How to do this is well commented in that file.

Running single test case or complete test suite via eclipse

This is best for the developer developing the test case or running the test suite before any major changes or checkin. If you want to test a single class, say a newly created class file, go to Run --> Run As --> JUnit Test. Clicking that link from the menu bar would run the single test case.

To run the complete test suite go to the java class file AllTests and again click the same link from the menu bar. This would dynamically add all the test case files in a test suite and run it.
note: if you want to exclude a test class file you can do that in AllTests. Currently CSTaskEndpointCalculationTest tests are excluded. This should be used for temporary exclusion only.

Running single test case or complete test suite via build target at build.xml

These are the old unit test targets when project.net used to support only weblogic. The build.xml is the build script for that purpose and these are the following targets that is of interest:

* unit-test - Run unit test after compile. PLAIN format
* unit-test-xml - Run unit test after compile. XML format
* run-single-unit-test - Run single unit test specified as -Dunittest.to.run=net.project.schedule.TaskEndpointCalculationTest. PLAIN format

note: unit-test and unit-test-xml would run all the test cases under the core/src directory, irrespective of some test classes excluded from the test suite or not in the file AllTests.

note: refer to the build file for more details on testing targets

Running complete test suite via build target at build-tomcat.xml

This target is best for QA and should be preferred in place of above as all the new development has been moved on to tomcat.

In the build file build-tomcat.xml a target run-unit-tests is added for running the complete test suite. This target simply executes the AllTests java class file that contains all the test cases to run in a suite.

What to test using unit testing

Functionality that involves some logic written in java files, a unit test case for that should be written. If change involves just UI design say in JSP or in java handler classes then there is no need to write unit test case. For capturing UI flow we are writing acceptance test case using httpunit. refer: Acceptance Testing

Unit test case are very essential and has to be written by the developer writing the code (no one else is going to write your test cases), and as we follow TDD and agile methodology, test cases should be written before writing the code.

Miscellaneous

At the time of writing this note we have just one test case failing which we would be addressing soon. When all test cases pass, every developer should make a point to run all the unit test cases before doing any checkin. In case you get a red signal, checkin should be avoided until bar is made green again.
note: running all test cases takes less than a minute so it can be run before every checkin.
This way we would ensure clean and bug free code in the system.