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

Spring單元測試類ApplicationTests錯誤的解決

 更新時間:2022年01月18日 09:54:59   作者:上官天夜  
這篇文章主要介紹了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() {
		// 準備,清空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ù)庫,應該有5個用戶
		Assert.assertEquals(5, userSerivce.getAllUsers().intValue());
		// 刪除兩個用戶
		userSerivce.deleteByName("a");
		userSerivce.deleteByName("e");
		// 查數(shù)據(jù)庫,應該有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() {
		// 準備,清空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ù)庫,應該有5個用戶
		Assert.assertEquals(5, userSerivce.getAllUsers().intValue());
		// 刪除兩個用戶
		userSerivce.deleteByName("a");
		userSerivce.deleteByName("e");
		// 查數(shù)據(jù)庫,應該有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)過查詢資料總結出現(xiàn)此錯誤的原因可能有兩種

1、沒有在測試方法上寫@Test

2、@Test包導入出錯,很有可能導入的是org.junit.jupiter.api.Test包,而使用Spring單元測試需要的包是org.junit.Test

可能由以上兩種可能導致出錯

要這樣

以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關文章

  • Spring AOP如何自定義注解實現(xiàn)審計或日志記錄(完整代碼)

    Spring AOP如何自定義注解實現(xiàn)審計或日志記錄(完整代碼)

    這篇文章主要介紹了Spring AOP如何自定義注解實現(xiàn)審計或日志記錄(完整代碼),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-12-12
  • springboot如何重定向攜帶數(shù)據(jù) RedirectAttributes

    springboot如何重定向攜帶數(shù)據(jù) RedirectAttributes

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

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

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

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

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

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

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

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

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

    Spring注解之@PropertySource詳解

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

    Java分布式服務框架Dubbo介紹

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

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

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

    Java 8 引入lambda表達式的原因解析

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

最新評論