JUnit and Mockito

 JUnit and Mockito are popular frameworks used for testing Java applications. 

 Junit is the unit testing framework in java created by Eric Gamma and Kent Beck. It allows programmers to write unit tests for methods to verify that they meet the required functionality. It is one of the most widely used testing frameworks, providing annotations to identify methods that specify a test, setup, teardown, etc. It integrates well with most build tools and IDEs. (API is org.junit)

Explanation:

  • JUnit Platform: This is the foundation on which testing frameworks, including JUnit Jupiter and others, are built. The JUnit Platform provides a launcher API for discovering and executing tests, which can be used by IDEs, build tools, and other testing frameworks.

  • Engine: Within the JUnit Platform, a test engine is a component that is responsible for discovering and executing tests for a specific testing framework. For example, JUnit Jupiter is the test engine for JUnit 5.

Key Points:

  1. Discovery: The test engine discovers test cases and test containers within the code base.
  2. Execution: The test engine executes the discovered tests and reports the results.
  3. Organization: The test engine organizes tests into a hierarchy, such as test classes and methods, to provide a structured way of running and reporting on tests.

Here's a more detailed breakdown:

JUnit Platform Components:

  • JUnit Platform Launcher: This component is used to discover and execute tests. It is the entry point for running tests and reporting results.
  • Test Engine: Each testing framework (like JUnit Jupiter, JUnit Vintage, or third-party frameworks) implements its own test engine by implementing the TestEngine interface.
  • Test Discovery: Test engines discover tests using various mechanisms (annotations, naming conventions, etc.).
  • Test Execution: Test engines execute tests and notify the launcher about test progress and results.

Features

  1. Fixtures: It is a set of objects with fixed state and used as a baseline for running the tests.  eg:@BeforeEach, @AfterEach
  2. Test suites: Bundles a set of test cases and runs them together. eg: @Suite, @RunWith
  3. Test Runner: It orchestrates the execution of tests and reports their results. It is responsible for running test cases, collecting the results, and presenting them to the user. 
  4. Junit Classes: Classes used to write the test cases and test them.
  5. Annotations: Simplify the identification and execution of test methods.
    1. @Test: Marks a method as a test method.
    2. @BeforeEach: Executed before each test method.
    3. @AfterEach: Executed after each test method.
    4. @BeforeAll: Executed once before all test methods in the class.
    5. @AfterAll: Executed once after all test methods in the class.
  6. Assertions: Methods to check expected results (assertEquals, assertTrue, assertNotNull, etc.).
  7. Parameterized Tests: Allow running a test with different sets of parameters.

Mockito

Mockito is a mocking framework for unit tests in Java. It allows you to create and configure mock objects. Using Mockito simplifies the development of tests for classes with external dependencies.

Key Features:

  • Mock Creation: Create mock instances of classes and interfaces.
  • Behavior Verification: Verify that certain methods were called on the mock objects.
  • Stubbing: Specify return values for method calls on mock objects.
  • Argument Capturing: Capture the arguments passed to method calls on mock objects for further assertions.

JUnit 5

  • Programming Models: JUnit 5 supports three distinct programming models for writing tests:

  1. JUnit Jupiter: This is the combination of programming model and extension for writing testsand extensions (like @Test, @BeforeEach, @AfterEach, etc.). It provides a launcher API for discovering and executing test, which can be used by IDEs , build tools and other testing frameworks. 
  2. JUnit Vintage: This provides engine to run tests written in JUnit 3 and JUnit 4 alongside JUnit 5.
  3. JUnit Platform: This provides the foundation for launching testing framework on JVM. It conatins test engine which is responsible for discovering, executing, and organizing tests.
  • improved Architecture: JUnit 5 is designed with a more modular architecture, allowing developers to use only the parts of the framework that are needed. This modular design makes it easier to extend JUnit with custom features or integrate it with other tools and frameworks.

  • Annotations: JUnit 5 introduces new annotations for writing tests:

    • @Test: Marks a method as a test method.
    • @BeforeEach and @AfterEach: These are used to specify methods that should be run before and after each test method.
    • @BeforeAll and @AfterAll: These are used to specify methods that should be run once before all test methods and once after all test methods in the current class.
    • @DisplayName: Specifies a custom display name for a test class or method.
    • @Nested: Allows nesting tests within one another to create a hierarchy.
  • Assertions: JUnit 5 includes a new set of assert methods in the Assertions class, providing more options and flexibility for making assertions in tests. Examples include assertEquals, assertNotNull, assertTrue, assertThrows, and more.

  • Parameterized Tests: JUnit 5 supports parameterized tests with the @ParameterizedTest annotation, allowing developers to run the same test with different sets of parameters.

  • Extension Model: JUnit 5 introduces a powerful extension model that replaces the old @RunWith and @Rule mechanisms from JUnit 4. Extensions can be used to add new behavior to tests, such as custom test instance lifecycle callbacks, dependency injection, and more.

  • Dynamic Tests: JUnit 5 supports dynamic tests, which are generated at runtime by a factory method annotated with @TestFactory.

  • JUnit Platform: The JUnit Platform provides a Console Launcher to launch the JUnit Platform from the command line, build tools, and IDEs. It also supports running tests on different JVM languages and environments.

  • JUnit 5 is designed to be backward-compatible with JUnit 4, allowing gradual adoption and migration of existing tests. It is widely used in the Java community for writing unit tests due to its enhanced features, improved architecture, and flexibility.

In JUnit 4, @RunWith and @Rule are annotations used to customize the way tests are run and to manage resources and behaviors for the tests.

The @RunWith annotation is used to specify a custom test runner. A test runner is responsible for running test methods in a test class and reporting the results. By default, JUnit uses the BlockJUnit4ClassRunner, but you can use other runners to customize the test execution.

Common Uses

  • Parameterized Tests: To run the same test with different data.
  • MockitoJUnitRunner: For using Mockito annotations.
  • SpringJUnit4ClassRunner: For integration with Spring TestContext framework.
 The @Rule annotation provides a way to add or modify the behavior of tests. Rules can be used to:
  • Manage external resources (e.g., files, sockets).
  • Change the behavior of each test method (e.g., setting up or tearing down resources).

Types of Rules

  • MethodRule: Applies to individual test methods.
  • TestRule: Applies to the entire test class.
In JUnit 5, @RunWith is replaced by @ExtendWith, and @Rule is replaced by extensions, providing more flexible and powerful ways to extend and manage tests.

More: https://www.vogella.com/tutorials/JUnit/article.html

Comments

Popular posts from this blog

Transform values with a stream

Collections Framework

Inspect a collection