AnnotationDrivenStaticEntityMockingControlTests.java 8.0 KB
Newer Older
C
Chris Beams 已提交
1
/*
2
 * Copyright 2002-2014 the original author or authors.
C
Chris Beams 已提交
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.springframework.mock.staticmock;

19
import javax.persistence.PersistenceException;
C
Chris Beams 已提交
20 21

import org.junit.Test;
22 23 24

import static org.junit.Assert.*;
import static org.springframework.mock.staticmock.AnnotationDrivenStaticEntityMockingControl.*;
C
Chris Beams 已提交
25 26

/**
27 28
 * Tests for Spring's static entity mocking framework (i.e., @{@link MockStaticEntityMethods}
 * and {@link AnnotationDrivenStaticEntityMockingControl}).
29
 *
C
Chris Beams 已提交
30 31
 * @author Rod Johnson
 * @author Ramnivas Laddad
32
 * @author Sam Brannen
C
Chris Beams 已提交
33 34
 */
@MockStaticEntityMethods
35
public class AnnotationDrivenStaticEntityMockingControlTests {
C
Chris Beams 已提交
36 37

	@Test
38
	public void noArgumentMethodInvocationReturnsInt() {
C
Chris Beams 已提交
39 40 41 42
		int expectedCount = 13;
		Person.countPeople();
		expectReturn(expectedCount);
		playback();
43
		assertEquals(expectedCount, Person.countPeople());
C
Chris Beams 已提交
44
	}
45

46
	@Test(expected = PersistenceException.class)
47
	public void noArgumentMethodInvocationThrowsException() {
C
Chris Beams 已提交
48 49 50 51 52 53 54
		Person.countPeople();
		expectThrow(new PersistenceException());
		playback();
		Person.countPeople();
	}

	@Test
55
	public void methodArgumentsMatch() {
C
Chris Beams 已提交
56 57 58 59 60
		long id = 13;
		Person found = new Person();
		Person.findPerson(id);
		expectReturn(found);
		playback();
61
		assertEquals(found, Person.findPerson(id));
C
Chris Beams 已提交
62 63 64
	}

	@Test
65
	public void longSeriesOfCalls() {
C
Chris Beams 已提交
66 67 68 69 70 71 72 73 74 75 76 77 78
		long id1 = 13;
		long id2 = 24;
		Person found1 = new Person();
		Person.findPerson(id1);
		expectReturn(found1);
		Person found2 = new Person();
		Person.findPerson(id2);
		expectReturn(found2);
		Person.findPerson(id1);
		expectReturn(found1);
		Person.countPeople();
		expectReturn(0);
		playback();
79

80 81 82 83
		assertEquals(found1, Person.findPerson(id1));
		assertEquals(found2, Person.findPerson(id2));
		assertEquals(found1, Person.findPerson(id1));
		assertEquals(0, Person.countPeople());
C
Chris Beams 已提交
84 85
	}

86
	@Test(expected = IllegalArgumentException.class)
87 88 89 90
	public void methodArgumentsDoNotMatchAndReturnsObject() {
		long id = 13;
		Person found = new Person();
		Person.findPerson(id);
91 92
		expectReturn(found);
		playback();
93
		assertEquals(found, Person.findPerson(id + 1));
C
Chris Beams 已提交
94 95
	}

96 97 98 99 100
	@Test(expected = IllegalArgumentException.class)
	public void methodArgumentsDoNotMatchAndThrowsException() {
		long id = 13;
		Person found = new Person();
		Person.findPerson(id);
101 102
		expectThrow(new PersistenceException());
		playback();
103
		assertEquals(found, Person.findPerson(id + 1));
C
Chris Beams 已提交
104 105 106
	}

	@Test
107
	public void reentrantCallToPrivateMethod() {
C
Chris Beams 已提交
108 109 110 111 112
		long id = 13;
		Person found = new Person();
		Person.findPerson(id);
		expectReturn(found);
		playback();
113
		privateMethod(found, id);
C
Chris Beams 已提交
114 115
	}

116
	private void privateMethod(Person found, long id) {
117 118 119
		assertEquals(found, Person.findPerson(id));
	}

120 121 122 123 124 125 126 127 128 129 130
	@Test
	public void reentrantCallToPublicMethod() {
		final Long ID = 13L;
		Person.findPerson(ID);
		expectReturn(new Person());
		playback();
		try {
			publicMethod();
			fail("Should have thrown an IllegalStateException");
		}
		catch (IllegalStateException e) {
131 132
			String snippet = "Calls have been recorded, but playback state was never reached.";
			assertTrue("Exception message should contain [" + snippet + "]", e.getMessage().contains(snippet));
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
		}

		// Now to keep the mock for "this" method happy:
		Person.findPerson(ID);
	}

