java 240 lines · 2 tabs

JUnit 5 and Mockito testing strategies

David Kumar Jan 2026
2 tabs
package com.example.demo.service;

import com.example.demo.exception.ResourceNotFoundException;
import com.example.demo.model.User;
import com.example.demo.repository.UserRepository;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import org.mockito.ArgumentCaptor;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;

import java.util.Arrays;
import java.util.List;
import java.util.Optional;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.*;

@ExtendWith(MockitoExtension.class)
class UserServiceTest {

    @Mock
    private UserRepository userRepository;

    @InjectMocks
    private UserService userService;

    private User testUser;

    @BeforeEach
    void setUp() {
        testUser = new User();
        testUser.setId(1L);
        testUser.setName("John Doe");
        testUser.setEmail("john@example.com");
    }

    @Test
    void findById_WhenUserExists_ReturnsUser() {
        // Arrange
        when(userRepository.findById(1L)).thenReturn(Optional.of(testUser));

        // Act
        Optional<User> result = userService.findById(1L);

        // Assert
        assertThat(result).isPresent();
        assertThat(result.get().getName()).isEqualTo("John Doe");
        verify(userRepository, times(1)).findById(1L);
    }

    @Test
    void findById_WhenUserDoesNotExist_ReturnsEmpty() {
        // Arrange
        when(userRepository.findById(999L)).thenReturn(Optional.empty());

        // Act
        Optional<User> result = userService.findById(999L);

        // Assert
        assertThat(result).isEmpty();
    }

    @Test
    void save_WhenValidUser_SavesAndReturnsUser() {
        // Arrange
        when(userRepository.save(any(User.class))).thenReturn(testUser);

        // Act
        User result = userService.save(testUser);

        // Assert
        assertThat(result).isNotNull();
        assertThat(result.getName()).isEqualTo("John Doe");

        // Verify save was called with correct argument
        ArgumentCaptor<User> userCaptor = ArgumentCaptor.forClass(User.class);
        verify(userRepository).save(userCaptor.capture());
        assertThat(userCaptor.getValue().getEmail()).isEqualTo("john@example.com");
    }

    @Test
    void findAll_ReturnsAllUsers() {
        // Arrange
        User user2 = new User();
        user2.setId(2L);
        user2.setName("Jane Doe");

        when(userRepository.findAll()).thenReturn(Arrays.asList(testUser, user2));

        // Act
        List<User> results = userService.findAll();

        // Assert
        assertThat(results).hasSize(2);
        assertThat(results).extracting(User::getName)
            .containsExactly("John Doe", "Jane Doe");
    }

    @Test
    void deleteById_WhenUserExists_DeletesUser() {
        // Arrange
        doNothing().when(userRepository).deleteById(1L);

        // Act
        userService.deleteById(1L);

        // Assert
        verify(userRepository, times(1)).deleteById(1L);
    }

    @ParameterizedTest
    @ValueSource(strings = {"john", "JOHN", "John", "JoHn"})
    void search_WithVariousCases_ReturnsResults(String query) {
        // Arrange
        when(userRepository.findByNameContainingIgnoreCase(anyString()))
            .thenReturn(Arrays.asList(testUser));

        // Act
        List<User> results = userService.search(query);

        // Assert
        assertThat(results).isNotEmpty();
    }

    @Test
    void existsById_WhenUserExists_ReturnsTrue() {
        // Arrange
        when(userRepository.existsById(1L)).thenReturn(true);

        // Act
        boolean exists = userService.existsById(1L);

        // Assert
        assertThat(exists).isTrue();
    }
}
2 files · java Explain with highlit

JUnit 5 provides a modern testing framework with @Test, @BeforeEach, @AfterEach lifecycle hooks. Mockito creates test doubles with @Mock and @InjectMocks annotations. I use when().thenReturn() to stub method responses and verify() to confirm interactions. ArgumentCaptor captures method arguments for detailed assertions. @ParameterizedTest runs tests with multiple inputs. @ExtendWith integrates Spring's test context. MockMvc tests REST controllers without starting a server. @WebMvcTest loads only web layer for fast tests. @DataJpaTest configures an in-memory database for repository tests. AssertJ provides fluent assertions. Test slices isolate components, improving test speed and focus. Proper mocking prevents flaky tests and enables true unit testing independent of external dependencies.


Related snips

ruby
class CommentsController < ApplicationController
  before_action :set_post

  def create
    @comment = @post.comments.build(comment_params)

System test: asserting Turbo Stream responses

rails hotwire turbo
by codesnips 4 tabs
graphql
type User {
    id: ID!
    name: String!
    email: String!
    posts: [Post!]!
    createdAt: String!

GraphQL API with Spring Boot

java graphql spring-boot
by David Kumar 3 tabs
java
package com.example.starter.config;

import com.example.starter.properties.CustomProperties;
import com.example.starter.service.CustomService;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;

Custom Spring Boot starters

java spring-boot starter
by David Kumar 4 tabs
rust
use my_crate::add;

#[test]
fn test_public_api() {
    assert_eq!(add(3, 4), 7);
}

Integration tests in tests/ directory

rust testing integration
by Marcus Chen 1 tab
java
package com.example.demo.config;

import org.apache.kafka.clients.consumer.ConsumerConfig;
import org.apache.kafka.clients.producer.ProducerConfig;
import org.apache.kafka.common.serialization.StringDeserializer;
import org.apache.kafka.common.serialization.StringSerializer;

Messaging with Apache Kafka

java kafka messaging
by David Kumar 3 tabs
kotlin
package com.example.myapp.data.repository

import com.example.myapp.data.local.PostDao
import com.example.myapp.data.local.PostEntity
import com.example.myapp.data.remote.ApiService
import com.example.myapp.models.Post

Unit testing with JUnit and MockK

kotlin android testing
by Alex Chen 2 tabs

Share this code

Here's the card — post it anywhere.

JUnit 5 and Mockito testing strategies — share card
Link copied