springboot Junit 執(zhí)行順序詳解
springboot Junit 執(zhí)行順序
我們在寫JUnit測試用例時,有時候需要按照定義順序執(zhí)行我們的單元測試方法,比如如在測試數(shù)據(jù)庫相關(guān)的用例時候要按照測試插入、查詢、刪除的順序測試。
如果不按照這個順序測試可能會出現(xiàn)問題,比如刪除方法在前面執(zhí)行,后面的方法就都不能通過測試,因為數(shù)據(jù)已經(jīng)被清空了。而JUnit測試時默認的順序是隨機的。
所以這時就需要有辦法要求JUnit在執(zhí)行測試方法時按照我們指定的順序來執(zhí)行。
JUnit是通過@FixMethodOrder注解(annotation)來控制測試方法的執(zhí)行順序的。
@FixMethodOrder注解的參數(shù)是org.junit.runners.MethodSorters對象,在枚舉類org.junit.runners.MethodSorters中定義了如下三種順序類型:
- MethodSorters.JVM
Leaves the test methods in the order returned by the JVM. Note that the order from the JVM may vary from run to run (按照JVM得到的方法順序,也就是代碼中定義的方法順序)
- MethodSorters.DEFAULT(默認的順序)
Sorts the test methods in a deterministic, but not predictable, order() (以確定但不可預(yù)期的順序執(zhí)行)
- MethodSorters.NAME_ASCENDING
Sorts the test methods by the method name, in lexicographic order, with Method.toString() used as a tiebreaker (按方法名字母順序執(zhí)行)
舉例說明
以下的代碼,定義了三個方法testAddAndGet,testSearch,testRemove,我設(shè)計的時候,是希望三個方法按定義的順序來執(zhí)行。
package test; import org.junit.Assert; import org.junit.FixMethodOrder; import org.junit.runners.MethodSorters; import org.junit.Test; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @FixMethodOrder(MethodSorters.JVM)//指定測試方法按定義的順序執(zhí)行 public class TestJNI { private static final Logger logger = LoggerFactory.getLogger(TestJNI.class); @Test public void testAddAndGet(){ logger.info("test 'addBean' and 'getBean' "); } @Test public final void testSearch() { logger.info("test search CODE from JNI memory..."); } @Test public final void testRemove() { logger.info("test remove CODE from JNI memory..."); } }
如果@FixMethodOrder定義為MethodSorters.DEFAULT或去掉代碼中的@FixMethodOrder注解,那么測試用便執(zhí)行的順序是
這并不是我要的結(jié)果,testRemove如果先執(zhí)行了,testSearch肯定什么也找不到。
如果改成@FixMethodOrder(MethodSorters.JVM),則這個執(zhí)行順序才是我想要的順序。
SpringBoot JUnit 測試 Controller
Controller層代碼如下:
@RestController public class HelloController { Logger logger = LoggerFactory.getLogger(this.getClass()); @Autowired private UserService userService; @RequestMapping("/hello") public String index() { logger.info("{}",userService == null); logger.info("{}",userService.getCount()); return "Hello World"; } }
JUnit 測試HelloController代碼如下:
@RunWith(SpringRunner.class) @SpringBootTest public class HelloControllerTest { private MockMvc mvc; @Before public void setUp() throws Exception { mvc = MockMvcBuilders.standaloneSetup(new HelloController()).build(); } @Test public void getHello() throws Exception { mvc.perform(MockMvcRequestBuilders.get("/hello").accept(MediaType.APPLICATION_JSON)) .andExpect(MockMvcResultMatchers.status().isOk()) .andDo(MockMvcResultHandlers.print()) .andReturn(); } }
但是這種方法在運行過程中,Controller 里面Autowired 的bean 無法注入,報空指針,因為這種方法沒有給通過Spring加載上下文實現(xiàn)注入參考這里的解決方法
采取下面這種測試寫法
@RunWith(SpringRunner.class) @SpringBootTest @AutoConfigureMockMvc public class HelloControllerTest { @Autowired private MockMvc mockMvc; @Test public void getHello() throws Exception { mockMvc.perform(MockMvcRequestBuilders.get("/hello").accept(MediaType.APPLICATION_JSON)) .andExpect(MockMvcResultMatchers.status().isOk()) .andDo(MockMvcResultHandlers.print()) .andReturn(); } }
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Spring MVC的優(yōu)點與核心接口_動力節(jié)點Java學(xué)院整理
這篇文章主要介紹了Spring MVC的優(yōu)點與核心接口,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-08-08idea與eclipse項目相互導(dǎo)入的過程(圖文教程)
這篇文章主要介紹了idea與eclipse項目相互導(dǎo)入的過程,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-03-03Spring之兩種任務(wù)調(diào)度Scheduled和Async詳解
這篇文章主要介紹了Spring之兩種任務(wù)調(diào)度Scheduled和Async,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-10-10WebDriver中實現(xiàn)對特定的Web區(qū)域截圖方法
這篇文章主要介紹了WebDriver中實現(xiàn)對特定的Web區(qū)域截圖方法,本文直接給出實現(xiàn)代碼,需要的朋友可以參考下2015-06-06