Junit4 模擬requert請求進行測試
阿新 • • 發佈:2019-02-08
package com.hytera.mrps.util; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.mock.web.MockHttpServletRequest; import org.springframework.mock.web.MockHttpServletResponse; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = {"classpath*:/spring.xml"}) public class OnlineUsersTest { private MockHttpServletRequest request; private MockHttpServletResponse response; @Before public void setUp(){ request = new MockHttpServletRequest(); request.setCharacterEncoding("UTF-8"); response = new MockHttpServletResponse(); } @Test public void test() { HttpSession session = request.getSession(true); session.setAttribute("mock", new OnlineUsers()); } }