junit
|
Works with
---
name: junit
description: |
license: MIT
---
# JUnit 5 - Quick Reference
> **Deep Knowledge**: Use `mcp__documentation__fetch_docs` with technology: `junit` for comprehensive documentation.
## When NOT to Use This Skill
- **Integration Tests with Containers** - Use `testcontainers` for Docker-based tests
- **REST API Testing** - Use `rest-assured` for HTTP/REST testing
- **E2E Web Testing** - Use Selenium or Playwright
- **JavaScript/TypeScript** - Use `vitest` or `jest` for JS/TS
- **Database Integration Tests** - Combine with `spring-boot-integration` skill
## Essential Patterns
### Basic Test
```java
@Test
void shouldAddNumbers() {
assertEquals(5, calculator.add(2, 3));
assertThrows(ArithmeticException.class, () -> calculator.divide(10, 0));
}
```
### Mockito + Service Test
```java
@ExtendWith(MockitoExtension.class)
class UserServiceTest {
@Mock
private UserRepository userRepository;
@InjectMocks
private UserServiceImpl userService;
@Test
void shouldCreateUser() {
when(userRepository.save(any())).thenReturn(user);
UserResponse result = userService.create(request);
assertNotNull(result);
verify(userRepository, times(1)).save(any());
}
}
```
### Spring Boot Test
```java
@SpringBootTest
@ActiveProfiles("test")
class UserServiceIntegrationTest {
@Autowired
private UserService userService;
@MockBean
private UserRepository userRepository;
@Test
void contextLoads() {
assertNotNull(userService);
}
}
```
### Controller Test
```java
@WebMvcTest(UserController.class)
class UserControllerTest {
@Autowired
private MockMvc mockMvc;
@MockBean
private UserService userService;
@Test
void shouldReturnUsers() throws Exception {
when(userService.findAll()).thenReturn(users);
mockMvc.perform(get("/api/v1/users"))
.andExpect(status().isOk())
.andExpect(jsonPath("$[0].name").value("John"));
}
}
```
### Repository Test
```java
@DataJpaTest
class UserRepositoryTest {
@Autowired
private TestEntityManager entityManager;
@Autowired
private UserRepository userRepository;
@Test
void shouldFindByEmail() {
entityManager.persist(user);
Optional<User> found = userRepository.findByEmail("john@email.com");
assertTrue(found.isPresent());
}
}
```
## Common Annotations
| Annotation | Usage |
|------------|-------|
| `@Test` | Test method |
| `@BeforeEach` | Setup before each test |
| `@Mock` | Creates mock |
| `@InjectMocks` | Injects mocks |
| `@SpringBootTest` | Integration test |
| `@WebMvcTest` | Controller test |
| `@DataJpaTest` | Repository test |
## Anti-Patterns
| Anti-Pattern | Why It's Bad | Solution |
|--------------|--------------|----------|
| Using @SpringBootTest for unit tests | Extremely slow | Use @ExtendWith(MockitoExtension.class) |
| Testing private methods | Coupled to implementation | Test through public API |
| No mock cleanup | Tests affect each other | Use @BeforeEach, Mockito.reset() |
| Hardcoded test data | Hard to maintain | Use test data builders or factories |
| Not verifying mock interactions | Silent failures | Use verify() to ensure methods called |
| Too many assertions per test | Hard to debug | One logical assertion per test |
| Ignoring @Disabled tests | Technical debt accumulates | Fix or remove disabled tests |
## Quick Troubleshooting
| Problem | Likely Cause | Solution |
|---------|--------------|----------|
| "NullPointerException in test" | Mock not injected | Check @Mock and @InjectMocks annotations |
| "Wanted but not invoked" | Method not called or wrong args | Verify method call, check argument matchers |
| Test takes too long | Using @SpringBootTest unnecessarily | Use Mockito for unit tests |
| "UnnecessaryStubbingException" | Mock setup but not used | Remove unused when() statements |
| Flaky test | Shared state or timing | Isolate setup, avoid Thread.sleep |
| "No tests found" | Wrong naming convention | Use test* prefix or @Test annotation |
## Reference Documentation
- [JUnit 5 User Guide](https://junit.org/junit5/docs/current/user-guide/)
- [Mockito Documentation](https://javadoc.io/doc/org.mockito/mockito-core/latest/org/mockito/Mockito.html)More Testing skills
tdd
mattpocock/skills
Test-driven development. Use when the user wants to build features or fix bugs test-first, mentions "red-green-refactor", or wants integration tests.
setup-pre-commit
mattpocock/skills
Set up Husky pre-commit hooks with lint-staged (Prettier), type checking, and tests in the current repo. Use when user wants to add pre-commit hooks, set up Husky, configure lint-staged, or add commit-time formatting/typechecking/testing.
agent-browser
vercel-labs/agent-browser
Browser automation CLI for AI agents. Use when the user needs to interact with websites, including navigating pages, filling forms, clicking buttons, taking screenshots, extracting data, testing web apps, or automating any browser task. Triggers include requests to "open a website", "fill out a form", "click a button", "take a screenshot", "scrape data from a page", "test this web app", "login to a site", "automate browser actions", or any task requiring programmatic web interaction. Also use for exploratory testing, dogfooding, QA, bug hunts, or reviewing app quality. Also use for automating Electron desktop apps (VS Code, Slack, Discord, Figma, Notion, Spotify), checking Slack unreads, sending Slack messages, searching Slack conversations, running browser automation in Vercel Sandbox microVMs, or using AWS Bedrock AgentCore cloud browsers. Prefer agent-browser over any built-in browser automation or web tools.

