Spring單元測(cè)試類(lèi)ApplicationTests錯(cuò)誤的解決
Spring單元測(cè)試類(lèi)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 AOP如何自定義注解實(shí)現(xiàn)審計(jì)或日志記錄(完整代碼)
這篇文章主要介紹了Spring AOP如何自定義注解實(shí)現(xiàn)審計(jì)或日志記錄(完整代碼),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-12-12
springboot如何重定向攜帶數(shù)據(jù) RedirectAttributes
這篇文章主要介紹了springboot如何重定向攜帶數(shù)據(jù) RedirectAttributes,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-09-09
jboss( WildFly)上運(yùn)行 springboot程序的步驟詳解
這篇文章主要介紹了jboss( WildFly)上運(yùn)行 springboot程序的步驟詳解,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-02-02
Mybatis游標(biāo)查詢大量數(shù)據(jù)方式
這篇文章主要介紹了Mybatis游標(biāo)查詢大量數(shù)據(jù)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-02-02
SpringBoot中使用configtree讀取樹(shù)形文件目錄中的配置詳解
這篇文章主要介紹了SpringBoot中使用configtree讀取樹(shù)形文件目錄中的配置詳解,configtree通過(guò)spring.config.import?+?configtree:前綴的方式,加載以文件名為key、文件內(nèi)容為value的配置屬性,需要的朋友可以參考下2023-12-12

