java-test-generator
Generate comprehensive JUnit 5 test cases for Java code with proper mocking and coverage. Use when writing tests, creating unit tests, generating test cases, adding JUnit tests, setting up Mockito mocks, writing parameterized tests, or achieving test coverage. Works with .java files, generates test classes following naming conventions, includes edge cases, boundary conditions, and uses Arrange-Act-Assert pattern.
What this skill does
# Java Test Generator
## Table of Contents
- [Purpose](#purpose)
- [When to Use](#when-to-use)
- [Quick Start](#quick-start)
- [Instructions](#instructions)
- [Examples](#examples)
- [Requirements](#requirements)
- [Testing Best Practices](#testing-best-practices)
- [Output Format](#output-format)
- [Error Handling](#error-handling)
## Purpose
Generates comprehensive, well-structured JUnit 5 test cases for Java code with proper mocking using Mockito, edge case coverage, parameterized tests, and adherence to testing best practices.
## When to Use
Use this skill when you need to:
- Generate JUnit 5 test cases for Java classes
- Create unit tests with Mockito mocking
- Write parameterized tests for multiple input scenarios
- Add test coverage for new or existing code
- Test service layers with dependency mocking
- Generate tests following Arrange-Act-Assert pattern
- Create tests for edge cases and boundary conditions
- Test exception handling scenarios
- Verify mock interactions with Mockito
- Generate tests for Spring Boot components
- Achieve high test coverage (>80%)
- Bootstrap test suites for new features
## Quick Start
Generate tests for any Java class instantly:
```bash
# Generate tests for a service class
Generate tests for UserService.java
# Generate tests for multiple classes
Generate tests for all classes in src/main/java/com/example/service/
```
## Instructions
### Step 1: Analyze Source Code
Read the target Java class and understand:
- Class purpose and responsibilities
- Public methods that need testing
- Dependencies (fields, constructor parameters)
- Return types and exception handling
- Business logic and edge cases
- Validation logic
Use Grep to find related classes if context is needed:
```bash
grep "class UserService" src/main/java/**/*.java
```
### Step 2: Identify Test Scenarios
For each public method, identify:
**Happy Path Tests:**
- Valid inputs producing expected outputs
- Typical use cases
**Edge Cases:**
- Boundary values (min, max, zero, empty)
- Null inputs (if not using @NonNull)
- Empty collections
- Special characters in strings
**Error Cases:**
- Invalid inputs
- Constraint violations
- Exception scenarios
**State-Based Tests:**
- Different object states
- Conditional branch coverage
**Integration Points:**
- Dependency interactions
- Method call verification
### Step 3: Generate Test Class Structure
Create test class following conventions:
**Naming:** [ClassName]Test.java (e.g., UserServiceTest.java)
**Location:** Mirror source structure in src/test/java/
**Structure Template:**
```java
@ExtendWith(MockitoExtension.class)
class ClassNameTest {
// Mocks for dependencies
@Mock
private DependencyClass mockDependency;
// System under test
@InjectMocks
private ClassUnderTest classUnderTest;
// Test data builders
private TestDataBuilder testDataBuilder;
@BeforeEach
void setUp() {
// Common test setup
}
@Nested
@DisplayName("methodName() tests")
class MethodNameTests {
// Group related tests
}
}
```
### Step 4: Write Individual Test Methods
Follow the Arrange-Act-Assert (AAA) pattern:
```java
@Test
@DisplayName("should return user when valid ID provided")
void shouldReturnUser_WhenValidIdProvided() {
// Arrange
String userId = "123";
User expectedUser = new User(userId, "John Doe");
when(mockRepository.findById(userId)).thenReturn(Optional.of(expectedUser));
// Act
Optional<User> result = userService.getUser(userId);
// Assert
assertThat(result).isPresent();
assertThat(result.get()).isEqualTo(expectedUser);
verify(mockRepository).findById(userId);
}
```
**Test Method Naming Conventions:**
- `shouldDoSomething_WhenCondition()` format
- Or `should_DoSomething_When_Condition()` for readability
- Descriptive names that explain the scenario
### Step 5: Add Parameterized Tests for Multiple Inputs
Use `@ParameterizedTest` for testing multiple similar scenarios:
```java
@ParameterizedTest
@ValueSource(strings = {"", " ", "\t", "\n"})
@DisplayName("should throw exception for blank names")
void shouldThrowException_ForBlankNames(String invalidName) {
assertThrows(IllegalArgumentException.class,
() -> userService.createUser(invalidName));
}
@ParameterizedTest
@CsvSource({
"[email protected], true",
"invalid-email, false",
"@example.com, false",
"john@, false"
})
@DisplayName("should validate email correctly")
void shouldValidateEmail_Correctly(String email, boolean expected) {
boolean result = validator.isValidEmail(email);
assertThat(result).isEqualTo(expected);
}
```
### Step 6: Add Exception Testing
Test exception scenarios explicitly:
```java
@Test
@DisplayName("should throw UserNotFoundException when user not found")
void shouldThrowUserNotFoundException_WhenUserNotFound() {
// Arrange
String userId = "999";
when(mockRepository.findById(userId)).thenReturn(Optional.empty());
// Act & Assert
assertThrows(UserNotFoundException.class,
() -> userService.getUser(userId));
verify(mockRepository).findById(userId);
}
```
### Step 7: Add Verification for Mock Interactions
Verify dependencies are called correctly:
```java
@Test
@DisplayName("should save user to repository")
void shouldSaveUser_ToRepository() {
// Arrange
User newUser = new User("John Doe", "[email protected]");
// Act
userService.createUser(newUser);
// Assert
verify(mockRepository).save(newUser);
verifyNoMoreInteractions(mockRepository);
}
```
### Step 8: Generate Test Data Builders (Optional)
For complex objects, create builder methods:
```java
private User createTestUser(String id, String name) {
return User.builder()
.id(id)
.name(name)
.email(name.toLowerCase() + "@example.com")
.createdAt(LocalDateTime.now())
.build();
}
```
### Step 9: Add Coverage for Edge Cases
Include boundary and special cases:
```java
@Nested
@DisplayName("Edge case tests")
class EdgeCaseTests {
@Test
@DisplayName("should handle empty list")
void shouldHandleEmptyList() {
List<User> emptyList = Collections.emptyList();
List<String> result = userService.extractNames(emptyList);
assertThat(result).isEmpty();
}
@Test
@DisplayName("should handle null optional")
void shouldHandleNullOptional() {
when(mockRepository.findById(anyString())).thenReturn(Optional.empty());
Optional<User> result = userService.getUser("123");
assertThat(result).isEmpty();
}
}
```
## Examples
### Example 1: Generate Tests for Simple Service
**Source Code:**
```java
@Service
@RequiredArgsConstructor
public class UserService {
private final UserRepository userRepository;
public Optional<User> getUser(String id) {
return userRepository.findById(id);
}
public User createUser(String name, String email) {
if (name == null || name.isBlank()) {
throw new IllegalArgumentException("Name cannot be blank");
}
User user = new User(name, email);
return userRepository.save(user);
}
}
```
**Generated Test:**
```java
@ExtendWith(MockitoExtension.class)
@DisplayName("UserService Tests")
class UserServiceTest {
@Mock
private UserRepository mockRepository;
@InjectMocks
private UserService userService;
@Nested
@DisplayName("getUser() tests")
class GetUserTests {
@Test
@DisplayName("should return user when ID exists")
void shouldReturnUser_WhenIdExists() {
// Arrange
String userId = "123";
User expectedUser = new User("John Doe", "[email protected]");
when(mockRepository.findById(userId))
.thenReturn(Optional.of(expectedUser));
// Act
Optional<User> result = userService.getUser(userId);
// Assert
assertThat(result).isPresent();
Related in Backend & APIs
jfrog
IncludedInteract with the JFrog Platform via the JFrog CLI and REST/GraphQL APIs. Use this skill when the user wants to manage Artifactory repositories, upload or download artifacts, manage builds, configure permissions, manage users and groups, work with access tokens, configure JFrog CLI servers, search artifacts, manage properties, set up replication, manage JFrog Projects, run security audits or scans, look up CVE details, query exposures scan results from JFrog Advanced Security, manage release bundles and lifecycle operations, aggregate or export platform data, or perform any JFrog Platform administration task. Also use when the user mentions jf, jfrog, artifactory, xray, distribution, evidence, apptrust, onemodel, graphql, workers, mission control, curation, advanced security, exposures, or any JFrog product name.
cupynumeric-migration-readiness
IncludedPre-migration readiness assessor for porting NumPy to cuPyNumeric. Use BEFORE substantial porting work begins when the user asks whether code will scale on GPU, whether they should migrate to cuPyNumeric, which NumPy patterns transfer cleanly, what must be refactored before porting, or mentions pre-port assessment, scaling analysis, or refactor planning. Inspect the user's source code, look up NumPy usage, cross-reference the cuPyNumeric API support manifest, and distinguish distributed-scaling-friendly patterns from blockers such as unsupported APIs, scalar synchronization, host round-trips, Python/object-heavy control flow, shape/data-dependent branching, and in-place mutation hazards. Produce a verdict of READY, LIGHT REFACTOR, SIGNIFICANT REFACTOR, or NOT RECOMMENDED, with concrete refactor pointers.
alibabacloud-data-agent-skill
IncludedInvoke Alibaba Cloud Apsara Data Agent for Analytics via CLI to perform natural language-driven data analysis on enterprise databases. Data Agent for Analytics is an intelligent data analysis agent developed by Alibaba Cloud Database team for enterprise users. It automatically completes requirement analysis, data understanding, analysis insights, and report generation based on natural language descriptions. This tool supports: discovering data resources (instances/databases/tables) managed in DMS, initiating query or deep analysis sessions, real-time progress tracking, and retrieving analysis conclusions and generated reports. Use this Skill when users need to query databases, analyze data trends, generate data reports, ask questions in natural language, or mention "Data Agent", "data analysis", "database query", "SQL analysis", "data insights".
token-optimizer
IncludedReduce OpenClaw token usage and API costs through smart model routing, heartbeat optimization, budget tracking, and native 2026.2.15 features (session pruning, bootstrap size limits, cache TTL alignment). Use when token costs are high, API rate limits are being hit, or hosting multiple agents at scale. The 4 executable scripts (context_optimizer, model_router, heartbeat_optimizer, token_tracker) are local-only — no network requests, no subprocess calls, no system modifications. Reference files (PROVIDERS.md, config-patches.json) document optional multi-provider strategies that require external API keys and network access if you choose to use them. See SECURITY.md for full breakdown.
resend-cli
IncludedUse this skill when the task is specifically about operating Resend from an AI agent, terminal session, or CI job via the official resend CLI: installing/authenticating the CLI, sending/listing/updating/cancelling emails, batch sends, domains and DNS, webhooks and local listeners, inbound receiving, contacts, topics, segments, broadcasts, templates, API keys, profiles, or debugging Resend CLI/API failures. Trigger on mentions of Resend CLI, `resend`, `resend doctor`, `resend emails send`, `resend domains`, `resend webhooks listen`, `resend emails receiving`, or agent-friendly terminal automation.
alibabacloud-odps-maxframe-coding
IncludedUse this skill for MaxFrame SDK development and documentation navigation on Alibaba Cloud MaxCompute (ODPS). Helps answer MaxFrame API, concept, official example, and supported pandas API questions; create data processing programs; read/write MaxCompute tables; debug jobs (remote or local); and build custom DPE runtime images. Trigger when users mention MaxFrame, MaxCompute with MaxFrame, ODPS table processing, DPE runtime, MaxFrame docs/examples, DataFrame/Tensor operations, or GPU runtime setup. Works for both English and Chinese queries about Alibaba Cloud data processing with MaxFrame.