	public void publicMethod() {
		// At this point, since publicMethod() is a public method in a class
		// annotated with @MockStaticEntityMethods, the current mock state is
		// fresh. In other words, we are again in recording mode. As such, any
		// call to a mocked method will return null. See the implementation of
		// the <methodToMock() && cflowbelow(mockStaticsTestMethod())> around
		// advice in AbstractMethodMockingControl for details.
		assertNull(Person.findPerson(99L));
	}

149
	@Test(expected = IllegalStateException.class)
150 151
	public void unexpectedCall() {
		playback();
152
		Person.countPeople();
C
Chris Beams 已提交
153
	}
154

155
	@Test(expected = IllegalStateException.class)
156 157 158 159
	public void tooFewCalls() {
		long id = 13;
		Person found = new Person();
		Person.findPerson(id);
160
		expectReturn(found);
161
		Person.countPeople();
162 163
		expectReturn(25);
		playback();
164
		assertEquals(found, Person.findPerson(id));
C
Chris Beams 已提交
165
	}
166

C
Chris Beams 已提交
167
	@Test
168 169 170 171 172 173 174 175 176 177
	public void noExpectationsAndNoPlayback() {
		// Ensure that mock verification doesn't blow up if playback() was not invoked and
		// no expectations were set.
	}

	@Test
	public void noExpectationsButWithPlayback() {
		// Ensure that mock verification doesn't blow up if playback() was invoked but no
		// expectations were set.
		playback();
C
Chris Beams 已提交
178
	}
179

180
	@Test(expected = IllegalStateException.class)
181 182
	public void doesNotEnterPlaybackMode() {
		Person.countPeople();
C
Chris Beams 已提交
183
	}
184

185
	@Test(expected = IllegalStateException.class)
186 187
	public void doesNotSetExpectedReturnValue() {
		Person.countPeople();
188
		playback();
189 190 191
	}

	/**
192 193 194 195 196 197 198 199 200 201 202
	 * Currently, the method mocking aspect will not verify the state of the
	 * expectations within the mock if a test method throws an exception.
	 *
	 * <p>The reason is that the {@code mockStaticsTestMethod()} advice in
	 * {@link AbstractMethodMockingControl} is declared as
	 * {@code after() returning} instead of simply {@code after()}.
	 *
	 * <p>If the aforementioned advice is changed to a generic "after" advice,
	 * this test method will fail with an {@link IllegalStateException} (thrown
	 * by the mock aspect) instead of the {@link UnsupportedOperationException}
	 * thrown by the test method itself.
203
	 */
204 205
	@Test(expected = UnsupportedOperationException.class)
	public void mockVerificationDoesNotOccurIfTestFailsWithAnExpectedException() {
206
		Person.countPeople();
207 208 209 210 211
		playback();
		// We intentionally do not execute any of the recorded method invocations in order
		// to demonstrate that mock verification does not occur if an
		// exception is thrown.
		throw new UnsupportedOperationException();
C
Chris Beams 已提交
212 213
	}

214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299
	@Test
	public void resetMockWithoutVerficationAndStartOverWithoutRedeclaringExpectations() {
		final Long ID = 13L;
		Person.findPerson(ID);
		expectReturn(new Person());

		reset();

		Person.findPerson(ID);
		// Omit expectation.
		playback();

		try {
			Person.findPerson(ID);
			fail("Should have thrown an IllegalStateException");
		}
		catch (IllegalStateException e) {
			String snippet = "Behavior of Call with signature";
			assertTrue("Exception message should contain [" + snippet + "]", e.getMessage().contains(snippet));
		}
	}

	@Test
	public void resetMockWithoutVerificationAndStartOver() {
		final Long ID = 13L;
		Person found = new Person();
		Person.findPerson(ID);
		expectReturn(found);

		reset();

		// Intentionally use a different ID:
		final long ID_2 = ID + 1;
		Person.findPerson(ID_2);
		expectReturn(found);
		playback();

		assertEquals(found, Person.findPerson(ID_2));
	}

	@Test
	public void verifyResetAndStartOver() {
		final Long ID_1 = 13L;
		Person found1 = new Person();
		Person.findPerson(ID_1);
		expectReturn(found1);
		playback();

		assertEquals(found1, Person.findPerson(ID_1));
		verify();
		reset();

		// Intentionally use a different ID:
		final long ID_2 = ID_1 + 1;
		Person found2 = new Person();
		Person.findPerson(ID_2);
		expectReturn(found2);
		playback();

		assertEquals(found2, Person.findPerson(ID_2));
	}

	@Test
	public void verifyWithTooFewCalls() {
		final Long ID = 13L;
		Person found = new Person();
		Person.findPerson(ID);
		expectReturn(found);
		Person.findPerson(ID);
		expectReturn(found);
		playback();

		assertEquals(found, Person.findPerson(ID));

		try {
			verify();
			fail("Should have thrown an IllegalStateException");
		}
		catch (IllegalStateException e) {
			assertEquals("Expected 2 calls, but received 1", e.getMessage());
			// Since verify() failed, we need to manually reset so that the test method
			// does not fail.
			reset();
		}
	}

300
}