1 Reply Latest reply on Dec 8, 2016 2:16 AM by Vagharshak Baghdasaryan

    Arquillian: inject WebApplicationContext

    Vagharshak Baghdasaryan Newbie

      Hello,

       

      I have following problem:

       

      I am testing a class A, which has @Autowired org.springframework.context.ApplicationContext.WebApplicationContext bean.

       

      During live run WebApplicationContext bean is setted via Spring magic.

       

      In Arquillian test, the bean should be created explicitely, say in a @Configuration annotated java Spring config file, which then can be added via @SpringAnnotationConfiguration to the test class declaration.

       

      I tried to make it like this:

       

      @Bean   public WebApplicationContext webApplicationContext() 
      {  
          return new AnnotationConfigEmbeddedWebApplicationContext();   
      }
      

      But this is bot enogh, bacause during run the code under test xebApplicationContext..getServletContext() is calles, which is NULL.

       

      My question: how can I inject WebApplicationContext bean toghether with servletContext in a clean and wright way?

       

      We don't use Spring MVC and don't have some web.xml files with explicit given servlet names

       

      Thank You in advice.

       

      Regards,

       

      Vagharshak

       

      TWebAppliicationContextZTT

        • 1. Re: Arquillian: inject WebApplicationContext
          Vagharshak Baghdasaryan Newbie

          I have solved the problem by creating the bean like this:

           

          @Bean

            public WebApplicationContext webApplicationContext() {

            AnnotationConfigEmbeddedWebApplicationContext ctx = new AnnotationConfigEmbeddedWebApplicationContext();

            StandardContext sCtx = new StandardContext();

            StandardContext p1 = new StandardContext();

            StandardEngine p2 = new StandardEngine();

            p2.setService(new StandardService());

            p1.setParent(p2);

            sCtx.setParent(p1);

           

           

            ctx.setServletContext(new ApplicationContextFacade(new ApplicationContext(sCtx)));

           

           

            return ctx;

           

           

            }