Skip to content

Commit

Permalink
Fix flaky test in DescriptionGenericBuilderTest by sorting for dete…
Browse files Browse the repository at this point in the history
…rministic iterations (#1547)

* Fixed flaky test by sorting the Non-Deterministic method getGenericExceptionTypes

* Fixed flakiness in tests ClassFileVersionOtherTest#testLatestVersion and MethodCallProxyTest#testNonGenericParameter

* Fixed flakiness caused by exception type ordering

* Removed flaky fix for testLatestVersion
  • Loading branch information
SaaiVenkat authored Nov 8, 2023
1 parent e997263 commit 9d6a587
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Type;
import java.util.Arrays;
import java.util.Comparator;

import static org.mockito.Mockito.mock;

Expand Down Expand Up @@ -35,7 +38,15 @@ protected TypeDescription.Generic describeParameterType(Method method, int index
}

protected TypeDescription.Generic describeExceptionType(Method method, int index) {
return TypeDefinition.Sort.describe(method.getGenericExceptionTypes()[index],
Type[] genericExceptionTypes = method.getGenericExceptionTypes();
Arrays.sort(genericExceptionTypes, new Comparator<Type>() {
@Override
public int compare(Type type, Type t1) {
return type.getTypeName().compareTo(t1.getTypeName());
}
});

return TypeDefinition.Sort.describe(genericExceptionTypes[index],
new TypeDescription.Generic.AnnotationReader.Delegator.ForLoadedExecutableExceptionType(method, index));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
import java.lang.reflect.Method;
import java.lang.reflect.Type;
import java.lang.reflect.WildcardType;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.sameInstance;
Expand Down Expand Up @@ -40,7 +42,15 @@ protected TypeDescription.Generic describeParameterType(Method method, int index
}

protected TypeDescription.Generic describeExceptionType(Method method, int index) {
return describe(method.getGenericExceptionTypes()[index], new TypeDescription.Generic.AnnotationReader.Delegator.ForLoadedExecutableExceptionType(method, index))
Type[] genericExceptionTypes = method.getGenericExceptionTypes();
Arrays.sort(genericExceptionTypes, new Comparator<Type>() {
@Override
public int compare(Type type, Type t1) {
return type.getTypeName().compareTo(t1.getTypeName());
}
});

return describe(genericExceptionTypes[index], new TypeDescription.Generic.AnnotationReader.Delegator.ForLoadedExecutableExceptionType(method, index))
.accept(TypeDescription.Generic.Visitor.Substitutor.ForAttachment.of(new MethodDescription.ForLoadedMethod(method)));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@
import org.junit.Test;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Type;
import java.util.Arrays;
import java.util.Comparator;
import java.util.concurrent.Callable;

import static org.hamcrest.CoreMatchers.is;
Expand Down Expand Up @@ -81,8 +84,15 @@ public void testNonGenericParameter() throws Exception {
Class<?> auxiliaryType = proxyOnlyDeclaredMethodOf(GenericType.class);
assertThat(auxiliaryType.getTypeParameters().length, is(0));
assertThat(auxiliaryType.getDeclaredMethod("call").getGenericReturnType(), is((Type) Object.class));
assertThat(auxiliaryType.getDeclaredFields()[1].getGenericType(), is((Type) Object.class));
assertThat(auxiliaryType.getDeclaredFields()[2].getGenericType(), is((Type) Number.class));
Field[] declaredFields = auxiliaryType.getDeclaredFields();
Arrays.sort(declaredFields, new Comparator<Field>() {
@Override
public int compare(Field field1, Field field2) {
return field1.getName().compareTo(field2.getName());
}
});
assertThat(declaredFields[1].getGenericType(), is((Type) Object.class));
assertThat(declaredFields[2].getGenericType(), is((Type) Number.class));
}

@Test
Expand Down

0 comments on commit 9d6a587

Please sign in to comment.