Getting Java.Lang.Classnotfoundexception: Javax.Servlet.Servletcontext in Junit

Getting java.lang.ClassNotFoundException: javax.servlet.ServletContext in JUnit

You have a single xml file for your ApplicationContext in this file there is a <mvc:annotation-driven /> tag. This tag loads different web related resources (view resolvers, handler mappings etc.) and as such requires the servlet api to be available.

You already should have the servlet api on your classpath as a provided dependency in maven.

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

Next to that you might want to remove the <mvc:annotation-driven /> tag and put it in a seperate configuration file. This is also a tag which should (in general speaking) be loaded by the DispatcherServlet. (I assume here the applicationContext.xml is, as default, loaded by the ContextLoaderListener).

Spring boot - java.lang.ClassNotFoundException: javax.servlet.ServletContext and Unable to start EmbeddedWebApplicationContext

The issue was with the build.gradle

provided "org.springframework.boot:spring-boot-starter-tomcat"

Intellij wasn't happy with the provided

as soon as I switched to

compile "org.springframework.boot:spring-boot-starter-tomcat"

the application worked

java.lang.NoClassDefFoundError: javax/servlet/ServletContext

OK here is what I had to do to solve this specific issue:

  1. IntelliJ needed to have a definition for Gradle Task called bootRun within the Debug/Run Configuration. This allows me to now run back-end code or debug back-end code updates in IntelliJ.

  1. Next I had to restructure my IntelliJ directories because IntelliJ 15 had installed both a parent and a child JRE folder which is incorrect so therefore IntelliJ displayed and exception stating that it could not find the a) java.exec nor the b) jvm.cfg files under it's JRE/Lib sub-directory.

Once I fixed these two issues both builds and debugging seemed to work for Java Spring back end. I followed up with IntelliJ about the quirky JRE installation.

Error when trying out SPRING (java.lang.NoClassDefFoundError: javax/servlet/ServletContext)

You must use the same version for all Spring dependencies:

        <dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>5.3.3</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.3.3</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.3.3</version>
</dependency>

Please update at least all components to version 5.3.3 or to the latest available version.

Spring is a project with multiple modules: https://github.com/spring-projects/spring-framework and different versions of Spring components can throw many different exceptions like: java.lang.NoSuchMethodError, java.lang.NoClassDefFoundError and etc.

Java Spring MVC - java.lang.NoClassDefFoundError: javax/servlet/ServletContext

You need to use @WebAppConfiguration annotation in your test class. Here is what the documentation states:

@WebAppConfiguration A class-level annotation that is used to declare
that the ApplicationContext loaded for an integration test should be a
WebApplicationContext. The mere presence of @WebAppConfiguration on a
test class ensures that a WebApplicationContext will be loaded for the
test, using the default value of "file:src/main/webapp" for the path
to the root of the web application (i.e., the resource base path). The
resource base path is used behind the scenes to create a
MockServletContext which serves as the ServletContext for the test’s
WebApplicationContext.

This works with provided scope of javax.servlet-api.

Besides, you can't use @EnableWebMvc in the configuration class you use for testing. Best if you create a separate configuration class that will be only used for testing, without @EnableWebMvc. You can check out this discussion: java.lang.IllegalArgumentException: A ServletContext is required to configure default servlet handling

Spring @ControllerAdvice - NoClassDefFoundError: javax/servlet/ServletException

It doesn't require for a standalone jar application: providedRuntime 'org.springframework.boot:spring-boot-starter-tomcat'. Starter-web includes one

When you've declared this dependency you said you build to exclude tomcat for a standalone run. This means - no servlet library inside


If you deploy this app somewhere, then just exclude tomcat from starter-web, like this:

compile('org.springframework.boot:spring-boot-starter-web'){
exclude module: "spring-boot-starter-tomcat"
}


Related Topics



Leave a reply



Submit