Tuesday, February 20, 2007
Amock 0.9
This is 0.9 version of AOP based Mock.
Changes
Now can record Exception throwing.
Easy use of comparators.
Changes
Now can record Exception throwing.
Easy use of comparators.
Sample code: SimpleTest.java
package package test;
import static my.amock.util.Will.call;
import static my.amock.util.Will.ret;
import junit.framework.TestCase;
import my.amock.bracket.Mock;
import my.amock.bracket.Story;
import my.amock.check.Check;
import my.amock.util.A;
class DivideAndDivide {
public int ADivideBDivideC(int a, int b, int c) {
DividerSingleton divider = DividerSingleton.getInstance();
int i = divider.divide(a, b);
return divider.divide(i, c);
}
}
class DividerSingleton {
private DividerSingleton() {
}
public static DividerSingleton getInstance() {
// Not yet implemented
return null;
}
public int divide(int a, int b) throws ArithmeticException {
// Not yet implemented
return 0;
}
}
class Dummy {
public void inBoolean(boolean b) {
}
public void inInt(int i) {
}
public void inObject(Object o) {
}
}
public class SimpleTest extends TestCase {
DividerSingleton divider;// mock
DivideAndDivide divideAndDivide;// test target
Dummy dummyMock;
protected void setUp() throws Exception {
// create mock object
new Mock() {{
// private constructor need constructorExact()
divider = A.constructorExact(DividerSingleton.class).invoke();
// if it was public constructor
// divider = new DividerSingleton();
// if it was interface
// divider = A.mock(DividerSingleton.class);
}};
//create normal object
divideAndDivide = new DivideAndDivide();
}
public void testAdderSingleton() {
//recording scenario
new Story() {{
// One time call record for static method with return value
// Only static method need call() method;
//Will.call();
call(divider); DividerSingleton.getInstance();
//Will.ret();
ret(2); divider.divide(4, 2);
ret(1); divider.divide(2, 2);
}};
//test target object
int returnValue = divideAndDivide.ADivideBDivideC(4, 2, 2);
assertEquals(1, returnValue);
}
public void testEqAnyIsNull() {
dummyMock = new Dummy();
//recording scenario
new Story() {{
dummyMock.inInt(Check.anyInt());
dummyMock.inBoolean(Check.anyBoolean());
dummyMock.inObject(Check.any(Dummy.class));
dummyMock.inObject(Check.isNull(Object.class));
dummyMock.inObject(Check.notNull(Object.class));
dummyMock.inObject(Check.isA(Object.class));
}};
dummyMock.inInt(20);
dummyMock.inBoolean(false);
dummyMock.inObject(new Dummy());
dummyMock.inObject(null);
dummyMock.inObject(new Object());
dummyMock.inObject(new Dummy());
}
}