Tuesday, June 26, 2007
Amock 9.5
With Amock, You can
- mock static method and constructor.
- mock private method.
- create mock instance of private constructor class, interface and of course normal class instance.
- create mock instance without any parameter input when class have only parameter constructor.
- access private method and attribute easily.
- use argument checker (argument matcher 'eq(). any()...') only where it is needed.
...
All you need to do is install aspectj eviroment and add Amock library to aspect path and class path.
It is FREE !
Download here
http://www.box.net/shared/2r5pkpkzpi
Sample code.
- mock static method and constructor.
- mock private method.
- create mock instance of private constructor class, interface and of course normal class instance.
- create mock instance without any parameter input when class have only parameter constructor.
- access private method and attribute easily.
- use argument checker (argument matcher 'eq(). any()...') only where it is needed.
...
All you need to do is install aspectj eviroment and add Amock library to aspect path and class path.
It is FREE !
Download here
http://www.box.net/shared/2r5pkpkzpi
Sample code.
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 = A.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(),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);
}};
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());
}
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);
}
}