代码之家  ›  专栏  ›  技术社区  ›  We are Borg

Spring测试:缺少servlet上下文

  •  0
  • We are Borg  · 技术社区  · 8 年前

    我正在为Spring编写测试用例,但很难启动并运行它来加载所有Spring bean。我正在 Missing Servlet Context error 总是我已经为上下文提供了路径,所以我希望文件会自动加载,但在我看来情况并非如此。

    测试控制器:

    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration(locations = {"classpath*:../../main/webapp/WEB-INF/spring/appServlet/servlet-context.xml",
            "classpath*:../../main/webapp/WEB-INF/spring/root-context.xml", "classpath*:../../main/webapp/WEB-INF/spring/appServlet/security-applicationContext.xml"})
    @Ignore
    @ComponentScan("com.tooltank.spring")
    public class TestingController {
    
        ApplicationContext context = new ClassPathXmlApplicationContext("servlet-context.xml","root-context.xml","security-applicationContext.xml");
    
        @Autowired
        protected PersonService personService = (PersonService) context.getBean("personService");
    
        @Autowired
        protected GroupAccountService groupAccountService = (GroupAccountService) context.getBean("groupAccountService");
    
        @Autowired
        protected GroupCanvasService groupCanvasService = (GroupCanvasService) context.getBean("groupCanvasService");
    
        @Autowired
        protected GroupSectionService groupSectionService = (GroupSectionService) context.getBean("groupSectionService");
    
        @Autowired
        protected GroupNotesService groupNotesService = (GroupNotesService) context.getBean("groupNotesService");
    
        @Autowired
        protected GroupMembersService groupMembersService = (GroupMembersService) context.getBean("groupMembersService");
    
        @Qualifier("authenticationManager")
        protected AuthenticationManager am;
    
        @After
        public void clear() {
            SecurityContextHolder.clearContext();
        }
    
        protected void login(String name, String password) {
            Authentication auth = new UsernamePasswordAuthenticationToken(name, password);
            SecurityContextHolder.getContext().setAuthentication(am.authenticate(auth));
        }
    
    }
    

    mvn测试输出:

    Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 2.061 sec <<< FAILURE! - in tests.AllTests
    runTests(tests.AllTests)  Time elapsed: 0.007 sec  <<< ERROR!
    org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'bayeuxInitializer': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private javax.servlet.ServletContext com.tooltank.spring.chat.BayeuxInitializer.servletContext; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [javax.servlet.ServletContext] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
            at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:1380)
            at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1126)
            at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1021)
            at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:545)
            at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88)
            at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:331)
            at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1218)
            at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:543)
    
    Results :
    
    Tests in error: 
      AllTests.<init>:9->TestingController.<init>:26 » BeanCreation Error creating b...
    
    Tests run: 1, Failures: 0, Errors: 1, Skipped: 0
    
    [INFO] ------------------------------------------------------------------------
    [INFO] BUILD FAILURE
    [INFO] ------------------------------------------------------------------------
    [INFO] Total time: 10.740 s
    [INFO] Finished at: 2017-11-27T17:01:26+05:30
    [INFO] Final Memory: 53M/507M
    

    知道我做错了什么吗?非常感谢。

    使现代化

    更新的测试控制器:

    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration(locations = {"classpath*:../../main/webapp/WEB-INF/spring/appServlet/servlet-context.xml",
            "classpath*:../../main/webapp/WEB-INF/spring/root-context.xml", "classpath*:../../main/webapp/WEB-INF/spring/appServlet/security-applicationContext.xml"})
    @Ignore
    @ComponentScan("com.tooltank.spring")
    @WebAppConfiguration
    public class TestingController {
    
     @Autowired
        protected PersonService personService;
    
        @Autowired
        protected GroupAccountService groupAccountService;
    // othre autowired
    }
    

    错误日志:

    Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.tooltank.spring.service.PersonService] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
            at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:1380)
            at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1126)
            at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1021)
            at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:545)
            at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88)
    

    PersonServiceImpl:

    @Service(value = "personService")
    @Transactional
    public class PersonServiceImpl implements PersonService {
    
        private final PersonDAO personDAO;
    
       @Autowired
        public PersonServiceImpl(PersonDAO personDAO) {
            this.personDAO = personDAO;
        }
    }
    
    2 回复  |  直到 8 年前
        1
  •  0
  •   Mehraj Malik    8 年前
    @Autowired
    private ApplicationContext context;
    

    试试这个,应该管用,

        2
  •  0
  •   We are Borg    8 年前

    所以

    问题是Comet的websockets库。我们一注释完websocket功能,测试就开始工作了。这是我们评论的2个库。

    <dependency>
                      <groupId>org.cometd.java</groupId>
                      <artifactId>cometd-java-websocket-javax-server</artifactId>
                      <version>3.1.2</version>
                  </dependency>
                  <dependency>
                      <groupId>javax.websocket</groupId>
                      <artifactId>javax.websocket-api</artifactId>
                  </dependency>
            <dependency>
                <groupId>org.cometd.java</groupId>
                <artifactId>cometd-java-websocket-common-server</artifactId>
                <version>3.1.2</version>
            </dependency>