springboot 新建測試類(junit),報錯:org.apache.shiro.UnavailableSecurityManagerException: No SecurityManager

新建測試類,因爲方法內容涉及到shiro和數據庫操作,被卡了數小時,這裏記錄一下成功方案
(因爲使用的是bootdo框架,框架已添加了所需依賴,未添加依賴的記得添加到pom文件)
在需要新建測試類的類中右鍵
在這裏插入圖片描述
出現下圖,點擊即可
在這裏插入圖片描述
點擊ok
在這裏插入圖片描述
(紅框處選中則生成的測試類中有該方法)

測試類代碼:

package XXX;

import org.apache.shiro.util.ThreadContext;
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.test.context.junit4.SpringRunner;

import javax.annotation.Resource;

import static org.junit.Assert.*;

@RunWith(SpringRunner.class)
@SpringBootTest
public class ProjectFontControllerTest {
    @Resource
    org.apache.shiro.mgt.SecurityManager securityManager;
    @Before
    public void setUp() throws Exception {
        ThreadContext.bind(securityManager);
    }
    @Autowired
    ProjectService projectService;
    @Test
    public void addProject() {  //自動生成的方法名,也可自定義
       //代碼,非自動生成
    }
}

點擊函數方法左側的運行按鈕即可

運行完成後點擊查看運行結果
在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章