Link to home
Start Free TrialLog in
Avatar of Jeth01
Jeth01

asked on

How to use different application context for JUnit versus regular execution

Hi,

I have some mock DAOs that I created for some JUnits. I have them autowired with a separate application context file. (They are in a different package and I'm using the component scan) They just return dummy objects instead of fetching from the database. The regular DAOs are through hibernate.
My problem is, I can't figure out how to tell Spring to use that file when running JUnits, and to use the regular XML file otherwise. I want the JUnits to run with the mock DAOs, so we don't need a DB for those. But then when running the application normally, use the real DAOs.
I am fairly new to Spring so this is all a learning experience for me. Any help would be appreciated. Thanks.
One other thing -- I have them in a separate file because they are the same names as the regular DAOs, so that nothing else has to change. But if there is a way to tell Spring to only load the mock ones, or to only use the mock ones for JUnit, then I don't need the separate file.
Avatar of anilallewar
anilallewar
Flag of India image

A feasible option would be to create a factory class that gives you the application context based on do you want it for JUnit or prod. The factory will load the appropriate conf file and return you the application context loaded with the correct XML beans.

E.g.

public ApplicationContext getApplicationContext(String usage){
    ApplicationContext  context = null;
    if("JUnit".equalsIgnoreCase(usage){
                    context = <load from mock DAO context file>
    }else{
                 context = <load from DB DAO context file>
    }
return context;
}
Avatar of Jeth01
Jeth01

ASKER

Ok, I thought about that. But where would I put it? We just have our Spring Dispatcher Servlet defined in the web.xml, so then that picks up the application context.  Could I just overwrite the one that is loaded after the fact? We are using GWT, so I'm not sure where to put that. I guess in onModuleLoad()? Would that work?
Thanks!
ASKER CERTIFIED SOLUTION
Avatar of anilallewar
anilallewar
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
If this doesn't work

 ClassPathXmlApplicationContext(
“application-config.xml”, “test-infrastructure-config.xml” );

Open in new window


use String[]

 ClassPathXmlApplicationContext(new String[] {
“application-config.xml”, “test-infrastructure-config.xml” });

Open in new window



--
Thanks
Subin Sugunan
http://www.subinsugunan.com/
More spring posts
Avatar of Kevin Cross
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.