Back to Repositories

Testing Portal Configuration Management Implementation in Apollo Config

This test suite validates the configuration management functionality in Apollo Portal, focusing on property retrieval and type conversion capabilities. The tests ensure proper handling of environment properties with various data types and default values.

Test Coverage Overview

The test suite provides comprehensive coverage of the PortalConfig component’s core functionality, including:

  • Property retrieval with default values
  • Array property parsing and conversion
  • Boolean property type conversion
  • Integer property parsing
  • Edge cases for non-existent properties

Implementation Analysis

The testing approach utilizes JUnit and Mockito frameworks to validate configuration handling. The implementation employs mock objects for the ConfigurableEnvironment to simulate various property scenarios and verify correct type conversions.

Key patterns include dependency injection through @InjectMocks and @Mock annotations, and behavior stubbing using when() statements.

Technical Details

Testing tools and setup:

  • JUnit 4 for test execution
  • Mockito for dependency mocking
  • AbstractUnitTest as the base test class
  • Spring ConfigurableEnvironment for configuration management
  • PortalConfig component under test

Best Practices Demonstrated

The test suite exemplifies several testing best practices:

  • Isolated test cases with clear assertions
  • Proper mock setup and verification
  • Consistent test method naming convention
  • Coverage of both happy path and edge cases
  • Effective use of test frameworks and annotations

apolloconfig/apollo

apollo-portal/src/test/java/com/ctrip/framework/apollo/portal/config/ConfigTest.java

            
/*
 * Copyright 2024 Apollo Authors
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */
package com.ctrip.framework.apollo.portal.config;


import com.ctrip.framework.apollo.portal.AbstractUnitTest;
import com.ctrip.framework.apollo.portal.component.config.PortalConfig;

import org.junit.Assert;
import org.junit.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.springframework.core.env.ConfigurableEnvironment;

import static org.mockito.Mockito.when;

public class ConfigTest extends AbstractUnitTest{

  @Mock
  private ConfigurableEnvironment environment;
  @InjectMocks
  private PortalConfig config;


  @Test
  public void testGetNotExistValue() {
    String testKey = "key";
    String testDefaultValue = "value";

    when(environment.getProperty(testKey, testDefaultValue)).thenReturn(testDefaultValue);

    Assert.assertEquals(testDefaultValue, config.getValue(testKey, testDefaultValue));
  }

  @Test
  public void testGetArrayProperty() {
    String testKey = "key";
    String testValue = "a,b,c";

    when(environment.getProperty(testKey)).thenReturn(testValue);

    String[] result = config.getArrayProperty(testKey, null);

    Assert.assertEquals(3, result.length);
    Assert.assertEquals("a", result[0]);
    Assert.assertEquals("b", result[1]);
    Assert.assertEquals("c", result[2]);
  }

  @Test
  public void testGetBooleanProperty() {
    String testKey = "key";
    String testValue = "true";

    when(environment.getProperty(testKey)).thenReturn(testValue);

    boolean result = config.getBooleanProperty(testKey, false);

    Assert.assertTrue(result);
  }

  @Test
  public void testGetIntProperty() {
    String testKey = "key";
    String testValue = "1024";

    when(environment.getProperty(testKey)).thenReturn(testValue);

    int result = config.getIntProperty(testKey, 0);

    Assert.assertEquals(1024, result);

  }


}