Issue
I am writing test-cases for Spring, but having a really hard time getting it up and running to load all spring beans. I am getting Missing Servlet Context error
all the time. I have given paths for context, so I expected, that the files would be automatically loaded, but seems to me that's not the case.
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));
}
}
I have copied the 3 xml files and added them in resources so I can directly access them.
mvn test output :
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
Any ideas what I am doing wrong? Thank you.
Update
Updated testing controller :
@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
}
Error log :
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;
}
}
Solution
So,
THe issue was with websockets libraries from cometd. As soon as we commented out websocket functionality, tests started working. These are the 2 libraries we commented.
<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>
Answered By - We are Borg