Spring测试:缺少servlet上下文

时间:2017-11-27 11:49:17

标签: java spring maven spring-mvc servlets

我正在为Spring编写测试用例,但是很难让它运行起来加载所有的spring bean。我一直在Missing Servlet Context error。我已经给出了上下文的路径,所以我预计,文件会被自动加载,但在我看来并非如此。

TestingController:

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

}

我复制了3个xml文件并将它们添加到资源中,以便我可以直接访问它们。

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 个答案:

答案 0 :(得分:0)

select max(case when fieldname = 'GEID' then fieldData end) as GEID,
       max(case when fieldname = 'Username' then fieldData end) as Username,
       . . .
from (select t.*,
             (select count(*) from t t2 where t2.id <= t.id and t2.fieldName = 'GEID'
             ) as grp
      from t
     ) t
group by grp;

试试这个,它应该可行,

答案 1 :(得分:0)

所以,

问题在于来自cometd的websockets库。一旦我们评论出websocket功能,测试就开始起作用了。这些是我们评论的2个库。

 #second_row {
     position: fixed;
     bottom: 0;
     left: 0;
     width: 100%;
 }