Unit Tests

You can create and run unit tests using JUnit in Studio. On the surface, this may appear the same as the plain Eclipse-based process, but One Network Studio adds some enhancements to speed the development of unit tests. Namely, you can create your test cases in classes in a particular location in the source tree. There is no need to create additional classes such as a test runner. The platform generates those for you automatically via an Ant script. To reap these benefits, you need to follow a number of conventions:

  • All unit test classes should be placed in the test-src folder within your application directory

images/download/attachments/144836438/montest_12-version-1-modificationdate-1645139581000-api-v2.png

  • The class name must end with "UnitTest". For example if I were creating a class called BookstorePurchase, its unit test's class name would be BookstorePurchaseUnitTest. The Ant tasks that generate the task runners and other JUnit artifacts look for UnitTest in the class name as the basis for their code generation. If you neglect to put UnitTest in the class name, the test will never run.

  • The test class must not contain any dependencies on EJBs or the database. Classes with this requirement must be implemented as Server Tests which allow for EJBs and database access and are run against a running server. Simple unit tests should not require a running server.

  • Your test class should extend com.onenetwork.platform.tools.test.AbstractUnitTest rather than the usual JUnit classes.

  • All your test methods in the unit test class should start with the word test—all lower case.

If you follow these guidelines, the creation of unit tests will simply be a matter of writing the actual test code needed to implement the tests. The usual boiler-plate code for creating and running JUnit tests is not needed. All the usual JUnit features are there. For example you can override the usual setUp and tearDown methods with your own.

Let's take a look at a sample unit test.

import com.onenetwork.platform.tools.test.AbstractUnitTest; public class MyUnitTest extends AbstractUnitTest { public void testMyTest() { fail("implement your test code in place of this fail statement."); } }

Note the conventions we followed. The class name ends with "Unit Test". The test method begins with the word "test". The class itself extends AbstractUnitTest. Follow these rules and the SDK will make unit tests a breeze to create and run.

For more information on JUnit, please see the JUnit documentation at http://www.junit.org.