Idea+JUnit+JUnitGenerator,生成自动测试类(可测试controller)
生活随笔
收集整理的這篇文章主要介紹了
Idea+JUnit+JUnitGenerator,生成自动测试类(可测试controller)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
Idea+JUnit+JUnitGenerator,生成自動測試類(可測試controller)
1.安裝JUnitGenerator插件
打開Settings窗口搜索junit,如圖:
?
JUnitGenerator V2.0插件,可以幫助我們自動生成測試代碼。
如果搜索junit沒有JUnitGenerator V2.0時,如下圖操作(下載添加):
JUnitGenerator V2.0插件模板配置:
輸出路徑(Output Path): ${SOURCEPATH}/../../test/java/${PACKAGE}/${FILENAME}
注:../表示返回上一級目錄
JUnit3和JUnit4可以設置自己需要的模板(我JUnit3是生成Controller的模板,JUnit4是生成Service的模板)
我的JUnit3模板:
## #macro (cap $strIn)$strIn.valueOf($strIn.charAt(0)).toUpperCase()$strIn.substring(1)#end ## Iterate through the list and generate testcase for every entry. #foreach ($entry in $entryList) #set( $testClass="${entry.className}Test") ## package $entry.packageName; import org.junit.Test; import com.benxiang.main.myTest.BaseJunit; import org.springframework.http.MediaType; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;/** * ${entry.className} Tester. * * @author <Authors LyinQi> * @since <pre>$today</pre> * @version 1.0 */ public class $testClass extends BaseJunit { #foreach($method in $entry.methodList) /** * Method: $method.signature */ @Testpublic void #cap(${method.name})() throws Exception { String responseString = mockMvc.perform(get("/api/v1/") .contentType(MediaType.APPLICATION_FORM_URLENCODED) .param("os","2") .param("osv","1")).andExpect(status().isOk()) .andDo(print()) .andReturn().getResponse().getContentAsString(); System.err.println(responseString);} #end } #end我的JUnit4模板:
## #macro (cap $strIn)$strIn.valueOf($strIn.charAt(0)).toUpperCase()$strIn.substring(1)#end ## Iterate through the list and generate testcase for every entry. #foreach ($entry in $entryList) #set( $testClass="${entry.className}Test") ## package $entry.packageName; import com.benxiang.main.myTest.BaseJunit; import org.junit.Test; /** * ${entry.className} Tester. * * @author <Authors LyinQi> * @since <pre>$today</pre> * @version 1.0 */ public class $testClass extends BaseJunit { #foreach($method in $entry.methodList) /** * Method: $method.signature */ @Testpublic void #cap(${method.name})() throws Exception { //TODO: Test goes here... } #end } #end?
?
2.快捷鍵設置(我的安裝完插件快捷鍵無作用)
調用模板的方法(Alt+Insert)默認測試所有所有方法。若想要動態個性化生成,可以在所要測試的類頁面上,使用該快捷操作Ctrl + Shift + T,如下圖個性化設置:
注意:若該快捷鍵點擊無反應,需要自己設置自動創建測試類的快捷鍵:
點擊 file-> setting -> keymap 搜索:test
在該欄中修改成你想要快捷鍵,點擊OK
3.單元測試父類
package com.benxiang.main;import org.junit.Before; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner; import org.springframework.test.context.web.WebAppConfiguration; import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.setup.MockMvcBuilders; import org.springframework.web.context.WebApplicationContext;/*** 基礎測試類** @author LyinQi*/ @RunWith(SpringRunner.class) @SpringBootTest(classes = BenxiangApplication.class) @WebAppConfiguration //@Transactional //打開的話測試之后數據可自動回滾 public class BaseJunit {@AutowiredWebApplicationContext webApplicationContext;protected MockMvc mockMvc;@Beforepublic void setupMockMvc(){mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build();}@Beforepublic void initDatabase(){} }4.生成的測試類示例
Controller示例
package com.benxiang.main.controller.v1; import org.junit.Test; import com.benxiang.main.myTest.BaseJunit; import org.springframework.http.MediaType; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;/** * DealerController Tester. * * @author <Authors LyinQi> * @since <pre>12/01/2020</pre> * @version 1.0 */ public class DealerControllerTest extends BaseJunit { /** * Method: getVipUpData() */ @Testpublic void getVipUpData() throws Exception {String responseString = mockMvc.perform(get("/api/v1/dealer/getVipUpData") //請求路徑.contentType(MediaType.APPLICATION_FORM_URLENCODED) //數據的格式.param("os","2") //請求參數.param("osv","1") //請求參數).andExpect(status().isOk()) //狀態碼200.andDo(print()) //打印出請求和相應的內容.andReturn().getResponse().getContentAsString(); //將相應的數據轉換為字符串System.err.println(responseString);}/** * Method: getBuyCardData() */ @Testpublic void getBuyCardData() throws Exception {String responseString = mockMvc.perform(get("/api/v1/dealer/getBuyCardData").contentType(MediaType.APPLICATION_FORM_URLENCODED) .param("os","2") .param("osv","1").param("userId","111111")).andExpect(status().isOk()).andDo(print()) .andReturn().getResponse().getContentAsString(); System.err.println(responseString);}}Service示例
package com.benxiang.main.module.order.service.impl; import org.junit.Test; import com.benxiang.main.myTest.BaseJunit;/** * DistributorOrderServiceImpl Tester. * * @author <Authors LyinQi> * @since <pre>12/02/2020</pre> * @version 1.0 */ public class DistributorOrderServiceImplTest extends BaseJunit { /** * Method: create(OrderCreateVo orderCreateVo) */ @Testpublic void Create() throws Exception { //TODO: Test goes here... } /** * Method: paymentSuccess(Order order, String payType) */ @Testpublic void PaymentSuccess() throws Exception { //TODO: Test goes here... } }?
?
?
總結
以上是生活随笔為你收集整理的Idea+JUnit+JUnitGenerator,生成自动测试类(可测试controller)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 完美者u盘linux,完美者u盘维护系统
- 下一篇: 视频、音乐播放器大家都听说过,那么图片播