Spring單元測試類ApplicationTests錯誤的解決
Spring單元測試類ApplicationTests錯誤
1)正確寫法
package com.boot.demo02restful;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import com.boot.restful.Application;
import com.boot.restful.service.UserService;
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes=Application.class)
public class ApplicationTests {
@Autowired
@Qualifier(value="myUserService")
private UserService userSerivce;
@Before
public void setUp() {
// 準(zhǔn)備,清空user表
userSerivce.deleteAllUsers();
}
@Test
public void test() throws Exception {
// 插入5個用戶
userSerivce.create("a", 1);
userSerivce.create("b", 2);
userSerivce.create("c", 3);
userSerivce.create("d", 4);
userSerivce.create("e", 5);
// 查數(shù)據(jù)庫,應(yīng)該有5個用戶
Assert.assertEquals(5, userSerivce.getAllUsers().intValue());
// 刪除兩個用戶
userSerivce.deleteByName("a");
userSerivce.deleteByName("e");
// 查數(shù)據(jù)庫,應(yīng)該有5個用戶
Assert.assertEquals(3, userSerivce.getAllUsers().intValue());
}
}2)異常寫法
package com.boot.demo02restful;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import com.boot.restful.Application;
import com.boot.restful.service.UserService;
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest
public class ApplicationTests {
@Autowired
@Qualifier(value="myUserService")
private UserService userSerivce;
@Before
public void setUp() {
// 準(zhǔn)備,清空user表
userSerivce.deleteAllUsers();
}
@Test
public void test() throws Exception {
// 插入5個用戶
userSerivce.create("a", 1);
userSerivce.create("b", 2);
userSerivce.create("c", 3);
userSerivce.create("d", 4);
userSerivce.create("e", 5);
// 查數(shù)據(jù)庫,應(yīng)該有5個用戶
Assert.assertEquals(5, userSerivce.getAllUsers().intValue());
// 刪除兩個用戶
userSerivce.deleteByName("a");
userSerivce.deleteByName("e");
// 查數(shù)據(jù)庫,應(yīng)該有5個用戶
Assert.assertEquals(3, userSerivce.getAllUsers().intValue());
}
}
SpringTest單元測試錯誤
java.lang.Exception: No runnable methods
at org.junit.runners.BlockJUnit4ClassRunner.validateInstanceMethods(BlockJUnit4ClassRunner.java:191)
進行SpringTest單元測試時遇到的錯誤
經(jīng)過查詢資料總結(jié)出現(xiàn)此錯誤的原因可能有兩種
1、沒有在測試方法上寫@Test
2、@Test包導(dǎo)入出錯,很有可能導(dǎo)入的是org.junit.jupiter.api.Test包,而使用Spring單元測試需要的包是org.junit.Test
可能由以上兩種可能導(dǎo)致出錯
要這樣

以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
使用Spring注解@EventListener實現(xiàn)監(jiān)聽原理
這篇文章主要介紹了使用Spring注解@EventListener實現(xiàn)監(jiān)聽原理,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-08-08
Mybatisplus自動填充實現(xiàn)方式及代碼示例
這篇文章主要介紹了Mybatisplus自動填充實現(xiàn)方式及代碼示例,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-11-11
Java實現(xiàn)單鏈表反轉(zhuǎn)的多種方法總結(jié)
這篇文章主要給大家介紹了關(guān)于Java實現(xiàn)單鏈表反轉(zhuǎn)的多種方法,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-04-04
springboot 運行 jar 包讀取外部配置文件的問題
這篇文章主要介紹了springboot 運行 jar 包讀取外部配置文件,本文主要描述linux系統(tǒng)執(zhí)行jar包讀取jar包同級目錄的外部配置文件,主要分為兩種方法,每種方法通過實例代碼介紹的非常詳細,需要的朋友可以參考下2021-07-07
IntelliJ?IDEA軟件內(nèi)如何實現(xiàn)更新到最新版本
文章介紹了如何在IntelliJIDEA中更新到最新版本以及如何回到之前忽略的版本,解決辦法是通過選擇"IgnoreThisUpdate"來跳過舊版本,重復(fù)操作即可更新到最新版本2024-12-12
Java的Hibernate框架中Criteria查詢使用的實例講解
這篇文章主要介紹了Java的Hibernate框架中Criteria查詢使用的實例講解,Hibernate是Java的SSH三大web開發(fā)框架之一,需要的朋友可以參考下2016-01-01
SpringBoot返回結(jié)果統(tǒng)一處理實例詳解
這篇文章主要為大家介紹了SpringBoot返回結(jié)果統(tǒng)一處理實例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-12-12

