springboot Junit 執(zhí)行順序
我們在寫JUnit測試用例時,有時候需要按照定義順序執(zhí)行我們的單元測試方法,比如如在測試數(shù)據(jù)庫相關(guān)的用例時候要按照測試插入、查詢、刪除的順序測試。
如果不按照這個順序測試可能會出現(xiàn)問題,比如刪除方法在前面執(zhí)行,后面的方法就都不能通過測試,因?yàn)閿?shù)據(jù)已經(jīng)被清空了。而JUnit測試時默認(rèn)的順序是隨機(jī)的。
所以這時就需要有辦法要求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 (按照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í)行)
舉例說明
以下的代碼,定義了三個方法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(); } }
但是這種方法在運(yùn)行過程中,Controller 里面Autowired 的bean 無法注入,報空指針,因?yàn)檫@種方法沒有給通過Spring加載上下文實(shí)現(xiàn)注入?yú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)驗(yàn),希望能給大家一個參考,也希望大家多多支持服務(wù)器之家。
原文鏈接:https://blog.csdn.net/qq_27173485/article/details/53996936