springboot Junit 執(zhí)行順序詳解
springboot Junit 執(zhí)行順序
我們?cè)趯慗Unit測(cè)試用例時(shí),有時(shí)候需要按照定義順序執(zhí)行我們的單元測(cè)試方法,比如如在測(cè)試數(shù)據(jù)庫相關(guān)的用例時(shí)候要按照測(cè)試插入、查詢、刪除的順序測(cè)試。
如果不按照這個(gè)順序測(cè)試可能會(huì)出現(xiàn)問題,比如刪除方法在前面執(zhí)行,后面的方法就都不能通過測(cè)試,因?yàn)閿?shù)據(jù)已經(jīng)被清空了。而JUnit測(cè)試時(shí)默認(rèn)的順序是隨機(jī)的。
所以這時(shí)就需要有辦法要求JUnit在執(zhí)行測(cè)試方法時(shí)按照我們指定的順序來執(zhí)行。
JUnit是通過@FixMethodOrder注解(annotation)來控制測(cè)試方法的執(zhí)行順序的。
@FixMethodOrder注解的參數(shù)是org.junit.runners.MethodSorters對(duì)象,在枚舉類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 (按照J(rèn)VM得到的方法順序,也就是代碼中定義的方法順序)
- MethodSorters.DEFAULT(默認(rèn)的順序)
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í)行)
舉例說明
以下的代碼,定義了三個(gè)方法testAddAndGet,testSearch,testRemove,我設(shè)計(jì)的時(shí)候,是希望三個(gè)方法按定義的順序來執(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)//指定測(cè)試方法按定義的順序執(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注解,那么測(cè)試用便執(zhí)行的順序是
這并不是我要的結(jié)果,testRemove如果先執(zhí)行了,testSearch肯定什么也找不到。
如果改成@FixMethodOrder(MethodSorters.JVM),則這個(gè)執(zhí)行順序才是我想要的順序。
SpringBoot JUnit 測(cè)試 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 測(cè)試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(); } }
但是這種方法在運(yùn)行過程中,Controller 里面Autowired 的bean 無法注入,報(bào)空指針,因?yàn)檫@種方法沒有給通過Spring加載上下文實(shí)現(xiàn)注入參考這里的解決方法
采取下面這種測(cè)試寫法
@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(); } }
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Mybatis實(shí)現(xiàn)一對(duì)多映射處理
MyBatis是一種流行的Java持久化框架,這篇文章主要為大家介紹了Mybatis如何實(shí)現(xiàn)一對(duì)多映射處理,文中的示例代碼講解詳細(xì),需要的可以參考下2023-08-08Spring MVC的優(yōu)點(diǎn)與核心接口_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
這篇文章主要介紹了Spring MVC的優(yōu)點(diǎn)與核心接口,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-08-08idea與eclipse項(xiàng)目相互導(dǎo)入的過程(圖文教程)
這篇文章主要介紹了idea與eclipse項(xiàng)目相互導(dǎo)入的過程,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-03-03Spring之兩種任務(wù)調(diào)度Scheduled和Async詳解
這篇文章主要介紹了Spring之兩種任務(wù)調(diào)度Scheduled和Async,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-10-10WebDriver中實(shí)現(xiàn)對(duì)特定的Web區(qū)域截圖方法
這篇文章主要介紹了WebDriver中實(shí)現(xiàn)對(duì)特定的Web區(qū)域截圖方法,本文直接給出實(shí)現(xiàn)代碼,需要的朋友可以參考下2015-06-06