Back to Repositories

Testing MyBatis Mapper Operations in Litemall System Configuration

This test suite validates the database mapping operations for the LitemallSystem entity in the litemall e-commerce application. It focuses on testing CRUD operations through MyBatis mapper functionality to ensure proper database interactions.

Test Coverage Overview

The test suite provides comprehensive coverage of basic database operations through the LitemallSystemMapper.

Key areas tested include:
  • Insert operations with selective field mapping
  • Delete operations using primary key
  • Update operations with primary key validation
  • Return value verification for all operations

Implementation Analysis

The testing approach utilizes Spring Boot’s test framework integrated with JUnit4. The implementation leverages @SpringBootTest for context loading and @WebAppConfiguration for web environment simulation.

Key patterns include:
  • Dependency injection of mapper interfaces
  • Assertion-based result validation
  • Transaction-aware test execution

Technical Details

Testing tools and configuration:
  • JUnit 4 test runner
  • Spring Test context framework
  • MyBatis mapper interfaces
  • Spring Boot test annotations
  • Assert utilities for verification

Best Practices Demonstrated

The test demonstrates several testing best practices for database integration testing.

Notable practices include:
  • Isolated test scenarios
  • Proper cleanup of test data
  • Verification of operation return values
  • Clear test method organization
  • Use of appropriate test annotations

linlinjava/litemall

litemall-db/src/test/java/org/linlinjava/litemall/db/MapperReturnTest.java

            
package org.linlinjava.litemall.db;

import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.linlinjava.litemall.db.dao.LitemallSystemMapper;
import org.linlinjava.litemall.db.domain.LitemallSystem;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.context.web.WebAppConfiguration;

@WebAppConfiguration
@RunWith(SpringRunner.class)
@SpringBootTest
public class MapperReturnTest {

    @Autowired
    private LitemallSystemMapper systemMapper;

    @Test
    public void test() {
        LitemallSystem system = new LitemallSystem();
        system.setKeyName("test-system-key");
        system.setKeyValue("test-system-value");
        int updates = systemMapper.insertSelective(system);
        Assert.assertEquals(updates, 1);

        updates = systemMapper.deleteByPrimaryKey(system.getId());
        Assert.assertEquals(updates, 1);

        updates = systemMapper.updateByPrimaryKey(system);
        Assert.assertEquals(updates, 0);
    }

}