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)注入?yú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è)參考,也希望大家多多支持服務(wù)器之家。
原文鏈接:https://blog.csdn.net/qq_27173485/article/details/53996936