Back to Repositories

Validating Configuration Property Resolution in Litemall

This test suite validates the configuration loading and environment property access in the Litemall application. It ensures proper integration of multiple YAML configuration files and verifies the Environment bean’s ability to retrieve configuration values across different application modules.

Test Coverage Overview

The test suite provides comprehensive coverage of configuration property access across multiple YAML files.

Key areas tested include:
  • Core configuration properties (express.appId)
  • Database configuration (datasource properties)
  • WeChat integration settings
  • Logging level configurations
  • Admin module properties

Implementation Analysis

The testing approach utilizes Spring’s TestContext framework with JUnit4 integration.

Key implementation features:
  • @WebAppConfiguration for web environment simulation
  • @SpringBootTest for full application context loading
  • Environment bean autowiring for property access
  • SpringJUnit4ClassRunner for test execution

Technical Details

Testing infrastructure includes:
  • JUnit 4 testing framework
  • Spring Boot Test module
  • Spring TestContext Framework
  • Environment interface for property resolution
  • Multiple YAML configuration files (core, db, wx, admin)

Best Practices Demonstrated

The test demonstrates several testing best practices:

  • Proper test class annotation structure
  • Dependency injection in tests
  • Systematic configuration verification
  • Clear separation of configuration concerns
  • Comprehensive environment property testing

linlinjava/litemall

litemall-all/src/test/java/org/linlinjava/litemall/allinone/AllinoneConfigTest.java

            
package org.linlinjava.litemall.allinone;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.core.env.Environment;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;

@WebAppConfiguration
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest
public class AllinoneConfigTest {
    @Autowired
    private Environment environment;

    @Test
    public void test() {
        // 测试获取application-core.yml配置信息
        System.out.println(environment.getProperty("litemall.express.appId"));
        // 测试获取application-db.yml配置信息
        System.out.println(environment.getProperty("spring.datasource.druid.url"));
        // 测试获取application-wx.yml配置信息
        System.out.println(environment.getProperty("litemall.wx.app-id"));
        // 测试获取application-admin.yml配置信息
//        System.out.println(environment.getProperty(""));
        // 测试获取application.yml配置信息
        System.out.println(environment.getProperty("logging.level.org.linlinjava.litemall.wx"));
        System.out.println(environment.getProperty("logging.level.org.linlinjava.litemall.admin"));
        System.out.println(environment.getProperty("logging.level.org.linlinjava.litemall"));
    }

}