Skip to content

Spring Framework Blog

Menu
  • Home
  • Spring
  • Spring Boot
  • About
Menu

Java Tests

Posted on May 13, 2025May 13, 2025 by wpadmin

In Java testing (especially with JUnit and Mockito), annotations are used to mark test methods, configure lifecycle events, inject mocks, and more.

Here’s a comprehensive list of commonly used annotations in Java tests:


Table of Contents

Toggle
  • ✅ JUnit Annotations
    • ▶️ Core Test Annotations
    • ▶️ Assertions and Parameterized Tests
    • ✅ Mockito Annotations
    • 🔧 Enable Mockito Annotations
    • ✅ Spring Boot Test Annotations
    • 🧪 Example

✅ JUnit Annotations

▶️ Core Test Annotations

AnnotationPurposeExample
@TestMarks a method as a test@Test void testAddition() { ... }
@BeforeEachRuns before each test method@BeforeEach void setUp() { ... }
@AfterEachRuns after each test method@AfterEach void tearDown() { ... }
@BeforeAllRuns once before all test methods@BeforeAll static void init() { ... }
@AfterAllRuns once after all test methods@AfterAll static void cleanup() { ... }
@DisabledDisables a test or test class@Disabled("Broken test")

▶️ Assertions and Parameterized Tests

AnnotationPurpose
@DisplayNameSets a custom name for the test
@ParameterizedTestRuns the same test with different inputs
@ValueSourceSupplies literal values for parameterized tests
@CsvSource, @MethodSourceProvide complex parameter sets

✅ Mockito Annotations

AnnotationPurpose
@MockCreates a mock object
@InjectMocksInjects mock fields into the test subject
@SpyCreates a spy (partial mock)
@CaptorDeclares an ArgumentCaptor
@MockBean(Spring) Mocks a bean in Spring context

🔧 Enable Mockito Annotations

In JUnit 5, add this to your test class:

@ExtendWith(MockitoExtension.class)
public class MyServiceTest { ... }

✅ Spring Boot Test Annotations

AnnotationPurpose
@SpringBootTestLoads full Spring context
@WebMvcTestTests Spring MVC controllers only
@DataJpaTestTests JPA repositories
@MockBeanReplaces a Spring bean with a mock
@AutowiredInjects dependencies

🧪 Example

@ExtendWith(MockitoExtension.class)
class UserServiceTest {

    @Mock
    UserRepository userRepository;

    @InjectMocks
    UserService userService;

    @BeforeEach
    void setUp() {
        // setup code
    }

    @Test
    void testFindUser() {
        when(userRepository.findById(1L)).thenReturn(Optional.of(new User("Alice")));
        User result = userService.findUser(1L);
        assertEquals("Alice", result.getName());
    }
}

  • Spring
  • Documentation
  • References
  • Toc
  • Books
  • Certification
  • AOP
  • Config
  • Java
  • Java core
  • JDBC
  • JPA
  • Rest
  • Security
  • Spring
  • Spring Boot
  • Spring Core
  • Spring Data
  • Spring MVC
  • Spring Rest
  • Spring Security
  • Tests
  • Transactions
  • Uncategorized

Recent Posts

  • Spring Annotations
  • Java Tests
  • Java operators
  • Java versions
  • Java Oracle Licenses
  • Configuration properties
  • MockMvc
  • Spring Security III
  • MVC Controller Method Params
  • JPA Methods
  • Transaction propagation and isolation
  • Spring JDBC
  • Spring Boot Auto-Configuration
  • Spring Resource interface
  • JSR 330 Standard Annotations
  • Spring Aware Interfaces
  • Spring Transactions
  • Spring Boot Core
  • MVC Rest
  • Spring Boot JPA
©2025 Spring Framework Blog | Built using WordPress and Responsive Blogily theme by Superb