1
+ package spoon .test .factory ;
2
+
3
+ import org .junit .jupiter .api .Test ;
4
+ import spoon .Launcher ;
5
+ import spoon .reflect .declaration .CtClass ;
6
+ import spoon .reflect .declaration .CtMethod ;
7
+ import spoon .reflect .factory .Factory ;
8
+ import spoon .reflect .factory .MethodFactory ;
9
+ import spoon .reflect .reference .CtExecutableReference ;
10
+ import spoon .reflect .reference .CtTypeReference ;
11
+ import spoon .test .factory .testclasses4 .Bar ;
12
+
13
+ import java .lang .reflect .Method ;
14
+
15
+ import static org .hamcrest .MatcherAssert .assertThat ;
16
+ import static org .hamcrest .CoreMatchers .is ;
17
+
18
+ public class MethodFactoryTest {
19
+
20
+ @ Test
21
+ public void testCreateReference () {
22
+ // contract: createReference creates a method reference of the foo method
23
+
24
+ Factory factory = new Launcher ().getFactory ();
25
+ CtClass <?> testClass = factory .Class ().get (Bar .class );
26
+ CtMethod <?> foo = testClass .getMethodsByName ("foo" ).get (0 );
27
+ CtExecutableReference <?> expectedReference = testClass .getMethod ("foo" ).getReference ();
28
+ MethodFactory methodFactory = testClass .getFactory ().Method ();
29
+ CtExecutableReference <?> actualCreatedReference = null ;
30
+
31
+ actualCreatedReference = methodFactory .createReference (foo );
32
+
33
+ assertThat (actualCreatedReference , is (expectedReference ));
34
+ }
35
+
36
+ @ Test
37
+ public void testCreateReferenceWithActualMethod () throws ClassNotFoundException , NoSuchMethodException {
38
+ // contract: createReference creates a method reference of a actual method foo
39
+
40
+ // arrange
41
+ Launcher launcher = new Launcher ();
42
+ Factory factory = launcher .getFactory ();
43
+ Class <?> testClass = Class .forName ("spoon.test.factory.testclasses4.Bar" );
44
+ Method testMethod = testClass .getMethod ("foo" );
45
+
46
+ CtExecutableReference <Void > expectedReference = factory .createExecutableReference ();
47
+ expectedReference .setSimpleName ("foo" );
48
+ CtTypeReference <?> ctTypeReference = factory .Type ().createReference (Bar .class );
49
+ expectedReference .setDeclaringType (ctTypeReference );
50
+ expectedReference .setType (launcher .getFactory ().Type ().voidPrimitiveType ());
51
+
52
+ MethodFactory methodFactory = factory .Method ();
53
+ CtExecutableReference <?> actualCreatedReference = null ;
54
+
55
+ // act
56
+ actualCreatedReference = methodFactory .createReference (testMethod );
57
+
58
+ // assert
59
+ assertThat (actualCreatedReference , is (expectedReference ));
60
+ }
61
+ }
0 commit comments