After upgrading from Spring Boot 2.7.13 to 2.7.14, all tests using WebApplicationContextRunner started failing.
To Reproduce 1. Create a pom.xml:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.14</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>11</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
- Create an application:
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
- Create a classic integration test:
package com.example.demo;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class DemoApplicationTest {
@Test
void contextLoads() {
}
}
- Create a test using
WebApplicationContextRunner:
package com.example.demo;
import static org.assertj.core.api.Assertions.assertThat;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.runner.WebApplicationContextRunner;
import org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext;
class DemoApplicationUsingWebApplicationContextRunnerTest {
private final WebApplicationContextRunner contextRunner = new WebApplicationContextRunner(AnnotationConfigServletWebServerApplicationContext::new);
@Test
void contextLoads() {
contextRunner
.withUserConfiguration(DemoApplication.class)
.run(applicationContext -> assertThat(applicationContext).hasNotFailed());
}
}
Expected behaviour Both tests passed.
Actual behaviour
- Using Spring Boot 2.7.13, both tests passed.
- Using Spring Boot 2.7.14, DemoApplicationUsingWebApplicationContextRunnerTest is failing with the following stack trace
java.lang.NoClassDefFoundError: javax/servlet/ServletContext
at com.example.demo.DemoApplicationUsingWebApplicationContextRunnerTest.<init>(DemoApplicationUsingWebApplicationContextRunnerTest.java:11)
Caused by: java.lang.ClassNotFoundException: javax.servlet.ServletContext
at com.example.demo.DemoApplicationUsingWebApplicationContextRunnerTest.<init>(DemoApplicationUsingWebApplicationContextRunnerTest.java:11)
Comment From: wilkinsona
I suspect you have a corrupted dependency in your local Maven cache that has made javax.servlet.ServletContext unavailable. Please try purging Maven's local cache.
Comment From: zeljko-mirovic
Purging the local maven cache solved the issue. I appreciate your help, and sorry for bothering you with something like this.
Comment From: wilkinsona
No problem. Thanks for letting us know.