Back to Repositories

Testing Sentinel Degrade Rule Implementation in Spring Cloud Alibaba

This test suite validates the degrade rule functionality in Spring Cloud Alibaba’s Sentinel component, focusing on fallback behavior when service degradation occurs. The tests ensure proper implementation of circuit breaking patterns and fallback mechanisms in a Spring Boot environment.

Test Coverage Overview

The test coverage focuses on Sentinel’s degrade rule implementation and fallback behavior verification.

Key areas tested include:
  • HTTP endpoint degradation
  • Fallback response validation
  • Integration with Spring Boot’s web environment
  • Dynamic port allocation handling

Implementation Analysis

The testing approach utilizes Spring Boot’s test framework with TestRestTemplate for HTTP endpoint verification. The implementation leverages Spring Boot’s random port allocation and dependency injection patterns to ensure isolated testing environments.

Notable patterns include:
  • RestTemplate-based HTTP testing
  • Automated port management
  • Assertion-based response validation

Technical Details

Testing tools and configuration:
  • JUnit Jupiter for test execution
  • Spring Boot Test framework
  • TestRestTemplate for HTTP calls
  • AssertJ for assertions
  • Random port configuration for test isolation
  • SpringBootTest annotation with web environment configuration

Best Practices Demonstrated

The test suite exemplifies several testing best practices in Spring Cloud applications.

Notable practices include:
  • Isolated test environment using random ports
  • Dependency injection for test components
  • Clear assertion messages
  • Focused test methods
  • Proper separation of concerns

alibaba/spring-cloud-alibaba

spring-cloud-alibaba-tests/sentinel-tests/sentinel-degrade-test/src/test/java/com/alibaba/cloud/tests/sentinel/degrade/SentinelDegradeTestAppTest.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.tests.sentinel.degrade;

import org.junit.jupiter.api.Test;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.boot.test.web.server.LocalServerPort;
import org.springframework.http.ResponseEntity;

import static org.assertj.core.api.Assertions.assertThat;
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT;

@SpringBootTest(webEnvironment = RANDOM_PORT)
class SentinelDegradeTestAppTest {

	@LocalServerPort
	int port;

	@Autowired
	TestRestTemplate rest;

	@Test
	public void testDegradeRule() {
		ResponseEntity<String> res = rest
				.getForEntity("http://localhost:" + port + "/degrade", String.class);

		assertThat(res.getBody()).contains("fallback");
	}

}