junit

$npx mdskill add partme-ai/full-stack-skills/junit

Guides developers in writing and configuring JUnit unit tests for Java and Kotlin projects using annotations, assertions, and best practices.

  • Helps users create and manage unit tests with JUnit 4 or 5 frameworks.
  • Integrates with Mockito, AssertJ, Maven Surefire, and Gradle for enhanced testing workflows.
  • Recommends solutions based on user queries about test annotations, assertions, and lifecycle management.
  • Delivers results through code examples, step-by-step instructions, and configuration guidance.

SKILL.md

.github/skills/junitView on GitHub ↗
---
name: junit
description: "Provides comprehensive guidance for JUnit testing framework including test annotations, assertions, test lifecycle, and best practices. Use when the user asks about JUnit, needs to write Java unit tests, use JUnit annotations, or configure JUnit for Java projects."
license: Complete terms in LICENSE.txt
---

## When to use this skill

Use this skill whenever the user wants to:
- Write Java or Kotlin unit tests with JUnit 4 or JUnit 5 (Jupiter)
- Use annotations (@Test, @BeforeEach, @AfterEach, @ParameterizedTest)
- Apply assertions (assertEquals, assertThrows, assertAll)
- Integrate with Mockito or AssertJ for mocking and fluent assertions
- Configure test execution with Maven Surefire or Gradle

## How to use this skill

### Workflow

1. **Write test classes and methods** annotated with `@Test`
2. **Set up and tear down** test state with lifecycle annotations
3. **Use assertions** to verify expected behavior
4. **Mock dependencies** with Mockito for isolated unit tests

### 1. Basic JUnit 5 Test

```java
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;

class CalculatorTest {
    @Test
    void shouldAddTwoNumbers() {
        Calculator calc = new Calculator();
        assertEquals(5, calc.add(2, 3));
    }

    @Test
    void shouldThrowOnDivideByZero() {
        Calculator calc = new Calculator();
        assertThrows(ArithmeticException.class, () -> calc.divide(1, 0));
    }
}
```

### 2. Lifecycle Annotations

```java
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.AfterEach;

class UserServiceTest {
    private UserService userService;

    @BeforeEach
    void setUp() {
        userService = new UserService(new InMemoryUserRepository());
    }

    @AfterEach
    void tearDown() {
        // Clean up resources
    }
}
```

### 3. Parameterized Tests

```java
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;

class StringTest {
    @ParameterizedTest
    @CsvSource({"hello,5", "'',0", "junit,5"})
    void shouldReturnCorrectLength(String input, int expected) {
        assertEquals(expected, input.length());
    }
}
```

### 4. Mocking with Mockito

```java
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;
import static org.mockito.Mockito.*;

@ExtendWith(MockitoExtension.class)
class OrderServiceTest {
    @Mock private OrderRepository orderRepository;
    @InjectMocks private OrderService orderService;

    @Test
    void shouldSaveOrder() {
        Order order = new Order("item-1", 2);
        orderService.placeOrder(order);
        verify(orderRepository).save(order);
    }
}
```

### 5. Running Tests

```bash
# Maven
mvn test

# Gradle
gradle test

# With coverage (JaCoCo)
mvn test jacoco:report
```

## Best Practices

- Keep tests independent and repeatable; avoid dependencies on execution order or external services
- Use Mockito to isolate the unit under test from its dependencies
- Name tests descriptively (e.g., `shouldReturnEmptyListWhenNoUsersExist`)
- Balance coverage with test speed; run full suite in CI
- Use `@Nested` classes to group related test cases

## Resources

- JUnit 5 User Guide: https://junit.org/junit5/docs/current/user-guide/
- Mockito: https://site.mockito.org/
- AssertJ: https://assertj.github.io/doc/

## Keywords

junit, JUnit 5, JUnit Jupiter, unit testing, Java, Kotlin, assertions, Mockito, parameterized tests, BeforeEach, AfterEach, test lifecycle

More from partme-ai/full-stack-skills

SkillDescription
adobe-xd"Guides creation of UI/UX designs, interactive prototypes, reusable components, and design specs in Adobe XD. Use when the user asks about Adobe XD artboards, prototype links, repeat grids, component states, design tokens export, or developer handoff."
angular"Provides comprehensive guidance for Angular framework including components, modules, services, dependency injection, routing, forms, and TypeScript integration. Use when the user asks about Angular, needs to create Angular applications, implement Angular components, or work with Angular features."
ansible"Provides comprehensive guidance for Ansible automation including playbooks, roles, inventory, and module usage. Use when the user asks about Ansible, needs to automate IT tasks, create Ansible playbooks, or manage infrastructure with Ansible."
ant-design-mini"Builds mini-program UIs with Ant Design Mini components for Alipay and WeChat mini-programs. Covers Button, Form, List, Modal, Tabs, NavBar, and 60+ components with theme customization and CSS variable theming. Use when the user needs to create mini-program interfaces with Ant Design Mini, configure themes, or implement mini-program-specific UI patterns."
ant-design-mobile"Builds React mobile UIs with Ant Design Mobile (antd-mobile) components including Button, Form, List, Modal, Picker, Tabs, PullToRefresh, InfiniteScroll, and 50+ mobile-optimized components. Use when the user needs to create mobile-first React interfaces, implement mobile navigation, forms, or data display with Ant Design Mobile."
ant-design-react"Builds enterprise React UIs with Ant Design (antd) including 60+ components (Button, Form, Table, Select, Modal, Message), design tokens, TypeScript support, and ConfigProvider theming. Use when the user needs to create React applications with Ant Design, build forms with validation, display data tables, or customize the Ant Design theme."
ant-design-vueProvides comprehensive guidance for Ant Design Vue (AntDV) component library for Vue 3. Covers installation, usage, API reference, templates, and all component categories. Use when building enterprise-class UI with Vue 3 and Ant Design.
api-doc-generator"Generate API documentation by scanning Controller classes, extracting endpoint URLs, HTTP methods, parameters, and response structures, then producing standardized docs from templates. Use when the user explicitly mentions generating API documentation, creating API docs, scanning interfaces, or documenting REST APIs. Do not trigger for generic documentation requests without explicit API mention."
appium"Provides comprehensive guidance for Appium mobile testing including mobile app automation, element location, gestures, and cross-platform testing. Use when the user asks about Appium, needs to test mobile applications, automate mobile apps, or write Appium test scripts."
ascii-ansi-colorizer"Add an ANSI color layer to existing ASCII/plain-text output (gradient/rainbow/highlights) with alignment-safe rules and a required no-color fallback. Use when the user wants to colorize terminal output, add rainbow effects to CLI text, or style ASCII art with ANSI colors."