![]() |
JUnit is one of the popular unit testing frameworks for Java projects. JUnit 5 is the latest version provided by the JUnit team launched with the aim of supporting the latest Java features and also providing notable advanced features to its predecessor JUnit 4. In this article, we will discuss one of the features called Conditional Test Execution in JUnit 5.
Conditional Test ExecutionConditional Test Execution is the ability to enable or disable the tests programmatically.
If a test method is disabled via any conditional-based annotation, that does not prevent the test class from being instantiated. When a test method or class contains multiple test conditions, if any of those conditions short-circuit results in disabling the test, then the test method or container will be disabled immediately without checking the remaining conditions similar to short-circuiting works in programmatic conditions.
Different Conditional Execution TypesSo far, we have seen we can enable or disable tests based on different conditions programmatically, let’s see how we can use the conditions, the prebuilt conditions provided by JUnit 5 and how to use those in our testing. Operating System and Architecture Conditions:
Java Runtime Environment Conditions:
System Property Conditions:
Environment Variable Conditions:
Native Image Conditions:
Custom Conditions:
Example of JUnit 5In this below example, we will use some of the use cases of the annotations that we have just discussed above.
Java
Java
Output:In the below image, we can see that some tests are being disabled for not passing the JUnit test conditional annotations. ![]() final output ConclusionWith that being said, we have seen how we can use the JUnit 5 conditional test execution feature to enable or disable the tests and containers based on our need programmatically with the help of the annotations provided by the JUnit 5 framework and also seen an code example of the implementation of it. |
Reffered: https://www.geeksforgeeks.org
Advance Java |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 12 |