欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

#Mockito#_mockito_mockHttpSession_使用示例

程序员文章站 2022-04-27 10:17:36
...
@RunWith(PowerMockRunner.class)
@PowerMockIgnore("javax.management.*")
public class UserRestControllerTest {

    @InjectMocks
    private UserRestController userRestController;


    private MockMvc mockMvc;

    @Mock
    private UserService userService;

    @Mock
    private User userInfo;

    @Mock
    private SyslogServiceImpl syslogServiceImpl;

    @Mock
    private Map user;

    @Mock
    private BaseRedisUtils baseRedisUtils;

    @Mock
    HttpServletRequest httpServletRequest;

    @InjectMocks
    MockHttpSession session;//引入mockhttpsession
   

    @Before
    public void setup() {

        mockMvc = MockMvcBuilders.standaloneSetup(userRestController).build();
    }


    @Test
    public void loginTest_0() throws Exception {
        MemberModifier.field(UserRestController.class, "baseRedisUtils").set(userRestController,baseRedisUtils);
        Mockito.when(baseRedisUtils.getBucket(RedisCommonKey.NG_KEY_PREFIX + "verification.enabled")).thenReturn("0");
        MvcResult mvcResult = mockMvc.perform(MockMvcRequestBuilders.get("/user/login")
                .param("username","ac1")
                .param("password","111111")
                .param("veryCode","0000")
                .param("randCodeStr","12222222222")
                .param("loginErrorCount","3")
                .param("loginUnlockMinute","3")
        ).andExpect(MockMvcResultMatchers.status().isOk())
                .andDo(MockMvcResultHandlers.print())
                .andReturn();
        String content = mvcResult.getResponse().getContentAsString();
        assertNotNull(content);
    }

    @Test
    public void loginTest_1() throws Exception {
        MemberModifier.field(UserRestController.class, "baseRedisUtils").set(userRestController,baseRedisUtils);
        Mockito.when(baseRedisUtils.getBucket(RedisCommonKey.NG_KEY_PREFIX + "verification.enabled")).thenReturn("1");
        MvcResult mvcResult = mockMvc.perform(MockMvcRequestBuilders.get("/user/login")
                .param("username","ac1")
                .param("password","111111")
                .param("veryCode","0000")
                .param("randCodeStr","12222222222")
                .param("loginErrorCount","3")
                .param("loginUnlockMinute","3")
        ).andExpect(MockMvcResultMatchers.status().isOk())
                .andDo(MockMvcResultHandlers.print())
                .andReturn();
        String content = mvcResult.getResponse().getContentAsString();
        assertNotNull(content);
    }

    @Test
    public void pswdTest_0() throws Exception{
        MemberModifier.field(UserRestController.class, "syslogServiceImpl").set(userRestController,syslogServiceImpl);
        MemberModifier.field(UserRestController.class, "userService").set(userRestController,userService);//把类的属性mock引入

        Map user=new HashMap();
        user.put("user_name","ac1");
        user.put("user_no","25");
        user.put("user_password","11111");
        session.setAttribute("userInfo",user);//加入session
        String userName=String.valueOf(user.get("user_name"));

        Mockito.when(userInfo.getUserNo()).thenReturn("25");
        Mockito.when(userInfo.getPassword()).thenReturn("11111");
        MvcResult mvcResult=mockMvc.perform(MockMvcRequestBuilders.get("/user/modifyPsw")
                .param("oldPassword","11111")
                .param("newPassword","22222")
                .param("count","5")
                .param("userName","ac1")
                .param("user_name","ac1")
                .session(session) //加入session到mock中
        ).andExpect(MockMvcResultMatchers.status().isOk())
                .andDo(MockMvcResultHandlers.print())
                .andReturn();
        String content = mvcResult.getResponse().getContentAsString();
        assertNotNull(content);
    }
}

 

相关标签: Mockito