Skip to content
Back to skills

Java Testing

ASecurity

Use Mockito with JUnit 5’s `MockitoExtension` to inject a mock `UserRepository` into the `UserService`, then stub the repository and verify the interaction. ```java import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; import java.util.Optional; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.InjectMocks; import...

  • 584 stars
  • 0 votes
  • 0 copies
  • 4 views
  • Added September 5, 2026
developmentjavaapidatabase

Works with

  • api

Security analysis

A100/100

Pro scans all 18 files and shows the line behind each finding

Scanned September 5, 2026

npx -y skills add HoangNguyen0403/agent-skills-standard --skill java-testing --agent claude-code

Installs into .claude/skills of the current project.

Are you the author of Java Testing?

Add the live security badge to your README. It updates with every re-scan.

Security grade badge for Java Testing
[![Security: A — Skills Directory](https://www.skillsdirectory.com/api/skills/hoangnguyen0403-java-testing-agent-skills-standard/badge)](https://www.skillsdirectory.com/skills/hoangnguyen0403-java-testing-agent-skills-standard)

More formats (shields.io, HTML) on the badges page. Keep it an A: scan every change in CI with Pro.

Download with Pro
SKILL.md
Use Mockito with JUnit 5’s `MockitoExtension` to inject a mock `UserRepository` into the `UserService`, then stub the repository and verify the interaction.

```java
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import java.util.Optional;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;

@ExtendWith(MockitoExtension.class)
class UserServiceTest {

    @Mock
    private UserRepository userRepository;

    @InjectMocks
    private UserService userService;

    @Test
    void findById_WhenUserExists_ReturnsUser() {
        User expected = new User(42L, "Ada Lovelace");
        when(userRepository.findById(42L)).thenReturn(Optional.of(expected));

        User actual = userService.findById(42L);

        assertThat(actual)
                .isNotNull()
                .isEqualTo(expected);
        verify(userRepository, times(1)).findById(42L);
    }
}
```

For a missing user, test the exception explicitly:

```java
import static org.assertj.core.api.Assertions.assertThatThrownBy;

@Test
void findById_WhenUserDoesNotExist_ThrowsException() {
    when(userRepository.findById(42L)).thenReturn(Optional.empty());

    assertThatThrownBy(() -> userService.findById(42L))
            .isInstanceOf(UserNotFoundException.class)
            .hasMessage("User not found: 42");

    verify(userRepository, times(1)).findById(42L);
}
```

This assumes the service has constructor injection, for example:

```java
class UserService {
    private final UserRepository userRepository;

    UserService(UserRepository userRepository) {
        this.userRepository = userRepository;
    }

    User findById(long id) {
        return userRepository.findById(id)
                .orElseThrow(() -> new UserNotFoundException("User not found: " + id));
    }
}
```

The test remains isolated because the repository is mocked; no database or shared state is involved.

Files in this skill

  • eval-1.baseline.md1.6 KB
  • eval-1.with-skill.md2.1 KB
  • eval-2.baseline.md604 B
  • eval-2.with-skill.md973 B
  • eval-3.baseline.md2.7 KB
  • eval-3.with-skill.md3.2 KB
  • trigger-1.md158 B
  • trigger-10.md85 B
  • trigger-11.md142 B
  • trigger-2.md150 B
  • trigger-3.md176 B
  • trigger-4.md121 B
  • trigger-5.md124 B
  • trigger-6.md117 B
  • trigger-7.md138 B
  • trigger-8.md187 B
  • trigger-9.md127 B

Attribution

Is this your skill, or is something wrong with this listing? Request removal or report an issue. Author removals are honored within 72 hours.

View all in development →

Comments

Loading comments…

Newsletter

Get the weekly list of safe new skills, and the ones we pulled.