You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Spring MVC整合JUnit测试初始化错误:找不到ServletContext类

解决JUnit测试Spring MVC时的初始化错误(NoClassDefFoundError: javax/servlet/ServletContext)

首先,你的错误堆栈已经明确指出了核心问题:java.lang.NoClassDefFoundError: javax/servlet/ServletContext,这说明测试运行时的类路径中缺少Servlet API相关的类支持。下面是具体的解决步骤和代码优化建议:

1. 修复Maven依赖,补充Servlet API测试范围依赖

你在测试类中使用了@WebAppConfiguration注解,这个注解需要Servlet API的类来支撑,但默认情况下,Maven中servlet-api依赖的scope是provided(表示仅在编译和容器运行阶段提供,测试阶段不会自动加载)。你需要在pom.xml中添加一个测试范围的Servlet API依赖:

<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>javax.servlet-api</artifactId>
    <version>4.0.1</version> <!-- 根据你的Spring版本选择对应版本,Spring 5建议使用Servlet 4+ -->
    <scope>test</scope>
</dependency>

添加完成后重新执行测试,这个类找不到的问题应该就能解决。

2. 修正TestBeanConfig配置类的注解错误

你的TestBeanConfig.java中错误地使用了@ContextConfiguration注解——这个注解是专门用于测试类上指定Spring配置来源的,配置类本身只需要@Configuration和@ComponentScan即可。修改后的配置类如下:

package com.mkyong.controller;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

@Configuration
@ComponentScan(basePackages = "com.mkyong.controller")
public class TestBeanConfig {
    @Bean
    MultiplicationService multiplicationservice() {
        return new MultiplicationService();
    }

    @Bean
    NumberDAO numberDao() {
        return new NumberDAO();
    }
}

3. 测试类的简化优化(可选)

你的测试类逻辑是正确的,但可以去掉一些不必要的代码,让结构更简洁:

  • 移除手动添加的getter/setter方法,@Autowired已经完成了依赖注入
  • 确保MultiplicationService和NumberDAO类确实存在于com.mkyong.controller包(或@ComponentScan覆盖的包)中,避免其他类找不到的问题

修改后的测试类示例:

package com.mkyong.controller;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;

import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;

@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextConfiguration(classes={TestBeanConfig.class})
public class MultiplicationServiceTest {
    @Autowired
    private WebApplicationContext wac;
    
    private MockMvc mockMvc ;

    @Before
    public void setup() {
        this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build();
    }

    @Test
    public void testHandleMultiply() throws Exception {
        mockMvc.perform(post("/multiply")
                        .param("number1","7")
                        .param("number2","7"))
                .andExpect(model().attribute("result", "49"))
                .andExpect(redirectedUrl("/multiply.jsp"));
    }
}

最后验证步骤

  1. 保存所有修改后的代码
  2. 执行mvn clean test重新运行测试
  3. 如果仍有类找不到的问题,检查对应类的包路径是否和@ComponentScan的范围匹配,或者是否已正确添加到类路径中

内容的提问来源于stack exchange,提问作者Disha Agrawal

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 04:46:21