0

I've just read the Vogella JUnit tutorial and found it very helpful in understanding how to use JUnit. However, I'm a bit confused about what the convention is for placing multiple test methods in code. The tutorial only places one test method in a class, then describes how you can use a test suite to group multiple test classes together.

Does this mean that it's common practice for each test class to only have one test method and then test suites are used to chain them together? Or was that just unintended and instead common practice is to put multiple test methods in a class?

2 Answers 2

5

It is absolutely common practice to have multiple test methods in a class.

JUnit methods are usually invoked via reflection by collecting all methods in a class (or, as you say, in an entire suite), not by a human application programmer choosing methods from a well-defined API. Therefore, there is no point in keeping each individual test class short or readable; people aren't supposed to understand a class that holds unit tests as a cohesive unit in the same the way that a class of production code should be cohesive. Having one class per test just puts unnecessary strain on your file system (dealing with thousands of files in a directory is often non-linearly expensive).

1
  • 1
    It is important to have one test test one thing. A test that tests five things becomes difficult to determine the 'shape' of a regression bug (it fails at the first one). Having five tests test one thing each and having two of the five fail is much easier to determine where something went wrong.
    – user40980
    Nov 1, 2013 at 14:49
3

@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).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.