Modified staticmethod mocking to remove compile-time dependency on JUnit (see ROO-314 and related issues)
This commit is contained in:
parent
04a7656292
commit
868cf09b2d
|
@ -52,7 +52,7 @@
|
|||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<scope>compile</scope>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>javax.mail</groupId>
|
||||
|
|
|
@ -1,26 +1,24 @@
|
|||
package org.springframework.mock.staticmock;
|
||||
|
||||
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.
|
||||
* <br>
|
||||
* Mocking will occur in JUnit tests where the Test class is annotated with the
|
||||
* @MockStaticEntityMethods annotation, in the call stack of each
|
||||
* JUnit @Test method.
|
||||
* Mocking will occur in the call stack of any method in a class (typically a test class)
|
||||
* that is annotated with the @MockStaticEntityMethods annotation.
|
||||
* <br>
|
||||
* Also provides static methods to simplify the programming model for
|
||||
* entering playback mode and setting expected return values.
|
||||
* <br>
|
||||
* Usage:<ol>
|
||||
* <li>Annotate a JUnit test class with @MockStaticEntityMethods.
|
||||
* <li>In each @Test method, JUnitMockControl will begin in recording mode.
|
||||
* <li>Annotate a test class with @MockStaticEntityMethods.
|
||||
* <li>In each test method, AnnotationDrivenStaticEntityMockingControl will begin in recording mode.
|
||||
* Invoke static methods on Entity classes, with each recording-mode invocation
|
||||
* being followed by an invocation to the static expectReturn() or expectThrow()
|
||||
* method on JUnitMockControl.
|
||||
* <li>Invoke the static JUnitMockControl.playback() method.
|
||||
* method on AnnotationDrivenStaticEntityMockingControl.
|
||||
* <li>Invoke the static AnnotationDrivenStaticEntityMockingControl() method.
|
||||
* <li>Call the code you wish to test that uses the static methods. Verification will
|
||||
* occur automatically.
|
||||
* </ol>
|
||||
|
@ -31,26 +29,26 @@ import org.junit.Test;
|
|||
* @author Ramnivas Laddad
|
||||
*
|
||||
*/
|
||||
public aspect JUnitStaticEntityMockingControl extends AbstractMethodMockingControl {
|
||||
public aspect AnnotationDrivenStaticEntityMockingControl extends AbstractMethodMockingControl {
|
||||
|
||||
/**
|
||||
* Stop recording mock calls and enter playback state
|
||||
*/
|
||||
public static void playback() {
|
||||
JUnitStaticEntityMockingControl.aspectOf().playbackInternal();
|
||||
AnnotationDrivenStaticEntityMockingControl.aspectOf().playbackInternal();
|
||||
}
|
||||
|
||||
public static void expectReturn(Object retVal) {
|
||||
JUnitStaticEntityMockingControl.aspectOf().expectReturnInternal(retVal);
|
||||
AnnotationDrivenStaticEntityMockingControl.aspectOf().expectReturnInternal(retVal);
|
||||
}
|
||||
|
||||
public static void expectThrow(Throwable throwable) {
|
||||
JUnitStaticEntityMockingControl.aspectOf().expectThrowInternal(throwable);
|
||||
AnnotationDrivenStaticEntityMockingControl.aspectOf().expectThrowInternal(throwable);
|
||||
}
|
||||
|
||||
// Only matches directly annotated @Test methods, to allow methods in
|
||||
// @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 *).*(..));
|
||||
|
|
@ -16,8 +16,6 @@
|
|||
|
||||
package org.springframework.mock.staticmock;
|
||||
|
||||
import java.rmi.RemoteException;
|
||||
|
||||
import javax.persistence.PersistenceException;
|
||||
|
||||
import junit.framework.Assert;
|
||||
|
@ -25,9 +23,8 @@ import junit.framework.Assert;
|
|||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.junit.runners.JUnit4;
|
||||
import org.springframework.mock.staticmock.JUnitStaticEntityMockingControl;
|
||||
import org.springframework.mock.staticmock.MockStaticEntityMethods;
|
||||
import org.springframework.remoting.RemoteAccessException;
|
||||
|
||||
import static org.springframework.mock.staticmock.AnnotationDrivenStaticEntityMockingControl.*;
|
||||
|
||||
|
||||
/**
|
||||
|
@ -38,22 +35,22 @@ import org.springframework.remoting.RemoteAccessException;
|
|||
*/
|
||||
@MockStaticEntityMethods
|
||||
@RunWith(JUnit4.class)
|
||||
public class JUnitStaticEntityMockingControlTest {
|
||||
public class AnnotationDrivenStaticEntityMockingControlTest {
|
||||
|
||||
@Test
|
||||
public void testNoArgIntReturn() {
|
||||
int expectedCount = 13;
|
||||
Person.countPeople();
|
||||
JUnitStaticEntityMockingControl.expectReturn(expectedCount);
|
||||
JUnitStaticEntityMockingControl.playback();
|
||||
expectReturn(expectedCount);
|
||||
playback();
|
||||
Assert.assertEquals(expectedCount, Person.countPeople());
|
||||
}
|
||||
|
||||
@Test(expected=PersistenceException.class)
|
||||
public void testNoArgThrows() {
|
||||
Person.countPeople();
|
||||
JUnitStaticEntityMockingControl.expectThrow(new PersistenceException());
|
||||
JUnitStaticEntityMockingControl.playback();
|
||||
expectThrow(new PersistenceException());
|
||||
playback();
|
||||
Person.countPeople();
|
||||
}
|
||||
|
||||
|
@ -62,8 +59,8 @@ public class JUnitStaticEntityMockingControlTest {
|
|||
long id = 13;
|
||||
Person found = new Person();
|
||||
Person.findPerson(id);
|
||||
JUnitStaticEntityMockingControl.expectReturn(found);
|
||||
JUnitStaticEntityMockingControl.playback();
|
||||
expectReturn(found);
|
||||
playback();
|
||||
Assert.assertEquals(found, Person.findPerson(id));
|
||||
}
|
||||
|
||||
|
@ -74,15 +71,15 @@ public class JUnitStaticEntityMockingControlTest {
|
|||
long id2 = 24;
|
||||
Person found1 = new Person();
|
||||
Person.findPerson(id1);
|
||||
JUnitStaticEntityMockingControl.expectReturn(found1);
|
||||
expectReturn(found1);
|
||||
Person found2 = new Person();
|
||||
Person.findPerson(id2);
|
||||
JUnitStaticEntityMockingControl.expectReturn(found2);
|
||||
expectReturn(found2);
|
||||
Person.findPerson(id1);
|
||||
JUnitStaticEntityMockingControl.expectReturn(found1);
|
||||
expectReturn(found1);
|
||||
Person.countPeople();
|
||||
JUnitStaticEntityMockingControl.expectReturn(0);
|
||||
JUnitStaticEntityMockingControl.playback();
|
||||
expectReturn(0);
|
||||
playback();
|
||||
|
||||
Assert.assertEquals(found1, Person.findPerson(id1));
|
||||
Assert.assertEquals(found2, Person.findPerson(id2));
|
||||
|
@ -116,8 +113,8 @@ public class JUnitStaticEntityMockingControlTest {
|
|||
long id = 13;
|
||||
Person found = new Person();
|
||||
Person.findPerson(id);
|
||||
JUnitStaticEntityMockingControl.expectReturn(found);
|
||||
JUnitStaticEntityMockingControl.playback();
|
||||
expectReturn(found);
|
||||
playback();
|
||||
called(found, id);
|
||||
}
|
||||
|
|
@ -8,13 +8,13 @@ import junit.framework.Assert;
|
|||
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Test;
|
||||
import org.springframework.mock.staticmock.JUnitStaticEntityMockingControl;
|
||||
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 JUnintStaticEntityMockingControlTest
|
||||
@Ignore // This isn't meant for direct testing; rather it is driven from AnnotationDrivenStaticEntityMockingControl
|
||||
public class Delegate {
|
||||
|
||||
@Test
|
||||
|
@ -22,8 +22,8 @@ public class Delegate {
|
|||
long id = 13;
|
||||
Person found = new Person();
|
||||
Person.findPerson(id);
|
||||
JUnitStaticEntityMockingControl.expectReturn(found);
|
||||
JUnitStaticEntityMockingControl.playback();
|
||||
AnnotationDrivenStaticEntityMockingControl.expectReturn(found);
|
||||
AnnotationDrivenStaticEntityMockingControl.playback();
|
||||
Assert.assertEquals(found, Person.findPerson(id + 1));
|
||||
}
|
||||
|
||||
|
@ -32,8 +32,8 @@ public class Delegate {
|
|||
long id = 13;
|
||||
Person found = new Person();
|
||||
Person.findPerson(id);
|
||||
JUnitStaticEntityMockingControl.expectThrow(new PersistenceException());
|
||||
JUnitStaticEntityMockingControl.playback();
|
||||
AnnotationDrivenStaticEntityMockingControl.expectThrow(new PersistenceException());
|
||||
AnnotationDrivenStaticEntityMockingControl.playback();
|
||||
Assert.assertEquals(found, Person.findPerson(id + 1));
|
||||
}
|
||||
|
||||
|
@ -42,10 +42,10 @@ public class Delegate {
|
|||
long id = 13;
|
||||
Person found = new Person();
|
||||
Person.findPerson(id);
|
||||
JUnitStaticEntityMockingControl.expectReturn(found);
|
||||
AnnotationDrivenStaticEntityMockingControl.expectReturn(found);
|
||||
Person.countPeople();
|
||||
JUnitStaticEntityMockingControl.expectReturn(25);
|
||||
JUnitStaticEntityMockingControl.playback();
|
||||
AnnotationDrivenStaticEntityMockingControl.expectReturn(25);
|
||||
AnnotationDrivenStaticEntityMockingControl.playback();
|
||||
Assert.assertEquals(found, Person.findPerson(id));
|
||||
}
|
||||
|
||||
|
@ -57,19 +57,19 @@ public class Delegate {
|
|||
@Test
|
||||
public void doesntEverSetReturn() {
|
||||
Person.countPeople();
|
||||
JUnitStaticEntityMockingControl.playback();
|
||||
AnnotationDrivenStaticEntityMockingControl.playback();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void rejectUnexpectedCall() {
|
||||
JUnitStaticEntityMockingControl.playback();
|
||||
AnnotationDrivenStaticEntityMockingControl.playback();
|
||||
Person.countPeople();
|
||||
}
|
||||
|
||||
@Test(expected=RemoteException.class)
|
||||
public void testVerificationFailsEvenWhenTestFailsInExpectedManner() throws RemoteException {
|
||||
Person.countPeople();
|
||||
JUnitStaticEntityMockingControl.playback();
|
||||
AnnotationDrivenStaticEntityMockingControl.playback();
|
||||
// No calls to allow verification failure
|
||||
throw new RemoteException();
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue