Introduction

Many people like to do "unit testing" on classes, i.e. usually loosely structured and easy-to-use testing of one class' functionality at a time. This way you can test your application in small pieces and then try to assemble them. The problem is that:
  1. To be really effective, test code must often be "white-box testing", i.e. it must be able to make use of private features and hooks which are not part of any public API, thus only accessible via package-private or even class-private members in the implementing class.
  2. But such test code is not appropriate for released applications, since it is only designed for use within the development environment and would otherwise bloat the deployed application with unnecessary and misleading code.
This module attempts to address both these requirements at once by making use of a clever strategy (thanks to Skip Hovsmith for the idea!): write your code as usual, using whatever private members you wish; and place all of your test code into a public static inner class named e.g. TEST. This inner class will be able to access anything in the package, including private data within the outer class; but the outer class has no knowledge of it. If you provide a main method for the inner testing class, then it can be executed to perform unit testing; but before deployment, the testing classfiles are simply deleted and never appear in the deployed application.

This strategy is implemented in this module by means of a special compiler type and a special executor which cooperate to support inner test classes.

Information, bug reports, comments, and requests: please see innertesters.netbeans.org.

Compilation

The special trick for compiling these classes is that first a regular Java compiler is run as usual; it generated all classfiles, including the one for the inner testing class. But then the inner testing classfile is removed from the source directory and placed into a special scratch directory (temporary file area), which need not be mounted in Filesystems.

The module installs a new category of compiler type called Inner Tester Compiler. Refer to the User's Guide for configuring and using custom compiler types in general. This one has three special properties:

  1. Inner Class Name (e.g. TEST): name of the inner class to assume is implementing the testing.
  2. Test Directory (e.g. c:\temp\innertst): a scratch directory where inner test classfiles may be kept.
  3. Main Compiler (no default value): the regular Java compiler which will initially compile the source. Important: you must set this to some value (e.g. External Compilation, Internal Compilation, or FastJavac Compilation) in order for the whole compiler to work!
When the main compiler is correctly configured and this special compiler is associated with a source, the inner test classfile will automatically be moved to the designated directory during compilation and building, and removed from it during cleaning.

Note: normally you will not have any reason to mount the testing directory in Filesystems. However, if some of your test code refers to test code in other classes, in a complex system, you may need to mount it just so that the compiler can find the cross-references. This is because other inner test classfiles are not available by default in the main compiler's classpath, unless this directory is in Filesystems. If you mount it, you will probably want to mark it hidden using the customizer or using Filesystems Settings.

Execution

You should again refer to the User's Guide for details on how to configure custom executors in general. In the case of the Inner Tester Executor, there are just two properties in addition to those usually used by external execution: Inner Class Name and Test Directory. These should be configured to be the same as for the compiler, above.

The special executor does two things differently from normal external execution:

  1. It includes the testing directory in the classpath to the Java launcher, so that the testing classfiles can be used.
  2. It runs the main method from the testing class, rather than the outer class, when you hit Execute on the source file.

Template

The module also includes one template, in the Classes folder, called Unit-testable Class. This is simply a demo of how to use the module. It includes an inner testing class which tests some private functionality of the outer class. Objects created from the template will have the correct compiler and executor preset on them; you can create your own such templates in a similar fashion if you prefer.

Remember that you must configure the Main Compiler for the compiler type before trying to compile objects created from this template!