Skip to content

Commit

Permalink
Fixed flaky tests due to class.getMethods (#1525)
Browse files Browse the repository at this point in the history
* Fixed flaky tests due to `class.getMethod`

* Simplify Comparator code
  • Loading branch information
Augustine-C authored Oct 26, 2021
1 parent 0a01ee9 commit 9a5bc93
Showing 1 changed file with 26 additions and 17 deletions.
43 changes: 26 additions & 17 deletions core/src/test/java/feign/TypesResolveReturnTypeTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.lang.reflect.TypeVariable;
import java.util.Arrays;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand All @@ -39,6 +41,13 @@ interface OverridingConcreteSimple extends Simple {
String get();
}

public Method[] getMethods(Class<?> c) {
Method[] methods = c.getMethods();
Arrays.sort(methods,
Comparator.comparing(o -> (o.getName() + o.getGenericReturnType().getTypeName())));
return methods;
}

@Test
public void simple() {
Method[] methods = Simple.class.getMethods();
Expand Down Expand Up @@ -159,7 +168,7 @@ public void concreteSimpleClassGenericSecondLevel() {

@Test
public void overridingConcreteSimpleClassGenericSecondLevel() {
Method[] methods = OverridingConcreteSimpleClassGenericSecondLevel.class.getMethods();
Method[] methods = getMethods(OverridingConcreteSimpleClassGenericSecondLevel.class);
Assertions.assertThat(methods.length).isEqualTo(2);
Type resolved = Types.resolve(OverridingConcreteSimpleClassGenericSecondLevel.class,
OverridingConcreteSimpleClassGenericSecondLevel.class, methods[0].getGenericReturnType());
Expand Down Expand Up @@ -221,7 +230,7 @@ public void concreteSimpleClassMultipleGenericSecondLevel() {

@Test
public void overridingConcreteSimpleClassMultipleGenericSecondLevel() {
Method[] methods = OverridingConcreteSimpleClassMultipleGenericSecondLevel.class.getMethods();
Method[] methods = getMethods(OverridingConcreteSimpleClassMultipleGenericSecondLevel.class);
Assertions.assertThat(methods.length).isEqualTo(2);
Type resolved =
Types.resolve(OverridingConcreteSimpleClassMultipleGenericSecondLevel.class,
Expand Down Expand Up @@ -255,7 +264,7 @@ public void realizingSimpleClassGenericThirdLevel() {

@Test
public void realizingSimpleClassMultipleGenericThirdLevel() {
Method[] methods = RealizingSimpleClassMultipleGenericThirdLevel.class.getMethods();
Method[] methods = getMethods(RealizingSimpleClassMultipleGenericThirdLevel.class);
Assertions.assertThat(methods.length).isEqualTo(2);
Type resolved = Types.resolve(RealizingSimpleClassMultipleGenericThirdLevel.class,
RealizingSimpleClassMultipleGenericThirdLevel.class, methods[0].getGenericReturnType());
Expand All @@ -269,7 +278,7 @@ public void realizingSimpleClassMultipleGenericThirdLevel() {

@Test
public void realizingOverridingSimpleClassGenericThirdLevel() {
Method[] methods = RealizingOverridingSimpleClassGenericThirdLevel.class.getMethods();
Method[] methods = getMethods(RealizingOverridingSimpleClassGenericThirdLevel.class);
Assertions.assertThat(methods.length).isEqualTo(2);
Type resolved = Types.resolve(RealizingOverridingSimpleClassGenericThirdLevel.class,
RealizingOverridingSimpleClassGenericThirdLevel.class, methods[0].getGenericReturnType());
Expand All @@ -283,7 +292,7 @@ public void realizingOverridingSimpleClassGenericThirdLevel() {

@Test
public void realizingOverridingSimpleClassMultipleGenericThirdLevel() {
Method[] methods = RealizingOverridingSimpleClassMultipleGenericThirdLevel.class.getMethods();
Method[] methods = getMethods(RealizingOverridingSimpleClassMultipleGenericThirdLevel.class);
Assertions.assertThat(methods.length).isEqualTo(2);
Type resolved =
Types.resolve(RealizingOverridingSimpleClassMultipleGenericThirdLevel.class,
Expand Down Expand Up @@ -354,7 +363,7 @@ public void concreteSimpleClassGenericThirdLevel() {

@Test
public void OverridingConcreteSimpleClassGenericThirdLevel() {
Method[] methods = OverridingConcreteSimpleClassGenericThirdLevel.class.getMethods();
Method[] methods = getMethods(OverridingConcreteSimpleClassGenericThirdLevel.class);
Assertions.assertThat(methods.length).isEqualTo(2);
Type resolved = Types.resolve(OverridingConcreteSimpleClassGenericThirdLevel.class,
OverridingConcreteSimpleClassGenericThirdLevel.class, methods[0].getGenericReturnType());
Expand Down Expand Up @@ -452,31 +461,31 @@ public void concreteCollectionGenericFourthLevel() {

@Test
public void overridingConcreteCollectionGenericFourthLevel() {
Method[] methods = OverridingConcreteCollectionGenericFourthLevel.class.getMethods();
Method[] methods = getMethods(OverridingConcreteCollectionGenericFourthLevel.class);
Assertions.assertThat(methods.length).isEqualTo(2);
Type resolved = Types.resolve(OverridingConcreteCollectionGenericFourthLevel.class,
OverridingConcreteCollectionGenericFourthLevel.class, methods[0].getGenericReturnType());
OverridingConcreteCollectionGenericFourthLevel.class, methods[1].getGenericReturnType());
Assertions.assertThat(resolved instanceof ParameterizedType).isTrue();
Type resolved2 = Types.resolve(OverridingConcreteCollectionGenericFourthLevel.class,
OverridingConcreteCollectionGenericFourthLevel.class, methods[1].getGenericReturnType());
OverridingConcreteCollectionGenericFourthLevel.class, methods[0].getGenericReturnType());
Assertions.assertThat(resolved2).isEqualTo(Object.class);
Type resolvedType = Types.resolveReturnType(resolved, resolved2);
Assertions.assertThat(resolvedType).isEqualTo(resolved);
}

@Test
public void overrideOverridingConcreteCollectionGenericFourthLevel() {
Method[] methods = OverrideOverridingConcreteCollectionGenericFourthLevel.class.getMethods();
Method[] methods = getMethods(OverrideOverridingConcreteCollectionGenericFourthLevel.class);
Assertions.assertThat(methods.length).isEqualTo(2);
Type resolved =
Types.resolve(OverrideOverridingConcreteCollectionGenericFourthLevel.class,
OverrideOverridingConcreteCollectionGenericFourthLevel.class,
methods[0].getGenericReturnType());
methods[1].getGenericReturnType());
Assertions.assertThat(resolved instanceof ParameterizedType).isTrue();
Type resolved2 =
Types.resolve(OverrideOverridingConcreteCollectionGenericFourthLevel.class,
OverrideOverridingConcreteCollectionGenericFourthLevel.class,
methods[1].getGenericReturnType());
methods[0].getGenericReturnType());
Assertions.assertThat(resolved2).isEqualTo(Object.class);
Type resolvedType = Types.resolveReturnType(resolved, resolved2);
Assertions.assertThat(resolvedType).isEqualTo(resolved);
Expand Down Expand Up @@ -505,20 +514,20 @@ public void concreteGenericCollectionGenericFifthLevel() {

@Test
public void overridingConcreteGenericCollectionGenericFifthLevel() {
Method[] methods = OverridingConcreteGenericCollectionGenericFifthLevel.class.getMethods();
Method[] methods = getMethods(OverridingConcreteGenericCollectionGenericFifthLevel.class);
Assertions.assertThat(methods.length).isEqualTo(2);
Type resolved =
Types.resolve(OverridingConcreteGenericCollectionGenericFifthLevel.class,
OverridingConcreteGenericCollectionGenericFifthLevel.class,
methods[0].getGenericReturnType());
methods[1].getGenericReturnType());
Assertions.assertThat(resolved instanceof ParameterizedType).isTrue();
Type resolved2 =
Types.resolve(OverridingConcreteGenericCollectionGenericFifthLevel.class,
OverridingConcreteGenericCollectionGenericFifthLevel.class,
methods[1].getGenericReturnType());
methods[0].getGenericReturnType());
Assertions.assertThat(resolved2).isEqualTo(Object.class);
Type resolvedType = Types.resolveReturnType(methods[0].getGenericReturnType(),
methods[1].getGenericReturnType());
Type resolvedType = Types.resolveReturnType(methods[1].getGenericReturnType(),
methods[0].getGenericReturnType());
Assertions.assertThat(resolvedType).isEqualTo(resolved);
}

Expand Down

0 comments on commit 9a5bc93

Please sign in to comment.