Amock is AspecJ based mock library.
It will give you java mock which you have dreamed.
With Amock, you
* Can mock static method and constructor.
-> You can unit test with singleton.
* Can create mock object of a class which have only private constructor.
* Can create mock instance of interface and normal class instance.
* Can create mock object without any parameter even if the class does not have default constructor.
-> You don’t need to create expensive object just for mock constructor.
* Can access private method and attribute easily.
* Can mock private and protected method.
* Can use argument checker (argument matcher ‘eq(). any()…’) only where it is needed.
* Don’t need extending specific class.Prerequisite
JDK 1.5 version
http://java.sun.com/javase/downloads/index_jdk5.jsp
Eclipse 3.2
http://www.eclipse.org/downloads/
AJDT 1.4.0
Download Amock libarary and documentation from
http://www.box.net/shared/rmlnpaq0n2
and CGlib no dependency 2.1_3
http://prdownloads.sourceforge.net/cglib/cglib-nodep-2.1_3.jar?download
Download Amock here
http://www.box.net/shared/rmlnpaq0n2
Home page
http://amock.blogspot.com/
Example
package my.amock.example;
import static my.amock.util.Will.*;
import static my.amock.util.A.*;
import static my.amock.check.Check.*;
import my.amock.bracket.Story;
import my.amock.util.A;
import my.amock.util.Will;
import junit.framework.TestCase;
public class SimpleExampleTest extends TestCase {
private DummyClass dummyMock;
//private DummyInterface dummyMock;
public void setUp() {
//private constructor need A.mock()
//and when you use it you don’t have to specify parameter even if constructor need parameter
dummyMock = mock(DummyClass.class);
// if it was public constructor
//dummyMock = new DummyClass();
// if it was interface
//dummyMock = mock(DummyInterface.class);
}
public void testSimple() {
new Story() {{
//Recording is done in this block.
//Record Method call
dummyMock.noReturn(3);
//Record Method call with expected return value;
a(dummyMock.returnThis(1)).ret(1);
//Record Method call with expected return value;
a(dummyMock.multiply(0,anyInt())).ret(1);
//Record Method call with expected expected calling times and expected Exception to throw;
a(dummyMock.devide(anyInt(),eq(0))).times(2).toss(new ArithmeticException());
//Record Method call with expected expected calling times;
dummyMock.noReturn(1);a().times(2);
//Record static method call with expected expected return value
call();a(DummyClass.staticReturnThis(1)).ret(1);
//Record constructor call with expected expected return value
call();a(new Integer(1)).ret(new Integer(3));
//Record static method call with expected expected return value
call();a(DummyClass.getInstance()).ret(dummyMock);
a(dummyMock.toString()).ret("dummy");
}};
dummyMock.noReturn(3);
assertEquals(1, dummyMock.returnThis(1));
assertEquals(1, dummyMock.multiply(0,7));
try {
dummyMock.devide(3,0);
fail("should not reach here");
} catch (ArithmeticException e) {}
try {
dummyMock.devide(5,0);
fail("should not reach here");
} catch (ArithmeticException e) {}
dummyMock.noReturn(1);
dummyMock.noReturn(1);
assertEquals(1, DummyClass.staticReturnThis(1));
assertEquals(3, new Integer(1).intValue());
assertEquals(dummyMock, DummyClass.getInstance());
assertEquals("dummy", dummyMock.toString());
}
public void testField() {
Object original = A.field(dummyMock, "privateFinal").get();
Object newOne = new Object();
A.field(dummyMock, "privateFinal").set(newOne);
Object changed = A.field(dummyMock, "privateFinal").get();
assertNotSame(original, changed);
assertEquals(newOne, changed);
}
public void testMethod() {
final my.amock.util.Method privateMethod = A.methodExact(dummyMock, "privateMethod", int.class);
new Story() {{
a(privateMethod.invoke(3)).ret(5);
}};
assertEquals(5, privateMethod.invoke(3));
}
public void testAssign() {
String a = "a";
String ab = "ab";
A.assign(a, ab);
assertEquals("ab", a);
}
}