mapstruct-antipattern-mitigation-with-unit-test

Describe how to make Unit Tests for insegure mappers that uses antipattern Setter-Based Mapping

Safety Notice

This listing is imported from skills.sh public index metadata. Review upstream SKILL.md and repository scripts before running.

Copy this and send it to your AI assistant to learn

Install skill "mapstruct-antipattern-mitigation-with-unit-test" with this command: npx skills add anibalanto/mapstruct-antipattern-mitigation-with-unit-test/anibalanto-mapstruct-antipattern-mitigation-with-unit-test-mapstruct-antipattern-mitigation-with-unit-test

mapstruct-antipattern-mitigation-with-unit-test

This is a technical standard for testing many existing mappers. The objective is to ensure that following any migration or changes to Source or Target classes, data transfer remains consistent and no unexpected null values are introduced.

🧪 Implementation Standard

To ensure the test is pure and decoupled from POJO state, you must mock the Source object behavior. This forces the developer to explicitly define which fields the mapper is expected to read.

Code Template (Java + JUnit 5 + Mockito)

@ExtendWith(MockitoExtension.class)
class UserMapperTest {

    private final UserMapper mapper = Mappers.getMapper(UserMapper.class);

    @Test
    void shouldMapSourceToTargetExhaustively() {
        // 1. Arrange: Source Mocking
        // We create a mock to define exactly what the "source" provides
        UserSource sourceMock = mock(UserSource.class);
        
        when(sourceMock.getFullName()).thenReturn("John Doe");
        when(sourceMock.getEmail()).thenReturn("john@example.com");
        when(sourceMock.getAge()).thenReturn(30);

        // 2. Act: Execute mapping logic
        UserTarget target = mapper.toTarget(sourceMock);

        // 3. Assert: Exhaustive field-by-field verification
        // Validating the whole object is forbidden; each attribute must be verified individually
        assertAll("Mapping Integrity Verification",
            // Validate @Mapping(source = "fullName", target = "name")
            () -> assertEquals(sourceMock.getFullName(), target.getName(), "Mapping error: name field"),
            
            // Validate implicit/identical name fields
            () -> assertEquals(sourceMock.getEmail(), target.getEmail(), "Mapping error: email field"),
            
            // Validate default values or constants defined in the mapper
            () -> assertEquals("ACTIVE", target.getStatus(), "Status must be ACTIVE per mapper config"),
            
            // Ensure no unexpected nulls exist in the target after migration
            () -> assertNotNull(target.getCreatedAt(), "Timestamp field must not be null")
        );
    }
}


🚨 Golden Rules (Checklist)

  • Mandatory Mocking: The Source object must be a mock. This ensures the test validates the mapper's interaction with the source API.
  • Individual Assertions: Using assertEquals(expectedObj, actualObj) is prohibited. Use individual assertEquals for every field in the Target.
  • Constant Validation: If a mapper defines @Mapping(target = "x", constant = "y"), the test must verify that "y" reaches the target regardless of the source.
  • Zero-Null Policy: If a Target field is mandatory post-migration, the test must fail if the mapper is not explicitly populating it.

When to use

This skill should be used when exists previously a lot of mappers using antipattern Setter-Based Mapping and is not possible migrate each mapper consistently.

Instructions

  1. Field Reflection: List all fields in the Target class.
  2. Origin Identification:
  • Direct Match: Fields with identical names in Source and Target.
  • @Mapping: Fields requiring explicit transformation or name changes.
  • Default/Constant: Fields populated by the mapper's internal configuration (not dependent on the Source).

Source Transparency

This detail page is rendered from real SKILL.md content. Trust labels are metadata-based hints, not a safety guarantee.

Related Skills

Related by shared tags or category signals.

General

test_skill

import json import tkinter as tk from tkinter import messagebox, simpledialog

Archived SourceRecently Updated
General

错敏信息检测

1. 安装https://clawhub.ai/xxxx/sensitive-check-skill 这个技能 2. 调用错敏信息检测技能,传入参数 userKey=xxxx,content=xxxx

Archived SourceRecently Updated
General

neo

Browse websites, read web pages, interact with web apps, call website APIs, and automate web tasks. Use Neo when: user asks to check a website, read a web page, post on social media (Twitter/X), interact with any web app, look up information on a specific site, scrape data from websites, automate browser tasks, or when you need to call any website's API. Keywords: website, web page, browse, URL, http, API, twitter, tweet, post, scrape, web app, open site, check site, read page, social media, online service.

Archived SourceRecently Updated
General

image-gen

Generate AI images from text prompts. Triggers on: "生成图片", "画一张", "AI图", "generate image", "配图", "create picture", "draw", "visualize", "generate an image".

Archived SourceRecently Updated