I'm new to Mocking frameworks. The framework which I'm now working on is JMockit.I have a doubt. Forgive me if my understandings on this topic is very poor.
I have a class A which extends an abstract class. The class is like this:
class A extends AbstractClass{
private B b = UtilClass.getBean("b");
private C c = UtilClass.getBean("c");
.........
.........
.........
}
The UtilClass is like this:
public final class UtilClass{
private static UtilContext context = new UtilContext();
//getBean method which returns a bean object
}
i want to mock the getBean method. So to mock this method I have to mock the UtilContext class. If it is instantiating some other class, then i have to mock that too. This way i have to mock lots of classes, which is not a good idea. Please help me.
If you only need to access the getBean method in the UtilClass, you can mock the whole UtilClass and disable its static initializations with the $clinit method (it will not disable the static initializations that are resolved at compile time (constants)):
new MockUp<UtilClass>() {
@Mock
void $clinit() {
//disable static initialization
}
@Mock
public static SomeType getBean(String s) {
if("b".equals(s)) return new B();
if("c".equals(s)) return new C();
...
}
};
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With