问题描述
我正在模拟一个 HttpServletRequest ,在 servlet 调用中,请求中设置了新值,因为使用相同的请求我们将请求发送到某个 jsp,因此请求对象用作 servlet 的输入对象以及下一页的输出.
i am mocking a HttpServletRequest , in servlet call there are new values getting set in request because using same request we are dispaching request to some jsp so request object is used as a input object to servlet as well as output for next page.
我模拟了所有输入参数,但是对于所有 request.setAttribute(),我的代码什么也没做,因为它是一个模拟类,如果我有
i mocked all input parameters , but for all request.setAttribute() , my code is doing nothing as it's a mocked class , say if i have
我得到 null 因为我没有对 Request.getAttribute("a") 给出任何行为,我不能,这是我对下一页的响应,所以解释我需要 2 行为我的请求对象因此部分模拟,到目前为止,我无法窥探或对其进行任何部分嘲笑.有什么想法吗?
i get null cuz i haven't given any behavious for Request.getAttribute("a") , and i can't , it's my response for next page , so that explain i need 2 behaviour my request object thus partial mocking , and i am unable to spy or do any partial mocking on it so far. any ideas?
代码:
推荐答案
你需要将属性存储到一个集合中:
You need to store attributes into a collection :
这篇关于如何使用 Mockito 部分模拟 HttpServletRequest的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!