0
0
JUnittesting~8 mins

@InjectMocks annotation in JUnit - Framework Patterns

Choose your learning style9 modes available
Framework Mode - @InjectMocks annotation
Folder Structure for JUnit with Mockito (@InjectMocks)
project-root/
├── src/
│   ├── main/
│   │   └── java/
│   │       └── com/example/app/
│   │           ├── service/
│   │           │   └── UserService.java
│   │           └── repository/
│   │               └── UserRepository.java
│   └── test/
│       └── java/
│           └── com/example/app/
│               ├── service/
│               │   └── UserServiceTest.java  <--- Test class with @InjectMocks
│               └── repository/
│                   └── UserRepositoryTest.java
├── build.gradle or pom.xml
└── src/test/resources/
    └── test.properties
Test Framework Layers
  • Test Classes: Classes like UserServiceTest where @InjectMocks is used to create the class under test with its dependencies injected.
  • Mock Objects: Dependencies annotated with @Mock that are injected into the class under test.
  • Service Layer: Actual business logic classes (e.g., UserService).
  • Repository Layer: Data access classes (e.g., UserRepository), often mocked in tests.
  • Utilities: Helper classes for common test setup or data creation.
  • Configuration: Setup for Mockito and JUnit integration, e.g., @ExtendWith(MockitoExtension.class).
Configuration Patterns
  • Use @ExtendWith(MockitoExtension.class) on test classes to enable Mockito annotations.
  • Annotate dependencies with @Mock to create mock instances.
  • Annotate the class under test with @InjectMocks to inject mocks automatically.
  • Use src/test/resources/test.properties or similar for environment-specific test data.
  • Configure build tool (Maven/Gradle) to include Mockito and JUnit Jupiter dependencies.
Test Reporting and CI/CD Integration
  • JUnit generates XML test reports by default, which CI tools (Jenkins, GitHub Actions) can consume.
  • Use build tools to run tests and generate reports automatically on each commit.
  • Mockito tests with @InjectMocks appear as normal JUnit tests in reports.
  • Integrate code coverage tools (JaCoCo) to measure test coverage.
  • Fail builds if tests with mocks fail, ensuring dependency injection correctness.
Best Practices for Using @InjectMocks
  1. Keep Tests Focused: Use @InjectMocks to test one class at a time with its dependencies mocked.
  2. Use Constructor Injection: Prefer constructor injection in your classes to make @InjectMocks work reliably.
  3. Initialize Mocks Properly: Use @ExtendWith(MockitoExtension.class) or MockitoAnnotations.openMocks(this) to initialize mocks.
  4. Avoid Mixing Real and Mocked Dependencies: Keep dependencies either fully mocked or real to avoid confusion.
  5. Write Clear Assertions: Assert behavior on the class under test, not on mocks directly.
Self Check

Where in this folder structure would you add a new test class for a service named OrderService that uses @InjectMocks?

Key Result
Use @InjectMocks in JUnit tests to automatically inject mock dependencies into the class under test for clean, isolated unit testing.