Anand
Anand

Reputation: 21310

Spring JUnit files loading issue

I am using the below piece of code for JUnit test of my Spring maven application

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();
    }
}

While building my project using maven, I am getting some errors.The bean named bookingProcessBusiness is not creating may be because the spring configuartion files are not properly loaded.

Can somebody please help me?

Upvotes: 0

Views: 96

Answers (1)

Tomasz Nurkiewicz
Tomasz Nurkiewicz

Reputation: 340708

  1. Please provide exact exception you are getting

  2. Yes, the exception that you are swallowing instead of throwing further and letting JUnit know something is wrong. Don't do this:

    }catch (Exception e){
      e.printStackTrace();
    }
    
  3. Why won't you use built-in Spring support for integration testing?

    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration(locations={"applicationcontext-Service.xml","applicationcontext-Hibernate.xml","applicationContext-dao.xml"})
    public class BookingProcessServiceTest {
    
        @Autowired
        private BookingProcessService bookingProcessService;
    
    }
    

    No glue code, no statics.

Upvotes: 4

Related Questions