1 package com.lexicalscope.fluentreflection;
2
3 import static com.lexicalscope.fluentreflection.FluentReflection.method;
4 import static com.lexicalscope.fluentreflection.ReflectionMatchers.reflectingOn;
5 import static org.hamcrest.MatcherAssert.assertThat;
6 import static org.hamcrest.Matchers.*;
7
8 import org.junit.Rule;
9 import org.junit.Test;
10 import org.junit.rules.ExpectedException;
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28 public class TestBoundReflectedMethodImpl {
29 @Rule
30 public ExpectedException exception = ExpectedException.none();
31
32 static class ClassWithStaticMethod {
33 static void method() {
34 }
35 }
36
37 static class ClassWithMethod {
38 Integer method() {
39 return 42;
40 }
41
42 @Override
43 public String toString() {
44 return "my toString";
45 }
46 }
47
48 private final BoundFluentMethodImpl methodWithReturnType;
49
50 public TestBoundReflectedMethodImpl() throws SecurityException, NoSuchMethodException {
51 methodWithReturnType = new BoundFluentMethodImpl(
52 null,
53 method(ClassWithMethod.class.getDeclaredMethod("method")),
54 new ClassWithMethod());
55 }
56
57 @Test
58 public void bindingStaticMethodIsNotAllowed() throws Exception {
59 exception.expect(IllegalArgumentException.class);
60
61 new BoundFluentMethodImpl(
62 null,
63 method(ClassWithStaticMethod.class.getDeclaredMethods()[0]),
64 new ClassWithStaticMethod());
65 }
66
67 @Test
68 public void boundMethodToStringIsAllowed() throws Exception {
69 assertThat(
70 methodWithReturnType,
71 hasToString(containsString("method() in my toString")));
72 }
73
74 @Test
75 public void boundMethodReturnTypeIsAvailable() throws Exception {
76 assertThat(
77 methodWithReturnType.type(),
78 reflectingOn(Integer.class));
79 }
80 }