Hi: Normally you use mock (java agent) to replace an object in the class.
For example:
public class ClassA{
public ClassB realObjectB;
public ClassC realObjectC;
public void Method1(){
realObjectC = new ClassC();
realObjectB = new ClassB(realObjectC);
}
...
...
}
public class ClassB {
public void ParticularInhereitedMethod(){
many dependency objects involved...
}
}
of course, ClassB and ClassC have many other dependency, for example connection pool.
When I do testing, may I do:
public class Mock extends ClassB{
@override
public class ParticularInhereitedMethod(){
Make clean codes, no dependency.
}
}
In test application:
public class MainClass{
public static void main(String[] args){
ClassA a = new ClassA();
ClassM m = new Mock();
a.realObjectB = m;
manipulate m;
}
}
Is this above method works in real testing? Why do I have to use Java agent(byte code replacement)?
I suggest to use a Mockito http://code.google.com/p/mockito/ and Spring injection to resolve your problem.
With mockito tou can declare the return parameter on an object from the parameters, with spring you can resolve the instance of the object at runtime