To continue the series about JUnit 5 tutorial, we will get to know about JUnit 5 Assumptions. During the development, there are some situations that the source code is not written well, dependent on some conditions temporarily and this might cause several tests to fail. For example, a Backup file function should work on any operating systems but currently, it can work only on Windows. To allow the developer to run tests the source code like the above function, JUnit 5 provides us its assumptions feature.
1. Preparation
You need to get JUnit 5 setup on your environment. Below are some options for your references:
- Setting up JUnit 5 with Maven Example
- Setting up JUnit 5 with Gradle Example
- Introduction To JUnit 5 – Setting up environment with Eclipse, IntelliJ
The sample code presented in this tutorial is available on my Github project. It’s a both Maven and Gradle projects, so it’s easy to run or imported into any IDE.
2. JUnit 5 Assumptions
Let’s assume we have a class ScheduleService.java which is responsible for scheduling meetings and backup the calendar to file system.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
public class ScheduleService { public boolean doSchedule() { //TODO: put logic here return true; } public boolean backupCalendar() { // TODO: put logic here return true; } } |
At the time of writing this class, the doSchedule can work with the US/Moutain timezone and US locale while it should work with any locale and timezone. And the backupCalendar method can work with Windows file system while it should work on any operating system.
We will try to write some tests using JUnit 5 assumption features as below.
We will write tests for the method doSchedule first.
2.1. Using packages of JUnit 5
1 2 |
import static org.junit.jupiter.api.Assertions.assertTrue; import static org.junit.jupiter.api.Assumptions.*; |
2.2. Assume the current timezone is US/Mountain
1 2 3 4 5 6 7 8 9 10 11 12 |
@Test public void doScheduleSingleTimeZone() { TimeZone tzone = TimeZone.getDefault(); // Assume that the timezone is US/Mountain assumeTrue(tzone.getDisplayName().equals("US/Mountain")); // Test doSchedule method ScheduleService scheduleService = new ScheduleService(); assertTrue(scheduleService.doSchedule()); } |
2.3. Assume the current locale is the US
1 2 3 4 5 6 7 8 9 10 11 12 |
@Test public void doScheduleLocaleNonUS() { // Assume that the current locale is US Locale currentLocale = Locale.getDefault(); assumeTrue(currentLocale.equals(Locale.US)); // Test doSchedule method ScheduleService scheduleService = new ScheduleService(); assertTrue(scheduleService.doSchedule()); } |
2.4. Assume the current OS is Windows
1 2 3 4 5 6 7 8 9 10 |
@Test public void backupCalendarWindows() { assumeTrue(System.getProperty("os.name").startsWith("Windows")); // Test doSchedule method ScheduleService scheduleService = new ScheduleService(); assertTrue(scheduleService.backupCalendar()); } |
3. Output of Example
Here is the output on my Eclipse:

JUnit 5 Assumptions – Output
Because my OS is Windows, timezone is not US/Mountain, and locale is the US, the test “doScheduleSingleTimeZone” was skipped.
4. Conclusions
We have learned about JUnit 5 assumptions with several basic examples. Note that beside assumeTrue annotation, JUnit 5 provide use more assumption commands like assumeTrue, assumeThat, etc. Besides, those commands can be used with Java 8 Lambda Expression. In next posts, I’d like to share more about JUnit 5 features like Tag, Filter, etc. Recently, I have some posts related to JUnit 5. If you’re interested in, you can refer to the following links:
JUnit 5 Disable or Ignore A Test
JUnit 5 Test Suite – Aggregating Tests In Suites
JUnit 5 Dynamic Tests – Generate Tests at Run-time
Thanks for update.
Is mocking objects and JUnit5 assumption same.
No, they are not. If assumptions are false, then all the tests code below the assumptions will be skipped. This is not true with the mocking object.