欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Spring單元測(cè)試類ApplicationTests錯(cuò)誤的解決

 更新時(shí)間:2022年01月18日 09:54:59   作者:上官天夜  
這篇文章主要介紹了Spring單元測(cè)試類ApplicationTests錯(cuò)誤的解決,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(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 AOP如何自定義注解實(shí)現(xiàn)審計(jì)或日志記錄(完整代碼)

    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

    這篇文章主要介紹了springboot如何重定向攜帶數(shù)據(jù) RedirectAttributes,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-09-09
  • java 根據(jù)漢字生成拼音全拼或拼音首字母的示例

    java 根據(jù)漢字生成拼音全拼或拼音首字母的示例

    這篇文章主要介紹了java 根據(jù)漢字生成拼音全拼或拼音首字母的示例,幫助大家更好的利用Java處理數(shù)據(jù),感興趣的朋友可以了解下
    2020-11-11
  • jboss( WildFly)上運(yùn)行 springboot程序的步驟詳解

    jboss( WildFly)上運(yùn)行 springboot程序的步驟詳解

    這篇文章主要介紹了jboss( WildFly)上運(yùn)行 springboot程序的步驟詳解,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-02-02
  • Java 替換word文檔文字并指定位置插入圖片

    Java 替換word文檔文字并指定位置插入圖片

    這篇文章主要介紹了Java 替換word文檔文字,指定位置插入圖片功能,本文通過(guò)實(shí)例代碼給大家講解,需要的朋友可以參考下
    2018-02-02
  • Mybatis游標(biāo)查詢大量數(shù)據(jù)方式

    Mybatis游標(biāo)查詢大量數(shù)據(jù)方式

    這篇文章主要介紹了Mybatis游標(biāo)查詢大量數(shù)據(jù)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-02-02
  • Spring注解之@PropertySource詳解

    Spring注解之@PropertySource詳解

    這篇文章主要介紹了Spring注解之@PropertySource詳解,@PropertySource注解用于指定資源文件讀取的位置,它不僅能讀取properties文件,也能讀取xml文件,并且通過(guò)YAML解析器,配合自定義PropertySourceFactory實(shí)現(xiàn)解析YAML文件,需要的朋友可以參考下
    2023-11-11
  • Java分布式服務(wù)框架Dubbo介紹

    Java分布式服務(wù)框架Dubbo介紹

    這篇文章介紹了Java分布式服務(wù)框架Dubbo,對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2022-06-06
  • SpringBoot中使用configtree讀取樹(shù)形文件目錄中的配置詳解

    SpringBoot中使用configtree讀取樹(shù)形文件目錄中的配置詳解

    這篇文章主要介紹了SpringBoot中使用configtree讀取樹(shù)形文件目錄中的配置詳解,configtree通過(guò)spring.config.import?+?configtree:前綴的方式,加載以文件名為key、文件內(nèi)容為value的配置屬性,需要的朋友可以參考下
    2023-12-12
  • Java 8 引入lambda表達(dá)式的原因解析

    Java 8 引入lambda表達(dá)式的原因解析

    這篇文章主要介紹了Java 8 引入lambda表達(dá)式的原因解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-08-08

最新評(píng)論