Modified staticmethod mocking to remove compile-time dependency on JUnit (see ROO-314 and related issues)

git-svn-id: https://src.springframework.org/svn/spring-framework/trunk@2243 50f2f4bb-b051-0410-bef5-90022cba6387
This commit is contained in:
Ramnivas Laddad 2009-11-03 00:51:22 +00:00
parent 392ba142cd
commit c2c8150a35
4 changed files with 41 additions and 46 deletions

View File

@ -52,7 +52,7 @@
<dependency> <dependency>
<groupId>junit</groupId> <groupId>junit</groupId>
<artifactId>junit</artifactId> <artifactId>junit</artifactId>
<scope>compile</scope> <scope>test</scope>
</dependency> </dependency>
<dependency> <dependency>
<groupId>javax.mail</groupId> <groupId>javax.mail</groupId>

View File

@ -1,26 +1,24 @@
package org.springframework.mock.staticmock; package org.springframework.mock.staticmock;
import javax.persistence.Entity; import javax.persistence.Entity;
import org.junit.Test;
/** /**
* JUnit-specific aspect to use in test build to enable mocking static methods * Annotation-based aspect to use in test build to enable mocking static methods
* on Entity classes, as used by Roo for finders. * on Entity classes, as used by Roo for finders.
* <br> * <br>
* Mocking will occur in JUnit tests where the Test class is annotated with the * Mocking will occur in the call stack of any method in a class (typically a test class)
* @MockStaticEntityMethods annotation, in the call stack of each * that is annotated with the @MockStaticEntityMethods annotation.
* JUnit @Test method.
* <br> * <br>
* Also provides static methods to simplify the programming model for * Also provides static methods to simplify the programming model for
* entering playback mode and setting expected return values. * entering playback mode and setting expected return values.
* <br> * <br>
* Usage:<ol> * Usage:<ol>
* <li>Annotate a JUnit test class with @MockStaticEntityMethods. * <li>Annotate a test class with @MockStaticEntityMethods.
* <li>In each @Test method, JUnitMockControl will begin in recording mode. * <li>In each test method, AnnotationDrivenStaticEntityMockingControl will begin in recording mode.
* Invoke static methods on Entity classes, with each recording-mode invocation * Invoke static methods on Entity classes, with each recording-mode invocation
* being followed by an invocation to the static expectReturn() or expectThrow() * being followed by an invocation to the static expectReturn() or expectThrow()
* method on JUnitMockControl. * method on AnnotationDrivenStaticEntityMockingControl.
* <li>Invoke the static JUnitMockControl.playback() method. * <li>Invoke the static AnnotationDrivenStaticEntityMockingControl() method.
* <li>Call the code you wish to test that uses the static methods. Verification will * <li>Call the code you wish to test that uses the static methods. Verification will
* occur automatically. * occur automatically.
* </ol> * </ol>
@ -31,26 +29,26 @@ import org.junit.Test;
* @author Ramnivas Laddad * @author Ramnivas Laddad
* *
*/ */
public aspect JUnitStaticEntityMockingControl extends AbstractMethodMockingControl { public aspect AnnotationDrivenStaticEntityMockingControl extends AbstractMethodMockingControl {
/** /**
* Stop recording mock calls and enter playback state * Stop recording mock calls and enter playback state
*/ */
public static void playback() { public static void playback() {
JUnitStaticEntityMockingControl.aspectOf().playbackInternal(); AnnotationDrivenStaticEntityMockingControl.aspectOf().playbackInternal();
} }
public static void expectReturn(Object retVal) { public static void expectReturn(Object retVal) {
JUnitStaticEntityMockingControl.aspectOf().expectReturnInternal(retVal); AnnotationDrivenStaticEntityMockingControl.aspectOf().expectReturnInternal(retVal);
} }
public static void expectThrow(Throwable throwable) { public static void expectThrow(Throwable throwable) {
JUnitStaticEntityMockingControl.aspectOf().expectThrowInternal(throwable); AnnotationDrivenStaticEntityMockingControl.aspectOf().expectThrowInternal(throwable);
} }
// Only matches directly annotated @Test methods, to allow methods in // Only matches directly annotated @Test methods, to allow methods in
// @MockStatics classes to invoke each other without resetting the mocking environment // @MockStatics classes to invoke each other without resetting the mocking environment
protected pointcut mockStaticsTestMethod() : execution(@Test public * (@MockStaticEntityMethods *).*(..)); protected pointcut mockStaticsTestMethod() : execution(public * (@MockStaticEntityMethods *).*(..));
protected pointcut methodToMock() : execution(public static * (@Entity *).*(..)); protected pointcut methodToMock() : execution(public static * (@Entity *).*(..));

View File

@ -16,8 +16,6 @@
package org.springframework.mock.staticmock; package org.springframework.mock.staticmock;
import java.rmi.RemoteException;
import javax.persistence.PersistenceException; import javax.persistence.PersistenceException;
import junit.framework.Assert; import junit.framework.Assert;
@ -25,9 +23,8 @@ import junit.framework.Assert;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.junit.runners.JUnit4; import org.junit.runners.JUnit4;
import org.springframework.mock.staticmock.JUnitStaticEntityMockingControl;
import org.springframework.mock.staticmock.MockStaticEntityMethods; import static org.springframework.mock.staticmock.AnnotationDrivenStaticEntityMockingControl.*;
import org.springframework.remoting.RemoteAccessException;
/** /**
@ -38,22 +35,22 @@ import org.springframework.remoting.RemoteAccessException;
*/ */
@MockStaticEntityMethods @MockStaticEntityMethods
@RunWith(JUnit4.class) @RunWith(JUnit4.class)
public class JUnitStaticEntityMockingControlTest { public class AnnotationDrivenStaticEntityMockingControlTest {
@Test @Test
public void testNoArgIntReturn() { public void testNoArgIntReturn() {
int expectedCount = 13; int expectedCount = 13;
Person.countPeople(); Person.countPeople();
JUnitStaticEntityMockingControl.expectReturn(expectedCount); expectReturn(expectedCount);
JUnitStaticEntityMockingControl.playback(); playback();
Assert.assertEquals(expectedCount, Person.countPeople()); Assert.assertEquals(expectedCount, Person.countPeople());
} }
@Test(expected=PersistenceException.class) @Test(expected=PersistenceException.class)
public void testNoArgThrows() { public void testNoArgThrows() {
Person.countPeople(); Person.countPeople();
JUnitStaticEntityMockingControl.expectThrow(new PersistenceException()); expectThrow(new PersistenceException());
JUnitStaticEntityMockingControl.playback(); playback();
Person.countPeople(); Person.countPeople();
} }
@ -62,8 +59,8 @@ public class JUnitStaticEntityMockingControlTest {
long id = 13; long id = 13;
Person found = new Person(); Person found = new Person();
Person.findPerson(id); Person.findPerson(id);
JUnitStaticEntityMockingControl.expectReturn(found); expectReturn(found);
JUnitStaticEntityMockingControl.playback(); playback();
Assert.assertEquals(found, Person.findPerson(id)); Assert.assertEquals(found, Person.findPerson(id));
} }
@ -74,15 +71,15 @@ public class JUnitStaticEntityMockingControlTest {
long id2 = 24; long id2 = 24;
Person found1 = new Person(); Person found1 = new Person();
Person.findPerson(id1); Person.findPerson(id1);
JUnitStaticEntityMockingControl.expectReturn(found1); expectReturn(found1);
Person found2 = new Person(); Person found2 = new Person();
Person.findPerson(id2); Person.findPerson(id2);
JUnitStaticEntityMockingControl.expectReturn(found2); expectReturn(found2);
Person.findPerson(id1); Person.findPerson(id1);
JUnitStaticEntityMockingControl.expectReturn(found1); expectReturn(found1);
Person.countPeople(); Person.countPeople();
JUnitStaticEntityMockingControl.expectReturn(0); expectReturn(0);
JUnitStaticEntityMockingControl.playback(); playback();
Assert.assertEquals(found1, Person.findPerson(id1)); Assert.assertEquals(found1, Person.findPerson(id1));
Assert.assertEquals(found2, Person.findPerson(id2)); Assert.assertEquals(found2, Person.findPerson(id2));
@ -116,8 +113,8 @@ public class JUnitStaticEntityMockingControlTest {
long id = 13; long id = 13;
Person found = new Person(); Person found = new Person();
Person.findPerson(id); Person.findPerson(id);
JUnitStaticEntityMockingControl.expectReturn(found); expectReturn(found);
JUnitStaticEntityMockingControl.playback(); playback();
called(found, id); called(found, id);
} }

View File

@ -8,13 +8,13 @@ import junit.framework.Assert;
import org.junit.Ignore; import org.junit.Ignore;
import org.junit.Test; import org.junit.Test;
import org.springframework.mock.staticmock.JUnitStaticEntityMockingControl; import org.springframework.mock.staticmock.AnnotationDrivenStaticEntityMockingControl;
import org.springframework.mock.staticmock.MockStaticEntityMethods; import org.springframework.mock.staticmock.MockStaticEntityMethods;
//Used because verification failures occur after method returns, //Used because verification failures occur after method returns,
//so we can't test for them in the test case itself //so we can't test for them in the test case itself
@MockStaticEntityMethods @MockStaticEntityMethods
@Ignore // This isn't meant for direct testing; rather it is driven from JUnintStaticEntityMockingControlTest @Ignore // This isn't meant for direct testing; rather it is driven from AnnotationDrivenStaticEntityMockingControl
public class Delegate { public class Delegate {
@Test @Test
@ -22,8 +22,8 @@ public class Delegate {
long id = 13; long id = 13;
Person found = new Person(); Person found = new Person();
Person.findPerson(id); Person.findPerson(id);
JUnitStaticEntityMockingControl.expectReturn(found); AnnotationDrivenStaticEntityMockingControl.expectReturn(found);
JUnitStaticEntityMockingControl.playback(); AnnotationDrivenStaticEntityMockingControl.playback();
Assert.assertEquals(found, Person.findPerson(id + 1)); Assert.assertEquals(found, Person.findPerson(id + 1));
} }
@ -32,8 +32,8 @@ public class Delegate {
long id = 13; long id = 13;
Person found = new Person(); Person found = new Person();
Person.findPerson(id); Person.findPerson(id);
JUnitStaticEntityMockingControl.expectThrow(new PersistenceException()); AnnotationDrivenStaticEntityMockingControl.expectThrow(new PersistenceException());
JUnitStaticEntityMockingControl.playback(); AnnotationDrivenStaticEntityMockingControl.playback();
Assert.assertEquals(found, Person.findPerson(id + 1)); Assert.assertEquals(found, Person.findPerson(id + 1));
} }
@ -42,10 +42,10 @@ public class Delegate {
long id = 13; long id = 13;
Person found = new Person(); Person found = new Person();
Person.findPerson(id); Person.findPerson(id);
JUnitStaticEntityMockingControl.expectReturn(found); AnnotationDrivenStaticEntityMockingControl.expectReturn(found);
Person.countPeople(); Person.countPeople();
JUnitStaticEntityMockingControl.expectReturn(25); AnnotationDrivenStaticEntityMockingControl.expectReturn(25);
JUnitStaticEntityMockingControl.playback(); AnnotationDrivenStaticEntityMockingControl.playback();
Assert.assertEquals(found, Person.findPerson(id)); Assert.assertEquals(found, Person.findPerson(id));
} }
@ -57,19 +57,19 @@ public class Delegate {
@Test @Test
public void doesntEverSetReturn() { public void doesntEverSetReturn() {
Person.countPeople(); Person.countPeople();
JUnitStaticEntityMockingControl.playback(); AnnotationDrivenStaticEntityMockingControl.playback();
} }
@Test @Test
public void rejectUnexpectedCall() { public void rejectUnexpectedCall() {
JUnitStaticEntityMockingControl.playback(); AnnotationDrivenStaticEntityMockingControl.playback();
Person.countPeople(); Person.countPeople();
} }
@Test(expected=RemoteException.class) @Test(expected=RemoteException.class)
public void testVerificationFailsEvenWhenTestFailsInExpectedManner() throws RemoteException { public void testVerificationFailsEvenWhenTestFailsInExpectedManner() throws RemoteException {
Person.countPeople(); Person.countPeople();
JUnitStaticEntityMockingControl.playback(); AnnotationDrivenStaticEntityMockingControl.playback();
// No calls to allow verification failure // No calls to allow verification failure
throw new RemoteException(); throw new RemoteException();
} }