Tuesday, June 26, 2007
Amock 9.6
1. Features
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.
Can change final variable.
Can change String value.
Don’t need extending specific class.
2. Setup environment
2.1. 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
http://www.eclipse.org/downloads/AJDT 1.4.0
Download Amock libarary and documentation from
and CGlib no dependency 2.1_3
2.2. Setup eclipse
To use this libraryFirst change project to AspectJ project.
Second, Add 'Amock-bin.jar' in your AspectJ Aspect path.
Third, Add 'Amock-bin.jar' and 'cglib-nodep-2.1_3.jar' in your Class path.
Now you ready to use Amock.
3. Sample 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);
}
}
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);
}
}