@KilianFoth is right regarding putting multiple test methods in one test class. However, I think in some cases it can be important to run single test classes or even single test methods. That's what IDEs are for, to simplify this approach. For instance, if a test invokes a computationally intensive routine, that takes a few minutes to finish, you might want to skip this most of the time.
Structuring your tests
Every class in your source folder gets an according unit test class in the test folder.
Let the following directory tree be your source folder:
src/
|- persistance/
| |- DatabaseConnection.java
|
|- entities/
|- Apple.java
|- Orange.java
Then the folder for unit tests should have the same structure (like when applying the factory method pattern):
test/
|- persistance/
| |- DatabaseConnectionTest.java
|
|- entities/
|- AppleTest.java
|- OrangeTest.java
For every method of a class in src
you write at least two tests in the according unit test class - one positive and one negative test. These tests can be quite simple as testing the behaviour of a method that accepts one parameter of type integer and a string is passed. In this case you could tell the test to expect a certain exception, e.g., IllegalArgumentException
:
@Test(expected=IllegalArgumentException.class)
public void testIfExceptionIsThrown { ... }
You can also test more complex procedures (e.g., test the output of a black box algorithm against a log file).