Back to Repositories

Testing Nacos Service Discovery Implementation in Apollo Config

This test suite validates the NacosDiscoveryService implementation in Apollo’s configuration service, focusing on service instance discovery and retrieval functionality. The tests verify the integration between Apollo and Nacos service registry, ensuring proper handling of service instances and their metadata.

Test Coverage Overview

The test suite provides comprehensive coverage of the NacosDiscoveryService functionality.

Key areas tested include:
  • Empty instance handling
  • Invalid service ID scenarios
  • Successful service instance retrieval and mapping
  • Service instance metadata validation
Integration points focus on the interaction between Apollo’s ServiceDTO and Nacos Instance objects.

Implementation Analysis

The testing approach utilizes Mockito for service dependency isolation and JUnit for test execution. The implementation follows a clear pattern of arranging test data, mocking external services, and verifying response mappings.

Framework-specific features include @RunWith(MockitoJUnitRunner.class) for Mockito integration and @Mock for automatic mock injection.

Technical Details

Testing tools and configuration:
  • JUnit 4 for test execution
  • Mockito for mocking NamingService
  • Custom mock helpers for Instance creation
  • setUp() method for test initialization
  • Integration with Nacos API objects

Best Practices Demonstrated

The test suite exemplifies several testing best practices including proper test isolation, clear test method naming, and comprehensive assertion coverage.

Notable practices include:
  • Separate test cases for different scenarios
  • Proper mock setup and verification
  • Clear separation of concerns
  • Thorough edge case testing

apolloconfig/apollo

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

import com.alibaba.nacos.api.naming.NamingService;
import com.alibaba.nacos.api.naming.pojo.Instance;
import com.ctrip.framework.apollo.core.dto.ServiceDTO;
import com.google.common.collect.Lists;
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.List;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

/**
 * @author kl (http://kailing.pub)
 * @since 2020/12/21
 */
@RunWith(MockitoJUnitRunner.class)
public class NacosDiscoveryServiceTest {

    private NacosDiscoveryService nacosDiscoveryService;

    @Mock
    private NamingService nacosNamingService;

    private String someServiceId;


    @Before
    public void setUp() throws Exception {
        nacosDiscoveryService = new NacosDiscoveryService();
        nacosDiscoveryService.setNamingService(nacosNamingService);
        someServiceId = "someServiceId";
    }

    @Test
    public void testGetServiceInstancesWithEmptyInstances() throws Exception {
        assertTrue(nacosNamingService.selectInstances(someServiceId, true).isEmpty());
    }


    @Test
    public void testGetServiceInstancesWithInvalidServiceId() {
        assertTrue(nacosDiscoveryService.getServiceInstances(someServiceId).isEmpty());
    }

    @Test
    public void testGetServiceInstances() throws Exception {
        String someIp = "1.2.3.4";
        int somePort = 8080;
        String someInstanceId = "someInstanceId";
        Instance someServiceInstance = mockServiceInstance(someInstanceId, someIp, somePort);

        when(nacosNamingService.selectInstances(someServiceId, true)).thenReturn(
                Lists.newArrayList(someServiceInstance));

        List<ServiceDTO> serviceDTOList = nacosDiscoveryService.getServiceInstances(someServiceId);
        ServiceDTO serviceDTO = serviceDTOList.get(0);
        assertEquals(1, serviceDTOList.size());
        assertEquals(someServiceId, serviceDTO.getAppName());
        assertEquals("http://1.2.3.4:8080/", serviceDTO.getHomepageUrl());

    }

    private Instance mockServiceInstance(String instanceId, String ip, int port) {
        Instance serviceInstance = mock(Instance.class);
        when(serviceInstance.getInstanceId()).thenReturn(instanceId);
        when(serviceInstance.getIp()).thenReturn(ip);
        when(serviceInstance.getPort()).thenReturn(port);

        return serviceInstance;
    }

}