Back to Repositories

Testing Method Resolution Inheritance Patterns in Netflix Hystrix

This test suite validates the method reflection functionality in Hystrix’s Java implementation, specifically testing the MethodProvider utility for retrieving methods from class hierarchies. The tests use parameterized testing to verify method resolution across inherited classes.

Test Coverage Overview

The test suite provides comprehensive coverage of method resolution in class inheritance hierarchies.

Key areas tested include:
  • Method lookup across multiple inheritance levels
  • Parameter type matching for method resolution
  • Handling of different method signatures
  • Verification of Optional return types

Implementation Analysis

The testing approach utilizes JUnit’s parameterized testing framework to validate method resolution across different scenarios. The implementation leverages Guava’s Optional for null-safe method handling and employs a clear inheritance chain (A → B → C) to verify proper method lookup behavior.

Technical patterns include:
  • Parameterized test data generation
  • Class hierarchy testing
  • Reflection-based method validation

Technical Details

Testing tools and configuration:
  • JUnit 4 test framework
  • Parameterized test runner
  • Guava Optional for null safety
  • Custom MethodProvider utility class
  • Static test classes for inheritance verification

Best Practices Demonstrated

The test suite exemplifies several testing best practices in Java reflection testing.

Notable practices include:
  • Parameterized test data for multiple scenarios
  • Clear test class hierarchy setup
  • Explicit parameter type checking
  • Proper assertion usage for validation
  • Clean separation of test data and test logic

netflix/hystrix

hystrix-contrib/hystrix-javanica/src/test/java/com/netflix/hystrix/contrib/javanica/util/GetMethodTest.java

            
package com.netflix.hystrix.contrib.javanica.util;

import com.google.common.base.Optional;
import com.netflix.hystrix.contrib.javanica.utils.MethodProvider;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;

import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.Collection;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

/**
 * Created by dmgcodevil.
 */
@RunWith(Parameterized.class)
public class GetMethodTest {

    private String methodName;
    private Class<?>[] parametersTypes;

    @Parameterized.Parameters
    public static Collection<Object[]> data() {
        return Arrays.asList(new Object[][] {
                { "foo", new Class<?>[]{ String.class } },
                { "bar", new Class<?>[]{ Integer.class } }
        });
    }

    public GetMethodTest(String methodName, Class<?>[] parametersTypes) {
        this.methodName = methodName;
        this.parametersTypes = parametersTypes;
    }

    @Test
    public void testGetMethodFoo(){
       Optional<Method> method =  MethodProvider.getInstance().getMethod(C.class, methodName, parametersTypes);

        assertTrue(method.isPresent());
        assertEquals(methodName, method.get().getName());
    }


    public static class A { void foo(String  in) {} }
    public static class B extends A { void bar(Integer in) {} }
    public static class C extends B{ }

}