Few days ago a friend of mine showed me a nice project: StubOut. When you have strong dependencies between 2 classes in your project it is a very hard process to test each class separately. In this case StubOut makes a great job, it allows you replace one class with a mock class during the the test or to replace just some of the methods with mock methods.
Example :
package test;
public class A {
private B b=new B();
public int sum(int t){
return b.getInt()+t;
}
}
package test;
public class B {
public int getInt(){
return 10;
}
}
And the test case will be like this:
import us.blanshard.stubout.Stubber;
import us.blanshard.stubout.StubbingTestCase;
public class ATest extends StubbingTestCase{
@Override
public void beforeReloading() {
Stubber.replaceClass(”test.B”,Mock.class);
}
public void testSum() {
A a=new A();
assertEquals(1, a.sum(1));
}
}
package test;
public class Mock{
public int getInt(){
return 0;
}
}
Nice !!
In theory if each component is decoupled by an interface you will not use this library . But in reality …………….
Check the site of the project for more details