Spring單元測(cè)試類ApplicationTests錯(cuò)誤的解決
Spring單元測(cè)試類ApplicationTests錯(cuò)誤
1)正確寫(xiě)法
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個(gè)用戶
userSerivce.create("a", 1);
userSerivce.create("b", 2);
userSerivce.create("c", 3);
userSerivce.create("d", 4);
userSerivce.create("e", 5);
// 查數(shù)據(jù)庫(kù),應(yīng)該有5個(gè)用戶
Assert.assertEquals(5, userSerivce.getAllUsers().intValue());
// 刪除兩個(gè)用戶
userSerivce.deleteByName("a");
userSerivce.deleteByName("e");
// 查數(shù)據(jù)庫(kù),應(yīng)該有5個(gè)用戶
Assert.assertEquals(3, userSerivce.getAllUsers().intValue());
}
}2)異常寫(xiě)法
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個(gè)用戶
userSerivce.create("a", 1);
userSerivce.create("b", 2);
userSerivce.create("c", 3);
userSerivce.create("d", 4);
userSerivce.create("e", 5);
// 查數(shù)據(jù)庫(kù),應(yīng)該有5個(gè)用戶
Assert.assertEquals(5, userSerivce.getAllUsers().intValue());
// 刪除兩個(gè)用戶
userSerivce.deleteByName("a");
userSerivce.deleteByName("e");
// 查數(shù)據(jù)庫(kù),應(yīng)該有5個(gè)用戶
Assert.assertEquals(3, userSerivce.getAllUsers().intValue());
}
}
SpringTest單元測(cè)試錯(cuò)誤
java.lang.Exception: No runnable methods
at org.junit.runners.BlockJUnit4ClassRunner.validateInstanceMethods(BlockJUnit4ClassRunner.java:191)
進(jìn)行SpringTest單元測(cè)試時(shí)遇到的錯(cuò)誤
經(jīng)過(guò)查詢資料總結(jié)出現(xiàn)此錯(cuò)誤的原因可能有兩種
1、沒(méi)有在測(cè)試方法上寫(xiě)@Test
2、@Test包導(dǎo)入出錯(cuò),很有可能導(dǎo)入的是org.junit.jupiter.api.Test包,而使用Spring單元測(cè)試需要的包是org.junit.Test
可能由以上兩種可能導(dǎo)致出錯(cuò)
要這樣

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

