使用Mockito模拟HttpSession

编程入门 行业动态 更新时间:2024-10-26 22:24:54
本文介绍了使用Mockito模拟HttpSession的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我需要测试以下控制器方法:

I need to test the following controller method:

@RequestMapping(value="/addLocation") public String addLocation(HttpServletRequest request, HttpSession session) { String location = (String) request.getParameter("plz_ort"); String radius = (String) request.getParameter("umkreis"); ((ArrayList<String>) session.getAttribute("queryTopics")).clone(); ... }

因此,我使用Mockito和JUnit编写了该测试类

Therefor I wrote this test class using Mockito and JUnit

import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; import java.util.ArrayList; import org.junit.Before; import org.junit.Ignore; import org.junit.Test; import org.mockito.InjectMocks; import org.mockito.Mock; import org.mockito.MockitoAnnotations; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.mock.web.MockHttpSession; import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.setup.MockMvcBuilders; public class MyControllerTest { @InjectMocks private MyController myController; private MockMvc mockMvc; @InjectMocks MockHttpSession session; @Before public void setup() { // Process mock annotations MockitoAnnotations.initMocks(this); // Setup Spring test in standalone mode this.mockMvc = MockMvcBuilders.standaloneSetup(MyController) .build(); } @Test public void addLocation_StatusOK() throws Exception { session.setAttribute("queryTopics", new ArrayList<String>(0)); this.mockMvc.perform( post("/addLocation") .param("plz_ort", "PLZ ORT") .param("umkreis", "5")) .andExpect( status().isOk()); } }

如您所见,运行此测试时,我需要模拟会话. 当我调试代码并停止在session.setAttribute("queryTopics", new ArrayList<String>(0));行时,会话对象为org.springframework.mock.web.MockHttpSession@5583d693(包含属性哈希映射{queryTopics=[]}),所以我认为很好. 但是,我在((ArrayList<String>) session.getAttribute("queryTopics")).clone();行中的下一个断点的会话对象是org.springframework.mock.web.MockHttpSession@7545a27f.

As you can see I need to mock the session when running this test. When I debug through the code and stop at the line session.setAttribute("queryTopics", new ArrayList<String>(0)); the session object is org.springframework.mock.web.MockHttpSession@5583d693 (containing a attributes hash map {queryTopics=[]}), so I think that's fine. However the session object of my next breakpoint in line ((ArrayList<String>) session.getAttribute("queryTopics")).clone(); is org.springframework.mock.web.MockHttpSession@7545a27f.

至少它是正确的类型(MockHttpSession),但是它具有不同的ID,因此它实际上是一个全新的对象,因此不包含测试中的会话属性.

At least it's the correct type (MockHttpSession) but it's got a different ID so it's actually a whole new object and thus doesn't contain the session attributes from the test.

您能帮我解决这个问题吗?

Could you please help me out on this one?

推荐答案

模拟MVC将创建一个Spring模拟请求和一个Spring模拟会话,然后调用您的控制器方法.为了在调用controller方法之前在会话中设置某些状态,您需要使用构建器配置模拟请求:

The mock MVC will create a spring mock request and a spring mock session and then invoke your controller method. In order to set some state in the session before the controller method is called, you need to configure the mock request using the builder:

public class MyControllerTest { @InjectMocks private MyController myController; private MockMvc mockMvc; @Before public void setup() { // Process mock annotations MockitoAnnotations.initMocks(this); // Setup Spring test in standalone mode this.mockMvc = MockMvcBuilders.standaloneSetup(MyController) .build(); } @Test public void addLocation_StatusOK() throws Exception { this.mockMvc.perform( post("/addLocation") .param("plz_ort", "PLZ ORT") .param("umkreis", "5") .sessionAttr("queryTopics", new ArrayList<String>(0)) .andExpect( status().isOk()); } }

更多推荐

使用Mockito模拟HttpSession

本文发布于:2023-10-31 07:10:51,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1545391.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Mockito   HttpSession

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!