Home > Java > javaTutorial > Test-driven development using JUnit5 and Mockito

Test-driven development using JUnit5 and Mockito

王林
Release: 2023-08-19 23:57:07
forward
1488 people have browsed it

Test-driven development using JUnit5 and Mockito

Test-driven development (TDD) is a software development method in which tests are written before the actual code. TDD has gained considerable attention due to its emphasis on code quality and maintainability. This article explores how to implement TDD effectively using JUnit5 and Mockito, two powerful frameworks in the Java ecosystem.

What is test-driven development?

Test-Driven Development (Test-Driven Development) is an iterative development process. Developers first write test cases for new functions or features, then write the minimum amount of code to pass the test, and finally rewrite the code. Structure to optimize. This approach enhances the design, reduces errors, and improves the overall maintainability of the code.

Understanding JUnit5 and Mockito

JUnit5 is the latest version of JUnit, a widely used unit testing framework in Java. It introduces several new features and improvements that increase the ease and flexibility of testing in Java.

Mockito, on the other hand, is a mocking framework for creating mock objects and defining their behavior, which is useful for testing code with external dependencies.

Test-driven development using JUnit5 and Mockito

Step 1: Write a failing test

The first step in TDD is to write a test that fails. For this purpose, in JUnit5 we define a method annotated with @Test -

import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;

class ExampleTest {

   @Test
   void shouldReturnTrueWhenNumberIsEven() {
       Example example = new Example();
       boolean result = example.isNumberEven(3);
       assertTrue(result);
   }
}

Copy after login

This test will fail initially because we have not implemented the isNumberEven method in the Example class.

After a failed test, we write just enough code to pass the test. For example -

class Example {

   boolean isNumberEven(int num) {
       return num % 2 == 0;
   }
}
Copy after login

Run the test again, we should see that the test passes because the isNumberEven method now correctly checks if a number is even.

Step 3: Refactor the code

The last step involves refining and optimizing the code without changing its behavior. Although our example is very simple and may not require refactoring, in more complex cases this step may involve reducing redundancy, improving readability, or optimizing performance.

The Chinese translation of

Mocking with Mockito

is:

Use Mockito for simulation testing

Use Mockito to create mock objects when the method that needs to be tested involves external dependencies. This allows methods to be isolated for testing −

import org.junit.jupiter.api.Test;
import org.mockito.Mockito;

import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.*;

class ExampleTest {

   @Test
   void shouldCallDependency() {
       // Arrange
       Dependency dependency = Mockito.mock(Dependency.class);
       Example example = new Example(dependency);

       // Act
       example.performAction();
		
       // Assert
       verify(dependency, times(1)).action();
   }
}
Copy after login

In this test, we create a mocked dependency object and verify that when we call the performAction method on the Example object, its action method is called once.

in conclusion

Test-Driven Development (Test-Driven Development), combined with JUnit5 and Mockito, provides a solid foundation for writing high-quality, maintainable code. By understanding the TDD cycle and using these frameworks effectively, developers can significantly improve their testing skills and code quality.

The above is the detailed content of Test-driven development using JUnit5 and Mockito. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template