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
*/
@Test
public 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
*/
@Test
public 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 {
@Autowired
WebApplicationContext webApplicationContext;
protected MockMvc mockMvc;
@Before
public void setupMockMvc(){
mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build();
}
@Before
public 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()
*/
@Test
public 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()
*/
@Test
public 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)
*/
@Test
public void Create() throws Exception {
//TODO: Test goes here...
}
/**
* Method: paymentSuccess(Order order, String payType)
*/
@Test
public void PaymentSuccess() throws Exception {
//TODO: Test goes here...
}
}