|
1 |
| -/* |
2 |
| - * Copyright 2009 SpringSource Inc. |
3 |
| - * |
4 |
| - * Licensed under the Apache License, Version 2.0 (the "License"); |
5 |
| - * you may not use this file except in compliance with the License. |
6 |
| - * You may obtain a copy of the License at |
7 |
| - * |
8 |
| - * http://www.apache.org/licenses/LICENSE-2.0 |
9 |
| - * |
10 |
| - * Unless required by applicable law or agreed to in writing, software |
11 |
| - * distributed under the License is distributed on an "AS IS" BASIS, |
12 |
| - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
13 |
| - * See the License for the specific language governing permissions and |
14 |
| - * limitations under the License. |
15 |
| - */ |
16 |
| - |
17 |
| -package org.springframework.mock.staticmock; |
18 |
| - |
19 |
| -import javax.persistence.PersistenceException; |
20 |
| - |
21 |
| -import junit.framework.Assert; |
22 |
| - |
23 |
| -import org.junit.Test; |
24 |
| -import org.junit.runner.RunWith; |
25 |
| -import org.junit.runners.JUnit4; |
26 |
| - |
27 |
| -import static org.springframework.mock.staticmock.AnnotationDrivenStaticEntityMockingControl.*; |
28 |
| - |
29 |
| - |
30 |
| -/** |
31 |
| - * Test for static entity mocking framework. |
32 |
| - * @author Rod Johnson |
33 |
| - * @author Ramnivas Laddad |
34 |
| - * |
35 |
| - */ |
36 |
| -@MockStaticEntityMethods |
37 |
| -@RunWith(JUnit4.class) |
38 |
| -public class AnnotationDrivenStaticEntityMockingControlTest { |
39 |
| - |
40 |
| - @Test |
41 |
| - public void testNoArgIntReturn() { |
42 |
| - int expectedCount = 13; |
43 |
| - Person.countPeople(); |
44 |
| - expectReturn(expectedCount); |
45 |
| - playback(); |
46 |
| - Assert.assertEquals(expectedCount, Person.countPeople()); |
47 |
| - } |
48 |
| - |
49 |
| - @Test(expected=PersistenceException.class) |
50 |
| - public void testNoArgThrows() { |
51 |
| - Person.countPeople(); |
52 |
| - expectThrow(new PersistenceException()); |
53 |
| - playback(); |
54 |
| - Person.countPeople(); |
55 |
| - } |
56 |
| - |
57 |
| - @Test |
58 |
| - public void testArgMethodMatches() { |
59 |
| - long id = 13; |
60 |
| - Person found = new Person(); |
61 |
| - Person.findPerson(id); |
62 |
| - expectReturn(found); |
63 |
| - playback(); |
64 |
| - Assert.assertEquals(found, Person.findPerson(id)); |
65 |
| - } |
66 |
| - |
67 |
| - |
68 |
| - @Test |
69 |
| - public void testLongSeriesOfCalls() { |
70 |
| - long id1 = 13; |
71 |
| - long id2 = 24; |
72 |
| - Person found1 = new Person(); |
73 |
| - Person.findPerson(id1); |
74 |
| - expectReturn(found1); |
75 |
| - Person found2 = new Person(); |
76 |
| - Person.findPerson(id2); |
77 |
| - expectReturn(found2); |
78 |
| - Person.findPerson(id1); |
79 |
| - expectReturn(found1); |
80 |
| - Person.countPeople(); |
81 |
| - expectReturn(0); |
82 |
| - playback(); |
83 |
| - |
84 |
| - Assert.assertEquals(found1, Person.findPerson(id1)); |
85 |
| - Assert.assertEquals(found2, Person.findPerson(id2)); |
86 |
| - Assert.assertEquals(found1, Person.findPerson(id1)); |
87 |
| - Assert.assertEquals(0, Person.countPeople()); |
88 |
| - } |
89 |
| - |
90 |
| - // Note delegation is used when tests are invalid and should fail, as otherwise |
91 |
| - // the failure will occur on the verify() method in the aspect after |
92 |
| - // this method returns, failing the test case |
93 |
| - @Test |
94 |
| - public void testArgMethodNoMatchExpectReturn() { |
95 |
| - try { |
96 |
| - new Delegate().testArgMethodNoMatchExpectReturn(); |
97 |
| - Assert.fail(); |
98 |
| - } catch (IllegalArgumentException expected) { |
99 |
| - } |
100 |
| - } |
101 |
| - |
102 |
| - @Test(expected=IllegalArgumentException.class) |
103 |
| - public void testArgMethodNoMatchExpectThrow() { |
104 |
| - new Delegate().testArgMethodNoMatchExpectThrow(); |
105 |
| - } |
106 |
| - |
107 |
| - private void called(Person found, long id) { |
108 |
| - Assert.assertEquals(found, Person.findPerson(id)); |
109 |
| - } |
110 |
| - |
111 |
| - @Test |
112 |
| - public void testReentrant() { |
113 |
| - long id = 13; |
114 |
| - Person found = new Person(); |
115 |
| - Person.findPerson(id); |
116 |
| - expectReturn(found); |
117 |
| - playback(); |
118 |
| - called(found, id); |
119 |
| - } |
120 |
| - |
121 |
| - @Test(expected=IllegalStateException.class) |
122 |
| - public void testRejectUnexpectedCall() { |
123 |
| - new Delegate().rejectUnexpectedCall(); |
124 |
| - } |
125 |
| - |
126 |
| - @Test(expected=IllegalStateException.class) |
127 |
| - public void testFailTooFewCalls() { |
128 |
| - new Delegate().failTooFewCalls(); |
129 |
| - } |
130 |
| - |
131 |
| - @Test |
132 |
| - public void testEmpty() { |
133 |
| - // Test that verification check doesn't blow up if no replay() call happened |
134 |
| - } |
135 |
| - |
136 |
| - @Test(expected=IllegalStateException.class) |
137 |
| - public void testDoesntEverReplay() { |
138 |
| - new Delegate().doesntEverReplay(); |
139 |
| - } |
140 |
| - |
141 |
| - @Test(expected=IllegalStateException.class) |
142 |
| - public void testDoesntEverSetReturn() { |
143 |
| - new Delegate().doesntEverSetReturn(); |
144 |
| - } |
145 |
| -} |
146 |
| - |
| 1 | +/* |
| 2 | + * Copyright 2002-2010 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.mock.staticmock; |
| 18 | + |
| 19 | +import javax.persistence.PersistenceException; |
| 20 | + |
| 21 | +import junit.framework.Assert; |
| 22 | + |
| 23 | +import org.junit.Test; |
| 24 | +import org.junit.runner.RunWith; |
| 25 | +import org.junit.runners.JUnit4; |
| 26 | + |
| 27 | +import static org.springframework.mock.staticmock.AnnotationDrivenStaticEntityMockingControl.*; |
| 28 | + |
| 29 | + |
| 30 | +/** |
| 31 | + * Test for static entity mocking framework. |
| 32 | + * @author Rod Johnson |
| 33 | + * @author Ramnivas Laddad |
| 34 | + * |
| 35 | + */ |
| 36 | +@MockStaticEntityMethods |
| 37 | +@RunWith(JUnit4.class) |
| 38 | +public class AnnotationDrivenStaticEntityMockingControlTest { |
| 39 | + |
| 40 | + @Test |
| 41 | + public void testNoArgIntReturn() { |
| 42 | + int expectedCount = 13; |
| 43 | + Person.countPeople(); |
| 44 | + expectReturn(expectedCount); |
| 45 | + playback(); |
| 46 | + Assert.assertEquals(expectedCount, Person.countPeople()); |
| 47 | + } |
| 48 | + |
| 49 | + @Test(expected=PersistenceException.class) |
| 50 | + public void testNoArgThrows() { |
| 51 | + Person.countPeople(); |
| 52 | + expectThrow(new PersistenceException()); |
| 53 | + playback(); |
| 54 | + Person.countPeople(); |
| 55 | + } |
| 56 | + |
| 57 | + @Test |
| 58 | + public void testArgMethodMatches() { |
| 59 | + long id = 13; |
| 60 | + Person found = new Person(); |
| 61 | + Person.findPerson(id); |
| 62 | + expectReturn(found); |
| 63 | + playback(); |
| 64 | + Assert.assertEquals(found, Person.findPerson(id)); |
| 65 | + } |
| 66 | + |
| 67 | + |
| 68 | + @Test |
| 69 | + public void testLongSeriesOfCalls() { |
| 70 | + long id1 = 13; |
| 71 | + long id2 = 24; |
| 72 | + Person found1 = new Person(); |
| 73 | + Person.findPerson(id1); |
| 74 | + expectReturn(found1); |
| 75 | + Person found2 = new Person(); |
| 76 | + Person.findPerson(id2); |
| 77 | + expectReturn(found2); |
| 78 | + Person.findPerson(id1); |
| 79 | + expectReturn(found1); |
| 80 | + Person.countPeople(); |
| 81 | + expectReturn(0); |
| 82 | + playback(); |
| 83 | + |
| 84 | + Assert.assertEquals(found1, Person.findPerson(id1)); |
| 85 | + Assert.assertEquals(found2, Person.findPerson(id2)); |
| 86 | + Assert.assertEquals(found1, Person.findPerson(id1)); |
| 87 | + Assert.assertEquals(0, Person.countPeople()); |
| 88 | + } |
| 89 | + |
| 90 | + // Note delegation is used when tests are invalid and should fail, as otherwise |
| 91 | + // the failure will occur on the verify() method in the aspect after |
| 92 | + // this method returns, failing the test case |
| 93 | + @Test |
| 94 | + public void testArgMethodNoMatchExpectReturn() { |
| 95 | + try { |
| 96 | + new Delegate().testArgMethodNoMatchExpectReturn(); |
| 97 | + Assert.fail(); |
| 98 | + } catch (IllegalArgumentException expected) { |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + @Test(expected=IllegalArgumentException.class) |
| 103 | + public void testArgMethodNoMatchExpectThrow() { |
| 104 | + new Delegate().testArgMethodNoMatchExpectThrow(); |
| 105 | + } |
| 106 | + |
| 107 | + private void called(Person found, long id) { |
| 108 | + Assert.assertEquals(found, Person.findPerson(id)); |
| 109 | + } |
| 110 | + |
| 111 | + @Test |
| 112 | + public void testReentrant() { |
| 113 | + long id = 13; |
| 114 | + Person found = new Person(); |
| 115 | + Person.findPerson(id); |
| 116 | + expectReturn(found); |
| 117 | + playback(); |
| 118 | + called(found, id); |
| 119 | + } |
| 120 | + |
| 121 | + @Test(expected=IllegalStateException.class) |
| 122 | + public void testRejectUnexpectedCall() { |
| 123 | + new Delegate().rejectUnexpectedCall(); |
| 124 | + } |
| 125 | + |
| 126 | + @Test(expected=IllegalStateException.class) |
| 127 | + public void testFailTooFewCalls() { |
| 128 | + new Delegate().failTooFewCalls(); |
| 129 | + } |
| 130 | + |
| 131 | + @Test |
| 132 | + public void testEmpty() { |
| 133 | + // Test that verification check doesn't blow up if no replay() call happened |
| 134 | + } |
| 135 | + |
| 136 | + @Test(expected=IllegalStateException.class) |
| 137 | + public void testDoesntEverReplay() { |
| 138 | + new Delegate().doesntEverReplay(); |
| 139 | + } |
| 140 | + |
| 141 | + @Test(expected=IllegalStateException.class) |
| 142 | + public void testDoesntEverSetReturn() { |
| 143 | + new Delegate().doesntEverSetReturn(); |
| 144 | + } |
| 145 | +} |
| 146 | +
|
0 commit comments