Where Are Multiple JUnit Test Methods Typically Placed in Code?

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?

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

@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:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>src/
|- persistance/
| |- DatabaseConnection.java
|
|- entities/
|- Apple.java
|- Orange.java
</code>
<code>src/ |- persistance/ | |- DatabaseConnection.java | |- entities/ |- Apple.java |- Orange.java </code>
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):

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>test/
|- persistance/
| |- DatabaseConnectionTest.java
|
|- entities/
|- AppleTest.java
|- OrangeTest.java
</code>
<code>test/ |- persistance/ | |- DatabaseConnectionTest.java | |- entities/ |- AppleTest.java |- OrangeTest.java </code>
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:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>@Test(expected=IllegalArgumentException.class)
public void testIfExceptionIsThrown { ... }
</code>
<code>@Test(expected=IllegalArgumentException.class) public void testIfExceptionIsThrown { ... } </code>
@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).

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật