Back to Repositories

Testing Case-Insensitive Map Operations Implementation in Apollo Config

This test suite validates the CaseInsensitiveMapWrapper class in Apollo’s configuration service, ensuring proper handling of case-insensitive key operations in map structures. The tests verify the wrapper’s ability to perform basic map operations while maintaining case insensitivity for keys.

Test Coverage Overview

The test suite provides comprehensive coverage of the CaseInsensitiveMapWrapper functionality.

  • Tests core map operations: get, put, and remove
  • Verifies case-insensitive key handling
  • Validates return values for all operations
  • Ensures consistent behavior with lowercase key transformations

Implementation Analysis

The testing approach utilizes Mockito for dependency isolation and JUnit for test structure.

Key patterns include:
  • Mock object creation for map dependencies
  • Behavior verification using Mockito verify()
  • Assertion-based result validation
  • Setup initialization using @Before annotation

Technical Details

Testing infrastructure includes:

  • JUnit 4 test framework
  • Mockito mocking framework
  • MockitoJUnitRunner for test execution
  • Mock annotations for dependency injection
  • Before setup method for test initialization

Best Practices Demonstrated

The test suite exemplifies several testing best practices:

  • Clear test method naming conveying purpose
  • Proper test isolation using mocks
  • Consistent verification of both results and behaviors
  • Clean setup and initialization patterns
  • Focused test methods testing single responsibilities

apolloconfig/apollo

apollo-configservice/src/test/java/com/ctrip/framework/apollo/configservice/wrapper/CaseInsensitiveMapWrapperTest.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.configservice.wrapper;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;

import java.util.Map;

import static org.junit.Assert.*;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

/**
 @author Jason Song([email protected])
 */
@RunWith(MockitoJUnitRunner.class)
public class CaseInsensitiveMapWrapperTest {
  private CaseInsensitiveMapWrapper<Object> caseInsensitiveMapWrapper;
  @Mock
  private Map<String, Object> someMap;

  @Before
  public void setUp() throws Exception {
    caseInsensitiveMapWrapper = new CaseInsensitiveMapWrapper<>(someMap);
  }

  @Test
  public void testGet() throws Exception {
    String someKey = "someKey";
    Object someValue = mock(Object.class);

    when(someMap.get(someKey.toLowerCase())).thenReturn(someValue);

    assertEquals(someValue, caseInsensitiveMapWrapper.get(someKey));

    verify(someMap, times(1)).get(someKey.toLowerCase());
  }

  @Test
  public void testPut() throws Exception {
    String someKey = "someKey";
    Object someValue = mock(Object.class);
    Object anotherValue = mock(Object.class);

    when(someMap.put(someKey.toLowerCase(), someValue)).thenReturn(anotherValue);

    assertEquals(anotherValue, caseInsensitiveMapWrapper.put(someKey, someValue));

    verify(someMap, times(1)).put(someKey.toLowerCase(), someValue);
  }

  @Test
  public void testRemove() throws Exception {
    String someKey = "someKey";
    Object someValue = mock(Object.class);

    when(someMap.remove(someKey.toLowerCase())).thenReturn(someValue);

    assertEquals(someValue, caseInsensitiveMapWrapper.remove(someKey));

    verify(someMap, times(1)).remove(someKey.toLowerCase());
  }
}