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