栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

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

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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;


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

}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/988131.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号