Back to Repositories

Testing Sentinel Fallback Support for Feign Clients in Spring Cloud Alibaba

This test suite validates the Sentinel fallback support functionality in Spring Cloud Alibaba, specifically focusing on the integration between Feign clients and Sentinel fallback mechanisms. It ensures proper handling of fallback responses from both regular beans and FactoryBean implementations.

Test Coverage Overview

The test suite provides comprehensive coverage of Sentinel’s fallback support functionality.

Key areas tested include:
  • Fallback handling for standard Feign clients
  • Fallback support through FactoryBean implementations
  • Integration between Feign client annotations and Sentinel fallback configurations
  • Verification of fallback message delivery

Implementation Analysis

The testing approach utilizes Spring Boot’s ApplicationContextRunner for simulating application context scenarios.

Key implementation patterns include:
  • Mock Feign client interfaces with defined fallback classes
  • Configuration of Sentinel through property values
  • Use of EnableFeignClients annotation for client registration
  • Implementation of both standard and FactoryBean-based fallback mechanisms

Technical Details

Testing infrastructure includes:
  • JUnit Jupiter for test execution
  • Spring Boot test context runner
  • Feign client configuration with Sentinel integration
  • AssertJ for assertions
  • Mock URL endpoints for Feign clients

Best Practices Demonstrated

The test suite exemplifies several testing best practices:

  • Clear separation of test configuration and test cases
  • Proper use of Spring Boot test utilities
  • Consistent fallback message constants
  • Clean interface definitions with appropriate annotations
  • Comprehensive verification of multiple fallback scenarios

alibaba/spring-cloud-alibaba

spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-sentinel/src/test/java/com/alibaba/cloud/sentinel/SentinelFallbackSupportFactoryBeanTests.java

            
/*
 * Copyright 2013-2023 the original author or 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
 *
 *      https://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.alibaba.cloud.sentinel;

import org.junit.jupiter.api.Test;

import org.springframework.beans.factory.FactoryBean;
import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.cloud.openfeign.FeignAutoConfiguration;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.GetMapping;

import static org.assertj.core.api.Assertions.assertThat;

/**
 * @author 黄学敏(huangxuemin)
 */
public class SentinelFallbackSupportFactoryBeanTests {

	private static final String FACTORY_BEAN_FALLBACK_MESSAGE = "factoryBean fallback message";

	private static final String ORIGINAL_FALLBACK_MESSAGE = "OriginalFeign fallback message";

	private final ApplicationContextRunner runner = new ApplicationContextRunner()
			.withBean(FactoryBeanFallbackFeignFallback.class)
			.withBean(OriginalFeignFallback.class)
			.withConfiguration(AutoConfigurations.of(TestConfiguration.class, FeignAutoConfiguration.class))
			.withPropertyValues("feign.sentinel.enabled=true");

	@Test
	public void shouldRunFallbackFromBeanOrFactoryBean() {
		runner.run(ctx -> {
			assertThat(ctx.getBean(OriginalFeign.class).get()).isEqualTo(ORIGINAL_FALLBACK_MESSAGE);
			assertThat(ctx.getBean(FactoryBeanFallbackFeign.class).get()).isEqualTo(FACTORY_BEAN_FALLBACK_MESSAGE);
		});
	}

	@Configuration(proxyBeanMethods = false)
	@EnableFeignClients(clients = {OriginalFeign.class, FactoryBeanFallbackFeign.class })
	@EnableAutoConfiguration
	public static class TestConfiguration {

	}

	@FeignClient(name = "original", url = "https://original", fallback = OriginalFeignFallback.class)
	interface OriginalFeign {

		@GetMapping("/")
		String get();

	}

	@FeignClient(name = "factoryBean", url = "https://factoryBean", fallback = FactoryBeanFallbackFeignFallback.class)
	interface FactoryBeanFallbackFeign {

		@GetMapping("/")
		String get();

	}

	private static class FactoryBeanFallbackFeignFallback implements FactoryBean<FactoryBeanFallbackFeign> {

		@Override
		public FactoryBeanFallbackFeign getObject() {
			return () -> FACTORY_BEAN_FALLBACK_MESSAGE;
		}

		@Override
		public Class<?> getObjectType() {
			return FactoryBeanFallbackFeign.class;
		}

	}

	private static class OriginalFeignFallback implements OriginalFeign {

		@Override
		public String get() {
			return ORIGINAL_FALLBACK_MESSAGE;
		}

	}

}