2024年6月13日发(作者:)

本文由我司收集整编,推荐下载,如有疑问,请与我司联系

从url字符串创建模拟HttpServletRequest?

从url字符串创建模拟HttpServletRequest?[英]Creating a mock

HttpServletRequest out of a url string? I have a service that does some work on an

HttpServletRequest object, specifically using the ameterMap and

ameter to construct an object.

 我有一个服务在HttpServletRequest对象上做了一些工作,特别是使用

ameterMap和ameter来构造一个对象。

  I was wondering if there is a straightforward way to take a provided url, in the form of a

string, say

 我想知道是否有一种直接的方式来获取提供的URL,以字符串的形式,比如说

  String url = “example/?param1=value1 param”; and easily convert it to a

HttpServletRequest object so that I can test it with my unit tests? Or at least just so that

ameterMap and ameter work correctly?

 并轻松将其转换为HttpServletRequest对象,以便我可以使用单元测试进行测试?

或者至少只是为了让ameterMap和ameter正常工作?

  34

 Spring has MockHttpServletRequest in its spring-test module.

 Spring在其spring-test模块中有MockHttpServletRequest。

  If you are using maven you may need to add the appropriate dependency to your

. You can find spring-test at mvnrepository.

 如果您使用的是maven,则可能需要在中添加适当的依赖项。您可以在

mvnrepository找到spring-test。

  36

 Simplest ways to mock an HttpServletRequest:

 模拟HttpServletRequest的最简单方法:

  Create an anonymous subclass: