-1

I am struggling with my spring MVC web app. The app itself looks to work correctly. 2 days back i wanted to make some JUnit tests for my classes and i've read sth how to make it with Spring.

So i created my Test Class and i used below annotation to run tests with Spring and to load a proper contexts.

@RunWith(SpringJUnit4ClassRunner.class)
@ActiveProfiles(profiles = "test")
@ContextConfiguration(locations = {"classpath:/dispatcher-servlet.xml","classpath:/beans.xml","classpath:/jpaContext.xml"}

and when i try to run this test through maven or just by eclipse, im getting following error:

java.lang.IllegalStateException: Failed to load ApplicationContext
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'messageSource' defined in class path resource [dispatcher-servlet.xml]: Initialization of bean failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration': Injection of autowired dependencies failed; nested exception is java.lang.NoClassDefFoundError: javax/servlet/ServletContext

Can anyone tell me why it happens ? App works correctly without testing and all beans are loaded without any issue.

WHy i have such a problem with tests?

Thanks in advance ! Jan

Johnny
  • 11
  • 4
  • Have you actually read the stacktrace... It is telling you that you are missing a class, hence you need to add a dependency. – M. Deinum Sep 28 '16 at 14:42

1 Answers1

0

You should add servlet-api as a dependency.

<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>javax.servlet-api</artifactId>
    <version>3.0.1</version>
    <scope>provided</scope>
</dependency>

Check this answer for more information.

Community
  • 1
  • 1
djointster
  • 346
  • 3
  • 12