Spring 使用Junit的MockMvc 寫測試用例

聲明:本文系轉載

1、spring配置文件

該聲明爲bean的類就聲明,測試之前項目要能運行,所以spring的配置文件問題就不多說了,下面的數據庫配置和測試類中負責回滾的TransactionalConfigration註解有關,所以貼出來。

[html] view plain copy
  1. <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">  
  2.     <property name="dataSource" ref="mysqlDataSource" />  
  3. </bean>  

2、pom.xml需要使用的包:

[javascript] view plain copy
  1. <!-- TEST -->  
  2. <dependency>  
  3.     <groupId>junit</groupId>  
  4.     <artifactId>junit</artifactId>  
  5.     <version>4.10</version>  
  6.     <scope>test</scope>  
  7.     <exclusions>  
  8.         <exclusion>  
  9.             <artifactId>hamcrest-core</artifactId>  
  10.             <groupId>org.hamcrest</groupId>  
  11.         </exclusion>  
  12.     </exclusions>  
  13. </dependency>  
  14.   
  15. <dependency>  
  16.     <groupId>org.springframework</groupId>  
  17.     <artifactId>spring-test</artifactId>  
  18.     <version>3.2.8.RELEASE</version>  
  19.     <scope>test</scope>  
  20. </dependency>  
  21.   
  22. <dependency>  
  23.     <groupId>org.hamcrest</groupId>  
  24.     <artifactId>hamcrest-all</artifactId>  
  25.     <version>1.3</version>  
  26.     <scope>test</scope>  
  27. </dependency>  
  28. <dependency>  
  29.     <groupId>org.mockito</groupId>  
  30.     <artifactId>mockito-core</artifactId>  
  31.     <version>1.9.5</version>  
  32.     <scope>test</scope>  
  33.     <exclusions>  
  34.         <exclusion>  
  35.             <artifactId>hamcrest-core</artifactId>  
  36.             <groupId>org.hamcrest</groupId>  
  37.         </exclusion>  
  38.     </exclusions>  
  39. </dependency>  
  40.   
  41. <dependency>  
  42.     <groupId>com.jayway.jsonpath</groupId>  
  43.     <artifactId>json-path</artifactId>  
  44.     <version>0.8.1</version>  
  45.     <scope>test</scope>  
  46. </dependency>  

3、測試類

最好在源碼平行目錄下新建一個測試用的文件夾以及包等

[java] view plain copy
  1. @RunWith(SpringJUnit4ClassRunner.class)  
  2.   
  3. @TransactionConfiguration(transactionManager = "transactionManager", defaultRollback = true)//保證每次測試類執行完後數據庫進行回滾,防止測試時產生髒數據  
  4. @Transactional  
  5.   
  6. @WebAppConfiguration(value = "mydemo/src/main/webapp")  
  7. @ContextConfiguration(locations={"file:src/main/webapp/WEB-INF/spring-config.xml"})//如果spring的配置文件放在WEB-INF目錄下,需要使用這種方法  
  8. @ActiveProfiles("dev")  
  9. public class demoTest {  
  10.   
  11.     @Autowired  
  12.     WebApplicationContext wac;  
  13.   
  14.     private MockMvc mockMvc;  
  15.   
  16.     @Before  
  17.     public void setUp() {  
  18.         mockMvc = MockMvcBuilders.webAppContextSetup(wac).build();  
  19.     }  
  20.   
  21.   
  22. @Test  
  23.     public void getDemoidTest1() throws Exception {  
  24.         mockMvc.perform(MockMvcRequestBuilders.get("/config/getDemo?demoid=1005"))  
  25.                 .andDo(MockMvcResultHandlers.print())  
  26.                 .andExpect(MockMvcResultMatchers.status().isOk())  
  27.                 .andExpect(MockMvcResultMatchers.content().contentType("application/json;charset=UTF-8"))  
  28.                 .andExpect(jsonPath("$.code").value(20000))  
  29.                 .andExpect(jsonPath("$.demoList").exists())  
  30.                 .andDo(MockMvcResultHandlers.print())  
  31.                 .andReturn();  
  32.     }  
  33. }  

1、mockMvc.perform執行一個請求;

2、MockMvcRequestBuilders.get("/user/1")構造一個請求

3、ResultActions.andExpect添加執行完成後的斷言

4、ResultActions.andDo添加一個結果處理器,表示要對結果做點什麼事情,比如此處使用MockMvcResultHandlers.print()輸出整個響應結果信息。

5、ResultActions.andReturn表示執行完成後返回相應的結果。


4、其中可能遇到的坑:


1)、如果spring的配置文件在默認的目錄下,使用@ContextConfiguration(locations = "classpath:spring-config.xml")即可


但是!如果spring的配置文件在WEB-INF目錄下,請使用

@ContextConfiguration(locations={"file:src/main/webapp/WEB-INF/spring-config.xml"})

否則會找不到配置文件


說到這裏可以提一下爲什麼需要加載配置文件,使用mockMvc,會創建整套完整流程,模擬從前端發出的請求,個人理解爲使用這種方法調用controller和前端、postman等調用controller沒什麼區別,這樣可以測試完整的Spring MVC流程,即從URL請求到控制器處理,再到視圖渲染都可以測試。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章