0

I am using the following dependency for spring test and it is getting failed to load the application context while running.

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.4.3.RELEASE</version>
    <relativePath/> 
</parent>
<dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <!--<version>2.1.8.RELEASE</version>-->
        <scope>compile</scope>
        <!--<scope>compile</scope>-->
    </dependency>

Inside some of the cases I am using

@Autowired
    AutowireCapableBeanFactory beanFactory;

and getting the mentioned error while running test cases

java.lang.NoClassDefFoundError: javax/servlet/ServletException

at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:264)
at org.springframework.boot.SpringApplication.createApplicationContext(SpringApplication.java:587)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:311)
at org.springframework.boot.test.context.SpringBootContextLoader.loadContext(SpringBootContextLoader.java:111)
at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContextInternal(DefaultCacheAwareContextLoaderDelegate.java:98)
at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:116)
Caused by: java.lang.ClassNotFoundException: javax.servlet.ServletException
    at java.net.URLClassLoader.findClass(URLClassLoader.java:387)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:418)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:352)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:351)
    ... 29 more
divyanshu gupta
  • 127
  • 1
  • 3
  • 14

1 Answers1

1

You miss the servlet api in your claspath. Add:

<!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>javax.servlet-api</artifactId>
    <version>4.0.1</version>
    <scope>test</scope>
</dependency>

to your pom

Jens
  • 67,715
  • 15
  • 98
  • 113
  • Adding this dependency changed the error to java.lang.IllegalStateException: Failed to load ApplicationContext Caused by: org.springframework.context.ApplicationContextException: Unable to start embedded container; nested exception is org.springframework.context.ApplicationContextException: Unable to start EmbeddedWebApplicationContext due to missing EmbeddedServletContainerFactory bean. – divyanshu gupta Nov 03 '22 at 11:01
  • https://stackoverflow.com/questions/21783391/spring-boot-unable-to-start-embeddedwebapplicationcontext-due-to-missing-embedd – Jens Nov 03 '22 at 11:03
  • Thanks, this resolved my problem.having a doubt that why we need to add these dependencies explicitly as i am already using spring-boot-starter-parent.these all are not imported from parent? – divyanshu gupta Nov 03 '22 at 11:17
  • As i know spring-boot-starter-test does not have the servlet-api as a dependency – Jens Nov 03 '22 at 11:28