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)審計或日志記錄(完整代碼),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-12-12springboot如何重定向攜帶數(shù)據(jù) RedirectAttributes
這篇文章主要介紹了springboot如何重定向攜帶數(shù)據(jù) RedirectAttributes,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-09-09jboss( WildFly)上運行 springboot程序的步驟詳解
這篇文章主要介紹了jboss( WildFly)上運行 springboot程序的步驟詳解,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-02-02SpringBoot中使用configtree讀取樹形文件目錄中的配置詳解
這篇文章主要介紹了SpringBoot中使用configtree讀取樹形文件目錄中的配置詳解,configtree通過spring.config.import?+?configtree:前綴的方式,加載以文件名為key、文件內容為value的配置屬性,需要的朋友可以參考下2023-12-12