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);
    }
}

좋은 웹페이지 즐겨찾기