Back to Repositories

Testing Default Fallback Mechanisms for Hystrix Commands in Netflix/Hystrix

This test suite validates the default fallback functionality in Netflix Hystrix when used with Spring AOP and CGLIB. It extends BasicDefaultFallbackTest to verify both standard service fallbacks and command-specific fallbacks in a Spring context.

Test Coverage Overview

The test suite provides comprehensive coverage of Hystrix’s default fallback mechanisms within a Spring environment.

  • Tests service-level default fallbacks
  • Validates command-specific fallback behavior
  • Ensures proper Spring bean autowiring and configuration
  • Verifies AOP integration with CGLIB proxies

Implementation Analysis

The implementation leverages Spring’s testing framework with JUnit integration to validate Hystrix fallback patterns.

Key technical aspects include:
  • Uses SpringJUnit4ClassRunner for test execution
  • Implements context configuration with AopCglibConfig
  • Extends BasicDefaultFallbackTest for core fallback scenarios
  • Utilizes Spring’s dependency injection for service components

Technical Details

  • Spring Test Context framework
  • JUnit test runner
  • CGLIB-based AOP configuration
  • @Configurable annotation for bean definitions
  • @ContextConfiguration for test context setup
  • @Autowired for dependency injection

Best Practices Demonstrated

The test class exemplifies several testing best practices for Spring-based Hystrix applications.

  • Clean separation of configuration and test logic
  • Proper use of Spring’s testing infrastructure
  • Effective inheritance pattern for reusing test scenarios
  • Clear bean configuration through Java-based configuration

netflix/hystrix

hystrix-contrib/hystrix-javanica/src/test/java/com/netflix/hystrix/contrib/javanica/test/spring/fallback/DefaultFallbackTest.java

            
package com.netflix.hystrix.contrib.javanica.test.spring.fallback;

import com.netflix.hystrix.contrib.javanica.test.common.fallback.BasicDefaultFallbackTest;
import com.netflix.hystrix.contrib.javanica.test.spring.conf.AopCglibConfig;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.context.annotation.Bean;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

/**
 * Created by dmgcodevil.
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = {AopCglibConfig.class, DefaultFallbackTest.Config.class})
public class DefaultFallbackTest extends BasicDefaultFallbackTest {

    @Autowired
    private ServiceWithDefaultFallback serviceWithDefaultFallback;
    @Autowired
    private ServiceWithDefaultCommandFallback serviceWithDefaultCommandFallback;


    @Override
    protected ServiceWithDefaultFallback createServiceWithDefaultFallback() {
        return serviceWithDefaultFallback;
    }

    @Override
    protected ServiceWithDefaultCommandFallback serviceWithDefaultCommandFallback() {
        return serviceWithDefaultCommandFallback;
    }

    @Configurable
    public static class Config {
        @Bean
        public ServiceWithDefaultFallback serviceWithDefaultFallback() {
            return new ServiceWithDefaultFallback();
        }

        @Bean
        public ServiceWithDefaultCommandFallback serviceWithDefaultCommandFallback() {
            return new ServiceWithDefaultCommandFallback();
        }
    }
}