Java单元测试 MockMvcBuilders.webAppContextSetup返回404,No mapping found for HTTP request with URI

前几天做在某个项目上做单元测试,始终报No mapping found for HTTP request with URI,报404

一通baidu,google,stackoverflow, 基本有以下几个原因

一、检查RequestBuilder的路径是否跟Controller里的一致

        RequestBuilder request = MockMvcRequestBuilders.get("/corp-statistics/vm/download-corp-shop-daily-sales-v2")

 

二、检查ContextConfiguration里的参数是否有效

//测试环境使用,用来表示测试环境使用的ApplicationContext将是WebApplicationContext类型的;value指定web应用的根;
@WebAppConfiguration(value = "src/main/webapp")

@RunWith(SpringJUnit4ClassRunner.class)
//指定容器层次,即spring-config.xml是父容器,而spring-mvc.xml是子容器
@ContextHierarchy({
        @ContextConfiguration(name = "parent", locations = "classpath:spring-application.xml"),
        @ContextConfiguration(name = "child", locations = "classpath:spring-mvc-servlet.xml")
})
//声明一个事务管理 每个单元测试都进行事务回滚 无论成功与否
@Transactional

public class BaseTest {
    @Autowired
    private WebApplicationContext wac;

    protected MockMvc mockMvc;
    protected MockHttpServletRequest request;
    protected MockHttpServletResponse response;

    @Before
    public void setUp() throws Exception{
        mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build();
        //        mockMvc = MockMvcBuilders.standaloneSetup(new GoodNeighborController()).build();

    }
}

 

网上有很多文章ContextConfiguration是这么定义的

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath*:/*.xml"})
public class CDPlayerTest {
}

其中classpath的具体含义见:https://blog.csdn.net/linzhiji/article/details/104780126

这个地方我检查了很久,追溯MockMvc源码,发现根本没有遍历Controller,加入到RequestMappingHandlerMapping里,导致MockMvcRequestBuilders 处理的时候找不到对应的handle(具体Sping MVC初始化流程见https://blog.csdn.net/flowingflying/article/details/73368883

我遇到的情况是,在启动Tomcat的时候,对应的URI是能找到Controller进行处理的,而单元测试的时候却不行

三、最终的问题

再次debug,发现MockMvc加载 ApplicationContext 没问题,就是没加载到ServletContext,

再回头看classpath,以及本项目的结构,发现项目中原来的Spring-mvc-servlet.xml是放在1位置的,没在Classptah(Resource)里,把Spring-mvc-servlet.xml挪到2

再改代码

@ContextHierarchy({
        @ContextConfiguration(name = "parent", locations = "classpath:spring-application.xml"),
        @ContextConfiguration(name = "child", locations = "classpath:spring-mvc-servlet.xml")
})

这次终于可以。整整花了2天时间找这个问题。期间找到的文章https://blog.csdn.net/linzhiji/article/details/104743435

No mapping found for HTTP request with URI [/spmvctst/sigup.do] in DispatcherServlet with name 'springMVC'表示在DispatcherServlet中找不到对应的URI映射。这个错误常见于Spring MVC框架中。根据引用和引用的警告信息,这个错误可能是因为没有在DispatcherServlet的配置文件中正确配置URI映射的处理器(Handler)。具体来说,可能是没有在配置文件中添加对应的@Controller或@RequestMapping注解的类,或者没有在配置文件中配置<mvc:annotation-driven/>和<context:component-scan/>标签。引用中提到了这两个标签的作用,其中<context:component-scan/>用于扫描@Controller注解的类,而<mvc:annotation-driven/>用于启动Spring MVC的注解功能。 为解决这个问题,你可以按照以下步骤进行操作: 1. 确认你的DispatcherServlet配置文件中是否正确配置了@Controller注解的类。确保这些类被正确扫描到。可以使用<context:component-scan/>标签指定要扫描的包。 2. 确认你的DispatcherServlet配置文件中是否配置了<mvc:annotation-driven/>标签。这个标签用于启动Spring MVC的注解功能,确保HandlerMapping、HandlerAdapter和ExceptionResolver等处理器被正确注册。 3. 如果上述步骤都正确配置了,但仍然出现错误,请检查你的URI是否与配置文件中的URI映射匹配。确认是否存在拼写错误或者路径错误。 通过以上步骤,可以解决"No mapping found for HTTP request"的错误,确保DispatcherServlet能够找到正确的URI映射并处理请求。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [No mapping found for HTTP request with URI [/.../...] in DispatcherServlet](https://blog.csdn.net/qq_45592174/article/details/112914031)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* *3* [No mapping found for HTTP request with URI [/user/login.do] in DispatcherServlet with name 'dispatch](https://blog.csdn.net/Drrier/article/details/79892351)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值