Back to Repositories

Testing Service Discovery Controller Operations in Apollo Config

This test suite validates the Service Controller functionality in Apollo’s configuration service, focusing on service discovery and instance retrieval operations. The tests ensure proper handling of meta, config, and admin service endpoints through mock-based verification.

Test Coverage Overview

The test suite provides comprehensive coverage of the ServiceController class, verifying three core service endpoints.

Key functionality tested includes:
  • Meta service retrieval validation
  • Config service instance discovery with app and client parameters
  • Admin service instance lookup
Integration points cover the interaction with DiscoveryService and handling of ServiceDTO objects.

Implementation Analysis

The testing approach utilizes Mockito for dependency isolation and behavior verification. The implementation follows a clear arrange-act-assert pattern, with mock setup in @Before and specific test cases for each service type.

Framework features leveraged include:
  • MockitoJUnitRunner for automatic mock initialization
  • Mock annotations for clean dependency injection
  • Before setup for test context preparation

Technical Details

Testing tools and configuration:
  • JUnit 4 test framework
  • Mockito mocking framework
  • MockitoJUnitRunner for test execution
  • Mock objects for DiscoveryService and ServiceDTO list
  • ServiceController instance created per test

Best Practices Demonstrated

The test suite exemplifies several testing best practices for Java services. Each test method focuses on a single responsibility with clear naming and structure.

Notable practices include:
  • Proper test isolation through mocking
  • Consistent setup using @Before
  • Clear test method naming convention
  • Appropriate assertion usage
  • Clean separation of concerns

apolloconfig/apollo

apollo-configservice/src/test/java/com/ctrip/framework/apollo/metaservice/controller/ServiceControllerTest.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.metaservice.controller;

import static org.junit.Assert.*;
import static org.mockito.Mockito.when;

import com.ctrip.framework.apollo.core.ServiceNameConsts;
import com.ctrip.framework.apollo.core.dto.ServiceDTO;
import com.ctrip.framework.apollo.metaservice.service.DiscoveryService;
import java.util.List;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;

@RunWith(MockitoJUnitRunner.class)
public class ServiceControllerTest {

  @Mock
  private DiscoveryService discoveryService;

  @Mock
  private List<ServiceDTO> someServices;

  private ServiceController serviceController;

  @Before
  public void setUp() throws Exception {
    serviceController = new ServiceController(discoveryService);
  }

  @Test
  public void testGetMetaService() {
    assertTrue(serviceController.getMetaService().isEmpty());
  }

  @Test
  public void testGetConfigService() {
    String someAppId = "someAppId";
    String someClientIp = "someClientIp";

    when(discoveryService.getServiceInstances(ServiceNameConsts.APOLLO_CONFIGSERVICE))
        .thenReturn(someServices);

    assertEquals(someServices, serviceController.getConfigService(someAppId, someClientIp));
  }

  @Test
  public void testGetAdminService() {
    when(discoveryService.getServiceInstances(ServiceNameConsts.APOLLO_ADMINSERVICE))
        .thenReturn(someServices);

    assertEquals(someServices, serviceController.getAdminService());

  }
}