junit 5 测试 获取service层shiro登录用户信息

项目:前后离分离 spring boot  jwt shiro

junit 5版本  不能用junit4 

使用org.junit.jupiter.api

在before前登录用户 再跑测试用户

    @BeforeEach
    void setUp() throws UnsupportedEncodingException {
        SecurityUtils.setSecurityManager(webApplicationContext.getBean(org.apache.shiro.mgt.SecurityManager.class));
        String userName = "admin";
        String password = "admin";
        JWTToken token = new JWTToken(userName, password.toCharArray(), "10.1.11.1");
        SecurityUtils.getSubject().login(token);
        System.out.println("before");
    }

类上的注解除了 springtest原有的 加上

@TestInstance(TestInstance.Lifecycle.PER_METHOD)


import org.apache.shiro.SecurityUtils;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInstance;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.context.WebApplicationContext;

import java.io.UnsupportedEncodingException;
import java.util.List;

/**
 * @ClassName RuleCommandsServiceTest
 */
@TestInstance(TestInstance.Lifecycle.PER_METHOD)
public class RuleCommandsServiceTest extends HdlhApplicationTests {

    @Autowired
    private RuleCommandsService ruleCommandsService;

    @Autowired
    private WebApplicationContext webApplicationContext;


    @BeforeEach
    void setUp() throws UnsupportedEncodingException {
        SecurityUtils.setSecurityManager(webApplicationContext.getBean(org.apache.shiro.mgt.SecurityManager.class));
        String userName = "admin";
        String password = "admin";
        JWTToken token = new JWTToken(userName, password.toCharArray(), "10.1.11.1");
        SecurityUtils.getSubject().login(token);
        System.out.println("before");
    }

    

    /**
     * 测试添加脚本模拟开通任务单
     */
    @Test
    public void addRuleCommands(){
        NetworkCommandsSimulateGenerateEntity ruleCommands = new NetworkCommandsSimulateGenerateEntity();
        ruleCommands.setBusinessSystem("CompanyNetwork");
        ruleCommands.setTaskNumber("PTA_20220623152700033_553");
        ruleCommands.setOrderNumber("20220623152700033_553_self");
        ruleCommands.setProtocolDestPort("any");
        ruleCommands.setCreateUser("huangbin");
        ruleCommands.setSrcIp("any");
        ruleCommands.setDestIp("192.168.10.2");
        ruleCommands.setAction(0);
        ruleCommands.setIsForever(1);
        ruleCommandsService.createRuleCommands(ruleCommands);
    }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值