Category Archives: JUnit

Conditional Test Execution in JUnit 5

JUnit Jupiter allows developers to either disable or enable a test based on certain conditions programmatically. The org.junit.jupiter.api.condition package that allows developers to disable or enable containers and tests declaratively. If you want to specify details about why they might be disabled, every annotation associated with these built-in conditions has a disabledReason attribute available for that purpose. Let’s try to understand all concept… Read More »

JUnit 5 @Disabled Test Example

JUnit 5 @Disabled annotation can be used to disable the entire test class or individual test method. It accepts only one optional parameter, which indicates the reason why this test is disabled. When @Disabled annotation is applied over test class then all test methods within that class are automatically disabled, or if it is used before any… Read More »

JUnit 5 Assumptions Examples

Junit-5 org.junit.jupiter.api.Assumptions class has static methods to support conditional test execution based on assumptions. If the assumption is failed then the test will be aborted. Assumptions are typically used whenever it does not make sense to continue the execution of a given test method. JUnit Jupiter org.junit.jupiter.api.Assumptions  class has three such methods: assumeFalse(), assumeTrue() and assumingThat() Assumptions.assumeTrue() assumeTrue() … Read More »

Third-party Assertion Libraries support in JUnit 5

Assertion facilities provided by JUnit Jupiter or Junit 5 are sufficient for most testing scenarios, there are times when we need more power and additional functionality such as matchers are desired. In that case, the JUnit team recommends the use of third-party assertion libraries such as AssertJ, Truth,Hamcrest, etc. Developers are free to use the assertion library of their… Read More »

What is JUnit ?

Unit is a unit testing framework for the Java programming language that plays a big role in regression testing. We can perform unit testing by creating test cases. The unit test cases written by the developer/tester is a  source code which ensures that the program logic works as expected. Running tests automatically helps to identify the… Read More »

Assertions in JUnit 5

JUnit Jupiter comes with many of the assertion methods that JUnit 4 already has and added a few more so that it may support Java 8 lambdas. Also in this library, assertions are present for all primitive types, Objects, and arrays (either of primitives or Objects). The assertions are a collection of utility methods that support asserting… Read More »

JUnit 5 Display Names

In JUnit 5, Test classes and test methods may declare custom display names using @DisplayName — with spaces, special characters, and even emojis — that will be displayed in test reports and by test runners and IDEs. 1.The default name of test classes and methods The default name of test class and test methods are the same as a class… Read More »

JUnit 5 @BeforeEach and @AfterEach annotation Example

In this post, we will talk and learn about the JUnit 5 life cycle  @BeforeEach  and @AfterEach  annotations using a sample project. @BeforeEach Annotation This annotation used to indicate that the annotated method should be executed before each @Test method in the current class. It is the replacement of @Before annotation in JUnit 4. @AfterEach Annotation… Read More »

JUnit 5 with Gradle Dependency

In this post we will learn how to configure Gradle Dependency for JUnit 5 and how to use them to create and execute JUnit Tests To execute JUnit 5 tests using Gradle, we need minimum two dependencies. 1. JUnit Jupiter Engine Dependency junit-jupiter-engine has internally dependency on and junit-platform-engine and junit-jupiter-api so if you add junit-jupiter-engine dependency then it… Read More »

Unit 5 @AfterAll annotation example

In this post, we will talk and learn about the JUnit 5 life cycle @AfterAll annotation. In JUnit 5 @AfterAll annotation is used to signal that the annotated method should be executed after all tests in the current test class. It is a replacement for @AfterClass annotation in JUnit 4. @AfterAll  annotated method must be declared as a static method otherwise it will throw runtime… Read More »