spring boot Shiro 단위 테스트 기반
15325 단어 유닛 테스트
실제 프로젝트 를 할 때 많은 장면 에서 현재 로그 인 한 사용자 상 태 를 가 져 와 대응 하 는 논리 적 검증, 기본 값 설정 등 작업 을 해 야 합 니 다. 유닛 테스트 를 할 때 안전 문맥 을 초기 화하 지 않 으 면 장면 을 수행 할 때 상하 문의 빈 지침 오 류 를 보고 하지 못 하고 개발 효율 을 높이 기 위해 유닛 테스트 가 필요 합 니 다.
다음은 실제 사례 이다.
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);
}
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
자바 에서 sax 분석 xmlSax 해석 은 xml 파일 의 순서에 따라 해석 되 기 때문에 을 읽 을 때 startDocument () 방법 을 호출 합 니 다. 주의: 환경 이 다 르 기 때문에 두 번 째 매개 변 수 는 비어 있 을 수도 ...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.