From the course: Complete Guide To Java Testing with JUnit 5 & Mockito

Unlock the full course today

Join today to access over 24,700 courses taught by industry experts.

Conditional test execution based on Java version and OS

Conditional test execution based on Java version and OS

From the course: Complete Guide To Java Testing with JUnit 5 & Mockito

Conditional test execution based on Java version and OS

- [Instructor] Another feature JUnit offers is conditional test execution, which allows tests to be executed selectively based on specific criteria. This allows developers to tailor their testing strategies to particular scenarios, making the testing process more efficient and targeted. Let's take a look at an example. Here we have a class that retrieves the system's information. We can use JUnit annotations to enable certain tests for certain operating systems and versions of Java. In the first test, we'll only want to enable it on Windows. We can do this with the EnabledOnOs annotation. We'll select OS.WINDOWS. This test is only valid if the operating system is Windows, so with this annotation, it'll automatically be disabled for any non-Windows OS. Then for the second test, we'll only enable it if the machine is running Linux. We'll select OS.LINUX. For the third test, we'll enable it if we're running Java 11. We'll do this with the EnabledOnJre annotation. We'll select…

Contents