spring boot 基于Shiro单元测试

背景

在实际的工程做,在很多场景都需要获取当前登录的用户状态进行对应的逻辑校验、默认值配置等操作,估在做单元测试时,如果没有初始化安全上下文,那么在执行场景的时候会报取不到上下文的空指针错误,为提高开发效率,单元测试还是有必要的。

以下为实际案例:


import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.session.mgt.eis.SessionDAO;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.util.ThreadContext;
import org.apache.shiro.web.subject.WebSubject;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockHttpServletResponse;
import org.springframework.mock.web.MockHttpSession;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;

import javax.annotation.Resource;

/**
 * Created by wuqh on 2019/9/15.
 */
@RunWith(SpringRunner.class)
@SpringBootTest(classes={Application.class}, webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)// 指定启动类
public class RecordingVoucherGroupServiceTest {

    @Autowired
    IRecordingVoucherGroupService recordingVoucherGroupService;

    @Resource
    private org.apache.shiro.mgt.SecurityManager securityManager;
    @Resource
    private WebApplicationContext webApplicationContext;
    @Resource
    private SessionDAO sessionDAO;
    @Autowired
    private RedisUtil redisUtil;
    private Subject subject;
    private MockMvc mockMvc;
    private MockHttpServletRequest mockHttpServletRequest;
    private MockHttpServletResponse mockHttpServletResponse;
	
	//用户登录
    private void login(String username, String password) {
        subject = new WebSubject.Builder(mockHttpServletRequest, mockHttpServletResponse)
                .buildWebSubject();
          // 用户密码   
//        UsernamePasswordToken token = new UsernamePasswordToken(username, password, true);
        //基于JWT令牌验证
        String token = JwtUtil.sign(username, password);
        redisUtil.set(CommonConstant.PREFIX_USER_TOKEN + token, token);
        // 设置超时时间
        JwtToken jwtToken=new JwtToken(token);
        subject.login(jwtToken);
        ThreadContext.bind(subject);
    }

	//初始化上下文
    @Before
    public void before() {
        mockHttpServletRequest = new MockHttpServletRequest(webApplicationContext.getServletContext());
        mockHttpServletResponse = new MockHttpServletResponse();
        MockHttpSession mockHttpSession = new MockHttpSession(webApplicationContext.getServletContext());
        mockHttpServletRequest.setSession(mockHttpSession);
        SecurityUtils.setSecurityManager( securityManager);
        mockMvc = MockMvcBuilders
                .webAppContextSetup(webApplicationContext)
                .build();
        login("admin", "123456");
    }

	//业务方法
    @Test
    public void addRecordingVoucherGroupBySendRecordTest(){
        SendRecordVO sendRecordVO=new SendRecordVO();
        sendRecordVO.setSendRecordId("xxxxx");
        //这个方法里有获取上下文的配置
        recordingVoucherGroupService.addRecordingVoucherGroupBySendRecord(sendRecordVO);
    }
}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值