Fix off-by-one regression in AbstractMethodMockingControl

This commit fixes the off-by-one regression accidentally introduced in
commit 55961544a7.

Specifically, this fix ensures that the correct recorded call is
indexed in the 'calls' list in the implementation of
AbstractMethodMockingControl.Expectations.nextCall().

In addition, this commit improves the Javadoc for
AbstractMethodMockingControl, @MockStaticEntityMethods, and
AnnotationDrivenStaticEntityMockingControl and introduces a proper
toString() implementation for the internal Expectations.Call class in
AbstractMethodMockingControl. Furthermore, code from the obsolete
Delegate test class has been inlined in
AnnotationDrivenStaticEntityMockingControlTests.

Issue: SPR-11385, SPR-10885
This commit is contained in:
Sam Brannen
2014-02-04 23:14:39 +01:00
parent 1cd5071e6a
commit 69a89b1bb0
5 changed files with 166 additions and 197 deletions

View File

@@ -16,16 +16,18 @@
package org.springframework.mock.staticmock;
import java.rmi.RemoteException;
import javax.persistence.PersistenceException;
import org.junit.Ignore;
import org.junit.Test;
import static org.junit.Assert.*;
import static org.springframework.mock.staticmock.AnnotationDrivenStaticEntityMockingControl.*;
/**
* Tests for static entity mocking framework.
* Tests for Spring's static entity mocking framework (i.e., @{@link MockStaticEntityMethods}
* and {@link AnnotationDrivenStaticEntityMockingControl}).
*
* @author Rod Johnson
* @author Ramnivas Laddad
@@ -34,10 +36,8 @@ import static org.springframework.mock.staticmock.AnnotationDrivenStaticEntityMo
@MockStaticEntityMethods
public class AnnotationDrivenStaticEntityMockingControlTests {
// TODO Fix failing test
@Ignore
@Test
public void noArgIntReturn() {
public void noArgumentMethodInvocationReturnsInt() {
int expectedCount = 13;
Person.countPeople();
expectReturn(expectedCount);
@@ -45,20 +45,16 @@ public class AnnotationDrivenStaticEntityMockingControlTests {
assertEquals(expectedCount, Person.countPeople());
}
// TODO Fix failing test
@Ignore
@Test(expected = PersistenceException.class)
public void noArgThrows() {
public void noArgumentMethodInvocationThrowsException() {
Person.countPeople();
expectThrow(new PersistenceException());
playback();
Person.countPeople();
}
// TODO Fix failing test
@Ignore
@Test
public void argMethodMatches() {
public void methodArgumentsMatch() {
long id = 13;
Person found = new Person();
Person.findPerson(id);
@@ -67,8 +63,6 @@ public class AnnotationDrivenStaticEntityMockingControlTests {
assertEquals(found, Person.findPerson(id));
}
// TODO Fix failing test
@Ignore
@Test
public void longSeriesOfCalls() {
long id1 = 13;
@@ -91,36 +85,26 @@ public class AnnotationDrivenStaticEntityMockingControlTests {
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.
*/
// TODO Fix failing test
@Ignore
@Test
public void argMethodNoMatchExpectReturn() {
try {
new Delegate().argMethodNoMatchExpectReturn();
fail();
}
catch (IllegalArgumentException expected) {
}
}
// TODO Fix failing test
@Ignore
@Test(expected = IllegalArgumentException.class)
public void argMethodNoMatchExpectThrow() {
new Delegate().argMethodNoMatchExpectThrow();
public void methodArgumentsDoNotMatchAndReturnsObject() {
long id = 13;
Person found = new Person();
Person.findPerson(id);
AnnotationDrivenStaticEntityMockingControl.expectReturn(found);
AnnotationDrivenStaticEntityMockingControl.playback();
assertEquals(found, Person.findPerson(id + 1));
}
private void called(Person found, long id) {
assertEquals(found, Person.findPerson(id));
@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));
}
// TODO Fix failing test
@Ignore
@Test
public void reentrant() {
long id = 13;
@@ -131,31 +115,56 @@ public class AnnotationDrivenStaticEntityMockingControlTests {
called(found, id);
}
@Test(expected = IllegalStateException.class)
public void rejectUnexpectedCall() {
new Delegate().rejectUnexpectedCall();
private void called(Person found, long id) {
assertEquals(found, Person.findPerson(id));
}
// TODO Fix failing test
@Ignore
@Test(expected = IllegalStateException.class)
public void failTooFewCalls() {
new Delegate().failTooFewCalls();
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 that verification check doesn't blow up if no replay() call happened.
}
@Test(expected = IllegalStateException.class)
public void doesntEverReplay() {
new Delegate().doesntEverReplay();
public void doesNotEnterPlaybackMode() {
Person.countPeople();
}
@Test(expected = IllegalStateException.class)
public void doesntEverSetReturn() {
new Delegate().doesntEverSetReturn();
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();
}
}

View File

@@ -1,90 +0,0 @@
/*
* 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.Ignore;
import static org.junit.Assert.*;
/**
* This isn't meant for direct testing; rather it is driven from
* {@link AnnotationDrivenStaticEntityMockingControlTests}.
*
* @author Rod Johnson
* @author Ramnivas Laddad
* @author Sam Brannen
*/
@MockStaticEntityMethods
@Ignore("Used because verification failures occur after method returns, so we can't test for them in the test case itself")
public class Delegate {
public void argMethodNoMatchExpectReturn() {
long id = 13;
Person found = new Person();
Person.findPerson(id);
AnnotationDrivenStaticEntityMockingControl.expectReturn(found);
AnnotationDrivenStaticEntityMockingControl.playback();
assertEquals(found, Person.findPerson(id + 1));
}
public void argMethodNoMatchExpectThrow() {
long id = 13;
Person found = new Person();
Person.findPerson(id);
AnnotationDrivenStaticEntityMockingControl.expectThrow(new PersistenceException());
AnnotationDrivenStaticEntityMockingControl.playback();
assertEquals(found, Person.findPerson(id + 1));
}
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));
}
public void doesntEverReplay() {
Person.countPeople();
}
public void doesntEverSetReturn() {
Person.countPeople();
AnnotationDrivenStaticEntityMockingControl.playback();
}
public void rejectUnexpectedCall() {
AnnotationDrivenStaticEntityMockingControl.playback();
Person.countPeople();
}
public void verificationFailsEvenWhenTestFailsInExpectedManner()
throws RemoteException {
Person.countPeople();
AnnotationDrivenStaticEntityMockingControl.playback();
// No calls to allow verification failure
throw new RemoteException();
}
}