I am creating Junit test cases in Spring.
private static BeanFactory servicefactory = null;
private static BookingProcessService bookingProcessService;
@BeforeClass
public static void setUpBeforeClass() throws Exception{
try{
String[] configFiles = {"applicationcontext-Service.xml","applicationcontext-Hibernate.xml","applicationContext-dao.xml"};
ClassPathXmlApplicationContext appContext = new ClassPathXmlApplicationContext(configFiles);
servicefactory = (BeanFactory) appContext;
bookingProcessService = (BookingProcessService) servicefactory.getBean("bookingProcessService");
}catch (Exception e){
e.printStackTrace();
}
}
Here BookingProcessService is an interface and its implementation class is BookingProcessServiceImpl.java.
In spring configuration files, there is no bean id defined for that.
Is there any way I can use the ‘bookingProcessService’ for invoking the actual method definition written in BookingProcessImp.java in my test methods?
If u are configuring this test I recommend to use Annotations, will be more clear.
Usually in your “applicationcontext-Service.xml” must have the bean defined with the Implementation, and is the classes where you would use the Interface of this class.
With that you would not coupling code with implementation.
In this example, simple must do the next:
The annotation @Autowired from Spring will do the IOC from your xml configuration “applicationcontext-Service.xml”.