Back to Repositories

Validating BeanUtils Property Operations in Apollo Config

This test suite validates the BeanUtils utility class functionality in the Apollo framework, focusing on list transformation, key mapping, and property manipulation operations. The tests ensure robust handling of both valid and edge cases for bean property operations.

Test Coverage Overview

The test suite provides comprehensive coverage of BeanUtils functionality:
  • Batch transformation of lists with various input scenarios
  • Key-based mapping operations for collections
  • Property aggregation and set conversion
  • Direct property getting and setting
  • Exception handling for invalid inputs

Implementation Analysis

The testing approach utilizes JUnit and Mockito frameworks for structured unit testing. Each test method focuses on a specific aspect of BeanUtils, including empty collections, null values, and exception scenarios. The implementation leverages MockitoJUnitRunner for dependency injection and mock handling.

Key patterns include setup of test data through @Before initialization and expected exception testing with @Test(expected) annotations.

Technical Details

Testing infrastructure includes:
  • JUnit 4 testing framework
  • Mockito for mocking and verification
  • Custom KeyClass for property testing
  • ArrayList implementations for collection testing
  • BeanUtilsException handling

Best Practices Demonstrated

The test suite exemplifies several testing best practices:
  • Proper test isolation through @Before setup
  • Comprehensive edge case coverage
  • Clear test method naming conventions
  • Exception testing for error scenarios
  • Focused test methods for specific functionality

apolloconfig/apollo

apollo-common/src/test/java/com/ctrip/framework/apollo/common/utils/BeanUtilsTest.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.common.utils;

import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import java.util.ArrayList;
import java.util.List;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.junit.MockitoJUnitRunner;
import com.ctrip.framework.apollo.common.exception.BeanUtilsException;

@RunWith(MockitoJUnitRunner.class)
public class BeanUtilsTest {

  @InjectMocks
  private BeanUtils beanUtils;
  List<Integer> someList;
  List<KeyClass> someAnotherList;

  @Before
  public void setUp() {
    someList = new ArrayList<>();
    someAnotherList = new ArrayList<>();
  }

  @Test
  public void testBatchTransformListNotEmpty() {
    someList.add(77);
    assertNotNull(BeanUtils.batchTransform(String.class, someList));
  }

  @Test
  public void testBatchTransformListIsEmpty() {
    assertNotNull(BeanUtils.batchTransform(String.class, someList));
  }

  @Test(expected = BeanUtilsException.class)
  public void testBatchTransformBeanUtilsException() {
    someList.add(77);
    assertNotNull(BeanUtils.batchTransform(null, someList));
  }

  @Test
  public void testBatchTransformSrcIsNull() {
    someList.add(null);
    assertNotNull(BeanUtils.batchTransform(String.class, someList));
  }

  @Test
  public void testMapByKeyEmptyList() {
    assertNotNull(BeanUtils.mapByKey(null, someList));
  }

  class KeyClass {
    String keys;
  }

  @Test
  public void testMapByKeyNotEmptyList() {
    someAnotherList.add(new KeyClass());
    assertNotNull(BeanUtils.mapByKey("keys", someAnotherList));
  }

  @Test(expected = BeanUtilsException.class)
  public void testMapByKeyNotEmptyListThrowsEx() {
    someAnotherList.add(new KeyClass());
    assertNotNull(BeanUtils.mapByKey("wrongKey", someAnotherList));
  }

  @Test
  public void testAggByKeyToListEmpty() {
    assertNotNull(BeanUtils.aggByKeyToList("keys", someAnotherList));
  }

  @Test
  public void testAggByKeyToListNotEmpty() {
    someAnotherList.add(new KeyClass());
    assertNotNull(BeanUtils.aggByKeyToList("keys", someAnotherList));
  }

  @Test(expected = BeanUtilsException.class)
  public void testAggByKeyToListNotEmptyThrowsEx() {
    someAnotherList.add(new KeyClass());
    assertNotNull(BeanUtils.aggByKeyToList("wrongKey", someAnotherList));
  }

  @Test
  public void testToPropertySetEmpty() {
    assertNotNull(BeanUtils.toPropertySet("keys", someAnotherList));
  }

  @Test
  public void testToPropertySetNotEmpty() {
    someAnotherList.add(new KeyClass());
    assertNotNull(BeanUtils.toPropertySet("keys", someAnotherList));
  }

  @Test(expected = BeanUtilsException.class)
  public void testToPropertySetNotEmptyThrowsEx() {
    someAnotherList.add(new KeyClass());
    assertNotNull(BeanUtils.toPropertySet("wrongKey", someAnotherList));
  }

  @Test
  public void testGetAndsetProperty() {
    BeanUtils.setProperty(new KeyClass(), "keys", "value");
    assertNull(BeanUtils.getProperty(new KeyClass(), "keys"));
  }

}