[英]Using HttpServletRequest Outside of a Servlet
I'm working this API and there is a factory method that sets the current context (request, user, etc). 我正在使用此API,并且有一个工厂方法可以设置当前上下文(请求,用户等)。 The context needs a HttpServletRequest parameter, but I'm not for sure how to initialize it. 上下文需要HttpServletRequest参数,但是我不确定如何初始化它。 I would like this setContext(HttpServletRequest) method to be in the default constructor of the class, how do I go about initializing it so that I can use it with setContext? 我希望此setContext(HttpServletRequest)方法位于该类的默认构造函数中,如何进行初始化,以便可以与setContext一起使用? Is the only way to work with HttpServletRequest objects in a servlet, so I can't use the a java class? 是在Servlet中使用HttpServletRequest对象的唯一方法,因此我不能使用Java类吗?
Here's the library I'm using: http://library.blackboard.com/ref/6760ba98-8f24-44f2-8e65-0dcee799abb8/index.html 这是我正在使用的库: http : //library.blackboard.com/ref/6760ba98-8f24-44f2-8e65-0dcee799abb8/index.html
I'm not sure why you'd want to use it outside of a Servlet context other than testing. 我不确定为什么要在Servlet上下文之外而不是测试中使用它。 For automated tests you can create a mock implementation that includes the features required to get the required functionality to work. 对于自动化测试,您可以创建一个模拟实现,其中包括使所需功能正常运行所需的功能。 Just make sure that the mock implementation behaves the same way as when it's running inside a servlet container. 只要确保模拟实现的行为与在servlet容器中运行时的行为相同即可。
You can either do it using mocks, or better yet: Use HttpUnit ServletUnit 您可以使用模拟或更好的方法:使用HttpUnit ServletUnit
However, keep in mind that this library probably needs to run inside a container: Review your options and, when sure of all the operations "the library" will invoke on the container, simply mock it up. 但是,请记住,该库可能需要在容器中运行:检查您的选项,并且在确定“库”将在容器上调用的所有操作确定后,只需对其进行模拟即可。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.