Back to Repositories

Testing Product Management DAO Operations in Mall

This test suite validates the Product Management System (PMS) data access layer functionality in the Mall application, focusing on member pricing operations and product information retrieval. The tests ensure reliable database interactions and data integrity.

Test Coverage Overview

The test suite covers critical DAO operations for product management:
  • Batch insertion of member price records
  • Product information retrieval and updates
  • Transaction management and rollback scenarios
  • Data integrity validation for pricing operations

Implementation Analysis

The implementation utilizes Spring Boot’s testing framework with JUnit Jupiter, employing transaction management annotations for data consistency. The tests demonstrate proper DAO pattern implementation with automated rollback support and logging capabilities.

Key patterns include dependency injection for DAO components and assertion-based verification of database operations.

Technical Details

Testing stack includes:
  • JUnit Jupiter for test execution
  • Spring Boot Test for context management
  • SLF4J for logging
  • Hutool for JSON processing
  • @Transactional and @Rollback annotations for database state management

Best Practices Demonstrated

The test suite exemplifies several testing best practices:
  • Proper test isolation using transaction management
  • Clear test method naming conventions
  • Effective use of assertions for validation
  • Structured data setup and verification
  • Comprehensive logging for debugging

macrozheng/mall

mall-admin/src/test/com/macro/mall/PmsDaoTests.java

            
package com.macro.mall;


import cn.hutool.json.JSONUtil;
import com.macro.mall.dao.PmsMemberPriceDao;
import com.macro.mall.dao.PmsProductDao;
import com.macro.mall.dto.PmsProductResult;
import com.macro.mall.model.PmsMemberPrice;
import org.junit.jupiter.api.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.annotation.Rollback;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import static org.junit.jupiter.api.Assertions.assertEquals;

@SpringBootTest
public class PmsDaoTests {
    @Autowired
    private PmsMemberPriceDao memberPriceDao;
    @Autowired
    private PmsProductDao productDao;
    private static final Logger LOGGER = LoggerFactory.getLogger(PmsDaoTests.class);
    @Test
    @Transactional
    @Rollback
    public void testInsertBatch(){
        List<PmsMemberPrice> list = new ArrayList<>();
        for(int i=0;i<5;i++){
            PmsMemberPrice memberPrice = new PmsMemberPrice();
            memberPrice.setProductId(1L);
            memberPrice.setMemberLevelId((long) (i+1));
            memberPrice.setMemberPrice(new BigDecimal("22"));
            list.add(memberPrice);
        }
        int count = memberPriceDao.insertList(list);
        assertEquals(5,count);
    }

    @Test
    public void  testGetProductUpdateInfo(){
        PmsProductResult productResult = productDao.getUpdateInfo(7L);
        String json = JSONUtil.parse(productResult).toString();
        LOGGER.info(json);
    }
}