diff --git a/src/test/java/spoon/test/factory/MethodFactoryTest.java b/src/test/java/spoon/test/factory/MethodFactoryTest.java new file mode 100644 index 00000000000..f7147235f43 --- /dev/null +++ b/src/test/java/spoon/test/factory/MethodFactoryTest.java @@ -0,0 +1,61 @@ +package spoon.test.factory; + +import org.junit.jupiter.api.Test; +import spoon.Launcher; +import spoon.reflect.declaration.CtClass; +import spoon.reflect.declaration.CtMethod; +import spoon.reflect.factory.Factory; +import spoon.reflect.factory.MethodFactory; +import spoon.reflect.reference.CtExecutableReference; +import spoon.reflect.reference.CtTypeReference; +import spoon.test.factory.testclasses4.Bar; + +import java.lang.reflect.Method; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.CoreMatchers.is; + +public class MethodFactoryTest { + + @Test + public void testCreateReference() { + // contract: createReference creates a method reference of the foo method + + Factory factory = new Launcher().getFactory(); + CtClass testClass = factory.Class().get(Bar.class); + CtMethod foo = testClass.getMethodsByName("foo").get(0); + CtExecutableReference expectedReference = testClass.getMethod("foo").getReference(); + MethodFactory methodFactory = testClass.getFactory().Method(); + CtExecutableReference actualCreatedReference = null; + + actualCreatedReference = methodFactory.createReference(foo); + + assertThat(actualCreatedReference, is(expectedReference)); + } + + @Test + public void testCreateReferenceWithActualMethod() throws ClassNotFoundException, NoSuchMethodException { + // contract: createReference creates a method reference of a actual method foo + + // arrange + Launcher launcher = new Launcher(); + Factory factory = launcher.getFactory(); + Class testClass = Class.forName("spoon.test.factory.testclasses4.Bar"); + Method testMethod = testClass.getMethod("foo"); + + CtExecutableReference expectedReference = factory.createExecutableReference(); + expectedReference.setSimpleName("foo"); + CtTypeReference ctTypeReference = factory.Type().createReference(Bar.class); + expectedReference.setDeclaringType(ctTypeReference); + expectedReference.setType(launcher.getFactory().Type().voidPrimitiveType()); + + MethodFactory methodFactory = factory.Method(); + CtExecutableReference actualCreatedReference = null; + + // act + actualCreatedReference = methodFactory.createReference(testMethod); + + // assert + assertThat(actualCreatedReference, is(expectedReference)); + } +} \ No newline at end of file diff --git a/src/test/java/spoon/test/factory/testclasses4/Bar.java b/src/test/java/spoon/test/factory/testclasses4/Bar.java new file mode 100644 index 00000000000..b3d6520a052 --- /dev/null +++ b/src/test/java/spoon/test/factory/testclasses4/Bar.java @@ -0,0 +1,6 @@ +package spoon.test.factory.testclasses4; + +public class Bar { + + public void foo() { } +} \ No newline at end of file