IS there any way I can mock behaviour of an object created inside my Test class static method? Like, I have class like below:
class Main{
public static void main(String[] args){
AnnotationConfigApplicationContext cxt = new AnnotationConfigApplicationContext();
SomeMapper map = cxt.getBean(SomeMapper.class);
map.getData();
}
}
The mapper 'SomeMapper' is ibatis query mapper for fetching data from DB. Can I right a test class for this method using Mockito framework? Or can just mock behaviour of mapper class or ApplicationContext class?
Instead of trying to test your
mainmethod, lift the actual logic into a standard class and test that instead. Leave yourmainmethod to handle things that are explicitly concerned with launching your app. For example, you might break it up like this:With just a single method this definitely looks like overkill, but presumably your actual code does something more complex than this.