Junit 5
Junit 5
JUnit 5
@techwithvishalraj
What is JUnit?
JUnit is a widely used testing framework in Java for writing and running unit
tests. It allows developers to write test cases that verify the behavior of
individual units of code, typically methods or classes, to ensure they work as
expected.
JUnit 5 requires Java 8 (or higher) at runtime. However, you can still test code
that has been compiled with previous versions of the JDK.
JUnit Vintage provides a TestEngine for running JUnit 3 and JUnit 4 based tests
on the platform. It requires JUnit 4.12 or later to be present on the class path or
module path.
All core annotations are located in the org.junit.jupiter.api package in the junit-
jupiter-api module.
Why Use JUnit? @techwithvishalraj
Automated Testing: JUnit automates the process of running tests, so you can run all
your tests with a single command, reducing manual effort.
Early Bug Detection: By writing unit tests, you can catch bugs early in the
development cycle. Each time you make a change to your code, you can rerun the
tests to ensure that the new code doesn't break existing functionality.
Regression Testing: JUnit allows you to test your codebase continuously as it evolves.
Running your test suite regularly ensures that new features or changes do not
introduce regressions (i.e., errors in previously working functionality).
Code Quality: Writing unit tests encourages you to think about how your code is
structured, leading to better design, modular code, and fewer defects.
Integration with Build Tools: JUnit integrates seamlessly with build tools like Maven,
Gradle, and CI/CD pipelines (like Jenkins). It helps automate the testing process in
continuous integration systems.
Support for TDD: JUnit is frequently used in Test-Driven Development (TDD), where
you write the tests before writing the actual code. This process helps you define the
desired behavior before implementation, resulting in cleaner, more reliable code.
Annotations: JUnit uses annotations such as @Test to mark methods as test methods,
@Before and @After to set up and clean up before and after each test, and
@BeforeClass and @AfterClass for global setup and teardown.
Assertions: JUnit provides assertion methods that allow you to check if the actual
result matches the expected result.
1. assertEquals(expected, actual)
2. assertTrue(condition)
3. assertFalse(condition)
4. assertNotNull(object)
5. assertThrows(Class<T> expectedType, Executable executable)
Test Runners: JUnit test runners execute the test cases and report the results,
showing which tests passed, failed, or were skipped.
JUnit Jupiter supports the following annotations for
@techwithvishalraj
configuring tests and extending the framework.
Annotation Description
Denotes that a method is a test method. Unlike JUnit
4’s @Test annotation, this annotation does not declare any attributes,
@Test since test extensions in JUnit Jupiter operate based on their own
dedicated annotations. Such methods are inherited unless they are
overridden.
Denotes that a method is a parameterized test. Such methods are
@ParameterizedTest
inherited unless they are overridden.
Denotes that a method is a test template for a repeated test. Such
@RepeatedTest
methods are inherited unless they are overridden.
Denotes that a method is a test factory for dynamic tests. Such
@TestFactory
methods are inherited unless they are overridden.
Denotes that a method is a template for test cases designed to be
invoked multiple times depending on the number of invocation
@TestTemplate
contexts returned by the registered providers. Such methods are
inherited unless they are overridden.
Used to configure the test class execution order for @Nested test
@TestClassOrder
classes in the annotated test class. Such annotations are inherited.
Used to configure the test method execution order for the annotated
@TestMethodOrder test class; similar to JUnit 4’s @FixMethodOrder. Such annotations are
inherited.
Used to configure the test instance lifecycle for the annotated test
@TestInstance
class. Such annotations are inherited.
Declares a custom display name for the test class or test method. Such
@DisplayName
annotations are not inherited.
@DisplayNameGene Declares a custom display name generator for the test class. Such
ration annotations are inherited.
Denotes that the annotated method should be
executed before each @Test, @RepeatedTest, @ParameterizedTest,
@BeforeEach
or @TestFactory method in the current class; analogous to JUnit
4’s @Before. Such methods are inherited unless they are overridden.
Denotes that the annotated method should be
executed after each @Test, @RepeatedTest, @ParameterizedTest,
@AfterEach
or @TestFactory method in the current class; analogous to JUnit
4’s @After. Such methods are inherited unless they are overridden.
JUnit Jupiter supports the following annotations for
configuring tests and extending the framework. @techwithvishalraj
Annotation Description
Denotes that the annotated method should be
executed before all @Test, @RepeatedTest, @ParameterizedTest,
@BeforeAll and @TestFactory methods in the current class; analogous to JUnit
4’s @BeforeClass. Such methods are inherited unless they are overridden
and must be static unless the "per-class" test instance lifecycle is used.
Denotes that the annotated method should be
executed after all @Test, @RepeatedTest, @ParameterizedTest,
@AfterAll and @TestFactory methods in the current class; analogous to JUnit
4’s @AfterClass. Such methods are inherited unless they are overridden
and must be static unless the "per-class" test instance lifecycle is used.
Denotes that the annotated class is a non-static nested test class. On Java
8 through Java 15, @BeforeAll and @AfterAll methods cannot be used
directly in a @Nested test class unless the "per-class" test instance
@Nested lifecycle is used. Beginning with Java
16, @BeforeAll and @AfterAll methods can be declared as static in
a @Nested test class with either test instance lifecycle mode. Such
annotations are not inherited.
Used to declare tags for filtering tests, either at the class or method level;
@Tag analogous to test groups in TestNG or Categories in JUnit 4. Such
annotations are inherited at the class level but not at the method level.
Used to disable a test class or test method; analogous to JUnit
@Disabled
4’s @Ignore. Such annotations are not inherited.
Denotes that the annotated field represents a resource that will
@AutoClose
be automatically closed after test execution.
Used to fail a test, test factory, test template, or lifecycle method if its
@Timeout
execution exceeds a given duration. Such annotations are inherited.
Used to supply a temporary directory via field injection or parameter
@TempDir injection in a lifecycle method or test method; located in
the org.junit.jupiter.api.io package. Such fields are inherited.
@ExtendWith Used to register extensions declaratively. Such annotations are inherited.
Used to register extensions programmatically via fields. Such fields are
@RegisterExtension
inherited.
@techwithvishalraj
Explore more about Junit and its feature on its Official Website.
https://junit.org/junit5/docs/snapshot/user-guide/
Thank
you!
vishal-bramhankar
techwithvishalraj
Vishall0317