JUNIT / NOTES</>↗JUnit13 Jul 2026
To use @Mock and @InjectMocks with JUnit, you typically use them with Mockito. @Mock creates a fake/mock dependency. @InjectMocks creates the class under test and injects the mocks into it. With JUnit 5, you enable Mockito using @ExtendWith(MockitoExtension.class). 1. Add Mockito dependencies Maven <dependencies> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter</artifactId> <version>5.11.4</version> <scope>test</scope> </dependency> <dependency> <gr...
JUNIT / NOTES</>↗JUnit13 Jul 2026
To mock dependencies in unit tests, you usually use a mocking framework such as Mockito. Mocking lets you test one class in isolation without running the real logic of its collaborators. Basic Mockito Example Suppose you have a service that depends on another class: @Service public class MyService { private final MyDependency dependency; public MyService(MyDependency […]
JUNIT / NOTES</>↗JUnit13 Jul 2026
To use Mockito with JUnit, you add Mockito to your test dependencies, enable Mockito in your JUnit test class, then create mocks and define their behavior. Below is a simple JUnit 5 + Mockito example. 1. Add Dependencies Maven <dependencies> <!– JUnit 5 –> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter</artifactId> <version>5.11.4</version> <scope>test</scope> </dependency> <!– Mockito Core –> <dependency> […]
JUNIT / NOTES</>↗JUnit13 Jul 2026
Java records are compact classes designed to hold immutable data. Because records automatically provide a constructor, accessor methods, equals(), hashCode(), and toString(), testing them is usually simpler than testing ordinary classes. In most cases, you do not need to test Java’s generated record behavior directly. Instead, test: custom validation in the compact constructor custom methods […]
JUNIT / NOTES</>↗JUnit13 Jul 2026
Utility classes are usually tested like any other unit: test their public behavior, especially calculations, transformations, validation rules, edge cases, and error handling. The main difference is that utility classes often have static methods, so your tests usually call the method directly instead of creating an object. 1. Test Useful Behavior, Not the Fact That […]
JUNIT / NOTES</>↗JUnit12 Jul 2026
In a real Java project, unit tests should be organized so they are easy to find, run, understand, and maintain. A good structure follows the same shape as your production code and keeps tests focused on behavior. Most Java projects use this standard layout: project-root ├── src │ ├── main │ │ └── java │ […]