Testing

From andresdiegolanda/design-first-ai · View interactive version

Testing is an AI agent skill from the GitHub repository andresdiegolanda/design-first-ai. Consistent test structure for service and controller layers. Use when writing any new test or adding coverage.

How to use this skill

Skill file

Skill: Testing

When to load it: When writing any new test, or adding test coverage to an existing feature.


What This Skill Does

Produces unit tests for the service layer (plain JUnit + Mockito, no Spring context) and

integration tests for the controller layer (@WebMvcTest). Enforces a consistent naming

convention and test structure.


Rules

Unit tests (service layer):

Controller tests:

Naming: methodName_condition_expectedResult

Example: getById_unknownId_returns404


Pattern

Unit test:

@ExtendWith(MockitoExtension.class)
class [Resource]ServiceTest {

    @Mock
    private [Dependency] dependency;

    @InjectMocks
    private [Resource]Service service;

    @Test
    void [methodName]_[condition]_[expectedResult]() {
        // Arrange
        given(dependency.[method]()).willReturn([value]);

        // Act
        [ReturnType] result = service.[method]([args]);

        // Assert
        assertThat(result).[assertion];
    }
}

Controller test:

@WebMvcTest([Resource]Controller.class)
class [Resource]ControllerTest {

    @Autowired
    private MockMvc mockMvc;

    @MockBean
    private [Resource]Service service;

    @Test
    void [methodName]_[condition]_[expectedResult]() throws Exception {
        given(service.[method]([args])).willReturn([value]);

        mockMvc.perform(get("/api/v1/[resource]/{id}", [id]))
            .andExpect(status().isOk())
            .andExpect(jsonPath("$.field").value([expected]));
    }
}

Design Constraints

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.