项目:前后离分离 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);
}
}