From 1e2401c294b68ba5c05f1623fd1a96d26968f985 Mon Sep 17 00:00:00 2001 From: Peter Gafert Date: Sun, 3 Nov 2019 16:54:50 +0100 Subject: [PATCH] Review: Improve test readability Issue: #236 --- .../core/importer/ClassFileImporterTest.java | 39 +++++++++---------- 1 file changed, 19 insertions(+), 20 deletions(-) diff --git a/archunit/src/test/java/com/tngtech/archunit/core/importer/ClassFileImporterTest.java b/archunit/src/test/java/com/tngtech/archunit/core/importer/ClassFileImporterTest.java index 2c507718f4..1c39539c03 100644 --- a/archunit/src/test/java/com/tngtech/archunit/core/importer/ClassFileImporterTest.java +++ b/archunit/src/test/java/com/tngtech/archunit/core/importer/ClassFileImporterTest.java @@ -178,6 +178,7 @@ import com.tngtech.java.junit.dataprovider.UseDataProvider; import org.apache.logging.log4j.Level; import org.assertj.core.api.Condition; +import org.assertj.core.util.Objects; import org.junit.After; import org.junit.Rule; import org.junit.Test; @@ -663,18 +664,14 @@ public void imports_complex_method_with_correct_parameters() throws Exception { JavaClass clazz = classesIn("testexamples/complexmethodimport").get(ClassWithComplexMethod.class); assertThat(clazz.getMethods()).as("Methods of %s", ClassWithComplexMethod.class.getSimpleName()).hasSize(1); - assertThat(clazz.getMethod("complex", String.class, long.class, long.class, Serializable.class, Serializable.class)) - .isEquivalentTo(ClassWithComplexMethod.class.getDeclaredMethod( - "complex", String.class, long.class, long.class, Serializable.class, Serializable.class)); - assertThat(clazz.tryGetMethod("complex", String.class, long.class, long.class, Serializable.class, Serializable.class).get()) - .isEquivalentTo(ClassWithComplexMethod.class.getDeclaredMethod( - "complex", String.class, long.class, long.class, Serializable.class, Serializable.class)); - assertThat(clazz.getMethod("complex", "java.lang.String", "long", "long", "java.io.Serializable", "java.io.Serializable")) - .isEquivalentTo(ClassWithComplexMethod.class.getDeclaredMethod( - "complex", String.class, long.class, long.class, Serializable.class, Serializable.class)); - assertThat(clazz.tryGetMethod("complex", "java.lang.String", "long", "long", "java.io.Serializable", "java.io.Serializable").get()) - .isEquivalentTo(ClassWithComplexMethod.class.getDeclaredMethod( - "complex", String.class, long.class, long.class, Serializable.class, Serializable.class)); + + Class[] parameterTypes = {String.class, long.class, long.class, Serializable.class, Serializable.class}; + Method expectedMethod = ClassWithComplexMethod.class.getDeclaredMethod("complex", parameterTypes); + + assertThat(clazz.getMethod("complex", parameterTypes)).isEquivalentTo(expectedMethod); + assertThat(clazz.tryGetMethod("complex", parameterTypes).get()).isEquivalentTo(expectedMethod); + assertThat(clazz.getMethod("complex", Objects.namesOf(parameterTypes))).isEquivalentTo(expectedMethod); + assertThat(clazz.tryGetMethod("complex", Objects.namesOf(parameterTypes)).get()).isEquivalentTo(expectedMethod); } @Test @@ -898,14 +895,16 @@ public void imports_simple_constructors_with_correct_parameters() throws Excepti assertThat(clazz.getConstructors()).as("Constructors").hasSize(3); assertThat(clazz.getConstructor()).isEquivalentTo(ClassWithSimpleConstructors.class.getDeclaredConstructor()); - assertThat(clazz.getConstructor(Object.class)) - .isEquivalentTo(ClassWithSimpleConstructors.class.getDeclaredConstructor(Object.class)); - assertThat(clazz.getConstructor("java.lang.Object")) - .isEquivalentTo(ClassWithSimpleConstructors.class.getDeclaredConstructor(Object.class)); - assertThat(clazz.getConstructor(int.class, int.class)) - .isEquivalentTo(ClassWithSimpleConstructors.class.getDeclaredConstructor(int.class, int.class)); - assertThat(clazz.getConstructor("int", "int")) - .isEquivalentTo(ClassWithSimpleConstructors.class.getDeclaredConstructor(int.class, int.class)); + + Class[] parameterTypes = {Object.class}; + Constructor expectedConstructor = ClassWithSimpleConstructors.class.getDeclaredConstructor(parameterTypes); + assertThat(clazz.getConstructor(parameterTypes)).isEquivalentTo(expectedConstructor); + assertThat(clazz.getConstructor(Objects.namesOf(parameterTypes))).isEquivalentTo(expectedConstructor); + + parameterTypes = new Class[]{int.class, int.class}; + expectedConstructor = ClassWithSimpleConstructors.class.getDeclaredConstructor(parameterTypes); + assertThat(clazz.getConstructor(parameterTypes)).isEquivalentTo(expectedConstructor); + assertThat(clazz.getConstructor(Objects.namesOf(parameterTypes))).isEquivalentTo(expectedConstructor); } @Test