Issue
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
Solution
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
Answered By - Jens
Answer Checked By - Marilyn (JavaFixing Volunteer)