Browse Source
Issue: SPR-11385, SPR-10885 (cherry picked from commitspull/465/head69a89b1and3a89bc4)
6 changed files with 290 additions and 307 deletions
@ -1,148 +0,0 @@
@@ -1,148 +0,0 @@
|
||||
/* |
||||
* Copyright 2002-2012 the original author or authors. |
||||
* |
||||
* 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; |
||||
|
||||
import static org.junit.Assert.assertEquals; |
||||
import static org.junit.Assert.fail; |
||||
import static org.springframework.mock.staticmock.AnnotationDrivenStaticEntityMockingControl.expectReturn; |
||||
import static org.springframework.mock.staticmock.AnnotationDrivenStaticEntityMockingControl.expectThrow; |
||||
import static org.springframework.mock.staticmock.AnnotationDrivenStaticEntityMockingControl.playback; |
||||
|
||||
import javax.persistence.PersistenceException; |
||||
|
||||
import org.junit.Test; |
||||
import org.junit.runner.RunWith; |
||||
import org.junit.runners.JUnit4; |
||||
|
||||
|
||||
/** |
||||
* Test for static entity mocking framework. |
||||
* @author Rod Johnson |
||||
* @author Ramnivas Laddad |
||||
* |
||||
*/ |
||||
@MockStaticEntityMethods |
||||
@RunWith(JUnit4.class) |
||||
public class AnnotationDrivenStaticEntityMockingControlTest { |
||||
|
||||
@Test |
||||
public void testNoArgIntReturn() { |
||||
int expectedCount = 13; |
||||
Person.countPeople(); |
||||
expectReturn(expectedCount); |
||||
playback(); |
||||
assertEquals(expectedCount, Person.countPeople()); |
||||
} |
||||
|
||||
@Test(expected=PersistenceException.class) |
||||
public void testNoArgThrows() { |
||||
Person.countPeople(); |
||||
expectThrow(new PersistenceException()); |
||||
playback(); |
||||
Person.countPeople(); |
||||
} |
||||
|
||||
@Test |
||||
public void testArgMethodMatches() { |
||||
long id = 13; |
||||
Person found = new Person(); |
||||
Person.findPerson(id); |
||||
expectReturn(found); |
||||
playback(); |
||||
assertEquals(found, Person.findPerson(id)); |
||||
} |
||||
|
||||
|
||||
@Test |
||||
public void testLongSeriesOfCalls() { |
||||
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(); |
||||
|
||||
assertEquals(found1, Person.findPerson(id1)); |
||||
assertEquals(found2, Person.findPerson(id2)); |
||||
assertEquals(found1, Person.findPerson(id1)); |
||||
assertEquals(0, Person.countPeople()); |
||||
} |
||||
|
||||
// Note delegation is used when tests are invalid and should fail, as otherwise
|
||||
// the failure will occur on the verify() method in the aspect after
|
||||
// this method returns, failing the test case
|
||||
@Test |
||||
public void testArgMethodNoMatchExpectReturn() { |
||||
try { |
||||
new Delegate().testArgMethodNoMatchExpectReturn(); |
||||
fail(); |
||||
} catch (IllegalArgumentException expected) { |
||||
} |
||||
} |
||||
|
||||
@Test(expected=IllegalArgumentException.class) |
||||
public void testArgMethodNoMatchExpectThrow() { |
||||
new Delegate().testArgMethodNoMatchExpectThrow(); |
||||
} |
||||
|
||||
private void called(Person found, long id) { |
||||
assertEquals(found, Person.findPerson(id)); |
||||
} |
||||
|
||||
@Test |
||||
public void testReentrant() { |
||||
long id = 13; |
||||
Person found = new Person(); |
||||
Person.findPerson(id); |
||||
expectReturn(found); |
||||
playback(); |
||||
called(found, id); |
||||
} |
||||
|
||||
@Test(expected=IllegalStateException.class) |
||||
public void testRejectUnexpectedCall() { |
||||
new Delegate().rejectUnexpectedCall(); |
||||
} |
||||
|
||||
@Test(expected=IllegalStateException.class) |
||||
public void testFailTooFewCalls() { |
||||
new Delegate().failTooFewCalls(); |
||||
} |
||||
|
||||
@Test |
||||
public void testEmpty() { |
||||
// Test that verification check doesn't blow up if no replay() call happened
|
||||
} |
||||
|
||||
@Test(expected=IllegalStateException.class) |
||||
public void testDoesntEverReplay() { |
||||
new Delegate().doesntEverReplay(); |
||||
} |
||||
|
||||
@Test(expected=IllegalStateException.class) |
||||
public void testDoesntEverSetReturn() { |
||||
new Delegate().doesntEverSetReturn(); |
||||
} |
||||
} |
||||
|
||||
@ -0,0 +1,170 @@
@@ -0,0 +1,170 @@
|
||||
/* |
||||
* Copyright 2002-2014 the original author or authors. |
||||
* |
||||
* 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; |
||||
|
||||
import java.rmi.RemoteException; |
||||
|
||||
import javax.persistence.PersistenceException; |
||||
|
||||
import org.junit.Test; |
||||
|
||||
import static org.junit.Assert.*; |
||||
import static org.springframework.mock.staticmock.AnnotationDrivenStaticEntityMockingControl.*; |
||||
|
||||
/** |
||||
* Tests for Spring's static entity mocking framework (i.e., @{@link MockStaticEntityMethods} |
||||
* and {@link AnnotationDrivenStaticEntityMockingControl}). |
||||
* |
||||
* @author Rod Johnson |
||||
* @author Ramnivas Laddad |
||||
* @author Sam Brannen |
||||
*/ |
||||
@MockStaticEntityMethods |
||||
public class AnnotationDrivenStaticEntityMockingControlTests { |
||||
|
||||
@Test |
||||
public void noArgumentMethodInvocationReturnsInt() { |
||||
int expectedCount = 13; |
||||
Person.countPeople(); |
||||
expectReturn(expectedCount); |
||||
playback(); |
||||
assertEquals(expectedCount, Person.countPeople()); |
||||
} |
||||
|
||||
@Test(expected = PersistenceException.class) |
||||
public void noArgumentMethodInvocationThrowsException() { |
||||
Person.countPeople(); |
||||
expectThrow(new PersistenceException()); |
||||
playback(); |
||||
Person.countPeople(); |
||||
} |
||||
|
||||
@Test |
||||
public void methodArgumentsMatch() { |
||||
long id = 13; |
||||
Person found = new Person(); |
||||
Person.findPerson(id); |
||||
expectReturn(found); |
||||
playback(); |
||||
assertEquals(found, Person.findPerson(id)); |
||||
} |
||||
|
||||
@Test |
||||
public void longSeriesOfCalls() { |
||||
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(); |
||||
|
||||
assertEquals(found1, Person.findPerson(id1)); |
||||
assertEquals(found2, Person.findPerson(id2)); |
||||
assertEquals(found1, Person.findPerson(id1)); |
||||
assertEquals(0, Person.countPeople()); |
||||
} |
||||
|
||||
@Test(expected = IllegalArgumentException.class) |
||||
public void methodArgumentsDoNotMatchAndReturnsObject() { |
||||
long id = 13; |
||||
Person found = new Person(); |
||||
Person.findPerson(id); |
||||
AnnotationDrivenStaticEntityMockingControl.expectReturn(found); |
||||
AnnotationDrivenStaticEntityMockingControl.playback(); |
||||
assertEquals(found, Person.findPerson(id + 1)); |
||||
} |
||||
|
||||
@Test(expected = IllegalArgumentException.class) |
||||
public void methodArgumentsDoNotMatchAndThrowsException() { |
||||
long id = 13; |
||||
Person found = new Person(); |
||||
Person.findPerson(id); |
||||
AnnotationDrivenStaticEntityMockingControl.expectThrow(new PersistenceException()); |
||||
AnnotationDrivenStaticEntityMockingControl.playback(); |
||||
assertEquals(found, Person.findPerson(id + 1)); |
||||
} |
||||
|
||||
@Test |
||||
public void reentrant() { |
||||
long id = 13; |
||||
Person found = new Person(); |
||||
Person.findPerson(id); |
||||
expectReturn(found); |
||||
playback(); |
||||
called(found, id); |
||||
} |
||||
|
||||
private void called(Person found, long id) { |
||||
assertEquals(found, Person.findPerson(id)); |
||||
} |
||||
|
||||
@Test(expected = IllegalStateException.class) |
||||
public void rejectUnexpectedCall() { |
||||
AnnotationDrivenStaticEntityMockingControl.playback(); |
||||
Person.countPeople(); |
||||
} |
||||
|
||||
@Test(expected = IllegalStateException.class) |
||||
public void tooFewCalls() { |
||||
long id = 13; |
||||
Person found = new Person(); |
||||
Person.findPerson(id); |
||||
AnnotationDrivenStaticEntityMockingControl.expectReturn(found); |
||||
Person.countPeople(); |
||||
AnnotationDrivenStaticEntityMockingControl.expectReturn(25); |
||||
AnnotationDrivenStaticEntityMockingControl.playback(); |
||||
assertEquals(found, Person.findPerson(id)); |
||||
} |
||||
|
||||
@Test |
||||
public void empty() { |
||||
// Test that verification check doesn't blow up if no replay() call happened.
|
||||
} |
||||
|
||||
@Test(expected = IllegalStateException.class) |
||||
public void doesNotEnterPlaybackMode() { |
||||
Person.countPeople(); |
||||
} |
||||
|
||||
@Test(expected = IllegalStateException.class) |
||||
public void doesNotSetExpectedReturnValue() { |
||||
Person.countPeople(); |
||||
AnnotationDrivenStaticEntityMockingControl.playback(); |
||||
} |
||||
|
||||
/** |
||||
* Note: this test method currently does NOT actually verify that the mock |
||||
* verification fails. |
||||
*/ |
||||
// TODO Determine if it's possible for a mock verification failure to fail a test in
|
||||
// JUnit 4+ if the test method itself throws an expected exception.
|
||||
@Test(expected = RemoteException.class) |
||||
public void verificationFailsEvenWhenTestFailsInExpectedManner() throws Exception { |
||||
Person.countPeople(); |
||||
AnnotationDrivenStaticEntityMockingControl.playback(); |
||||
// No calls in order to allow verification failure
|
||||
throw new RemoteException(); |
||||
} |
||||
|
||||
} |
||||
@ -1,92 +0,0 @@
@@ -1,92 +0,0 @@
|
||||
/* |
||||
* Copyright 2002-2012 the original author or authors. |
||||
* |
||||
* 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; |
||||
|
||||
import static org.junit.Assert.assertEquals; |
||||
|
||||
import java.rmi.RemoteException; |
||||
|
||||
import javax.persistence.PersistenceException; |
||||
|
||||
import org.junit.Ignore; |
||||
import org.junit.Test; |
||||
import org.springframework.mock.staticmock.AnnotationDrivenStaticEntityMockingControl; |
||||
import org.springframework.mock.staticmock.MockStaticEntityMethods; |
||||
|
||||
//Used because verification failures occur after method returns,
|
||||
//so we can't test for them in the test case itself
|
||||
@MockStaticEntityMethods |
||||
@Ignore // This isn't meant for direct testing; rather it is driven from AnnotationDrivenStaticEntityMockingControl
|
||||
public class Delegate { |
||||
|
||||
@Test |
||||
public void testArgMethodNoMatchExpectReturn() { |
||||
long id = 13; |
||||
Person found = new Person(); |
||||
Person.findPerson(id); |
||||
AnnotationDrivenStaticEntityMockingControl.expectReturn(found); |
||||
AnnotationDrivenStaticEntityMockingControl.playback(); |
||||
assertEquals(found, Person.findPerson(id + 1)); |
||||
} |
||||
|
||||
@Test |
||||
public void testArgMethodNoMatchExpectThrow() { |
||||
long id = 13; |
||||
Person found = new Person(); |
||||
Person.findPerson(id); |
||||
AnnotationDrivenStaticEntityMockingControl.expectThrow(new PersistenceException()); |
||||
AnnotationDrivenStaticEntityMockingControl.playback(); |
||||
assertEquals(found, Person.findPerson(id + 1)); |
||||
} |
||||
|
||||
@Test |
||||
public void failTooFewCalls() { |
||||
long id = 13; |
||||
Person found = new Person(); |
||||
Person.findPerson(id); |
||||
AnnotationDrivenStaticEntityMockingControl.expectReturn(found); |
||||
Person.countPeople(); |
||||
AnnotationDrivenStaticEntityMockingControl.expectReturn(25); |
||||
AnnotationDrivenStaticEntityMockingControl.playback(); |
||||
assertEquals(found, Person.findPerson(id)); |
||||
} |
||||
|
||||
@Test |
||||
public void doesntEverReplay() { |
||||
Person.countPeople(); |
||||
} |
||||
|
||||
@Test |
||||
public void doesntEverSetReturn() { |
||||
Person.countPeople(); |
||||
AnnotationDrivenStaticEntityMockingControl.playback(); |
||||
} |
||||
|
||||
@Test |
||||
public void rejectUnexpectedCall() { |
||||
AnnotationDrivenStaticEntityMockingControl.playback(); |
||||
Person.countPeople(); |
||||
} |
||||
|
||||
@Test(expected=RemoteException.class) |
||||
public void testVerificationFailsEvenWhenTestFailsInExpectedManner() throws RemoteException { |
||||
Person.countPeople(); |
||||
AnnotationDrivenStaticEntityMockingControl.playback(); |
||||
// No calls to allow verification failure
|
||||
throw new RemoteException(); |
||||
} |
||||
} |
||||
Loading…
Reference in new issue