On this page:
How to test
Examples
Testing
Kinds of tests
Where do tests go in my project?
7.7

Testing

Testing Example Video

How to test

Writing tests is an essential part of design and implementation. The most important skill in writing tests is to determine what to test, and then determine how to test.

Examples

Imagine you have an interface IFunctionality and you must provide its implementation ConcreteFunctionality. You must check that your ConcreteFunctionality implementation of the IFunctionality interface works as specified. However thinking about tests after you have completed the implementation is not ideal. Since you have already written your implementation, you will likely come up with tests that you already know will pass, rather than tests that should pass. Here are some recommendations on how to come up with effective test cases:

Writing tests before writing your implementation will give you insight into what your implementation ought to do. Moreover, it will help you work through the types and visibilities of your interfaces, classes, and methods, and often just understanding that structure is a big help in understanding the problem.

Testing

Obviously, unfortunately, you often can’t write a complete set of tests for your code before you’ve started writing your code, as the process of implementing a design can easily bring issues to the forefront that you didn’t notice or anticipate. Proper testing is an iterative process: starting from initial examples you create an initial implementation, which might suggest additional tests, which might cause test failures, which you need to fix, which might suggest additional tests, etc. A successful set of test cases is one that tests whether your implementation adheres to your design, whether your design leaves loopholes and ambiguities that allow its incorrect usage, and whether the behavior of your implementation can be predicted in all situations. This set of test cases should compile, and upon running, should pass.

NOTE: It is far better to include tests that you know to fail, rather than comment them out or delete them. Leave a // FIXME comment next to the failing tests, explaining what you intended the test to check for, and why you think it’s currently failing. At some point you clearly had a reason for writing the test case, and it would be a shame to lose that insight by deleting the test! Equally bad is commenting the test out, since it gives the misleading impression that everything is fine and all tests pass, when there are known problems remaining...

Kinds of tests

There are many kinds of tests you may wish to write:

Where do tests go in my project?

Java’s package mechanisms allow for careful compartmentalizing of your code, to ensure that it neither relies upon nor modifies anything it should not have access to. This applies equally to your tests. Tests belong in a separate test/ directory, whose subdirectories parallel the structure of your src/ directory. Where specific tests belong depends on their intent: