懒人必备:一款自动生成单元测试的 IDEA 插件
阅读本文大概需要 7 分钟。







public class CrawlerScreenShotServiceImplTest {private CrawerScreenShotTaskMapper mockCrawerScreenShotTaskMapper;private CrawerScreenShotTaskLogMapper mockCrawerScreenShotTaskLogMapper;private CrawlerScreenShotServiceImpl crawlerScreenShotServiceImplUnderTest;public void setUp() {initMocks(this);}public void testReceiveData() {// Setupfinal CrawlerScreenShotVO vo = new CrawlerScreenShotVO();vo.setUrl("url");vo.setPcFlag(false);vo.setMembergroup("membergroup");vo.setTaskType(0);vo.setUrlType(0);when(mockCrawerScreenShotTaskLogMapper.saveSelective(any(CrawerScreenShotTaskLog.class))).thenReturn(0);when(mockCrawerScreenShotTaskMapper.saveBatch(Arrays.asList(new CrawlerScreenShotTask(0L, "url", "imageOssUrl", false, false, "memberGroup", 0, 0, "fileName", new GregorianCalendar(2019, Calendar.JANUARY, 1).getTime(), new GregorianCalendar(2019, Calendar.JANUARY, 1).getTime(), false, "skuCode", "state", "operater")))).thenReturn(0);// Run the testfinal Resultresult = crawlerScreenShotServiceImplUnderTest.receiveData(vo); // Verify the results}public void testListJobScreenShotTask() {// Setup// Configure CrawerScreenShotTaskMapper.listJobScreenShotTask(...).final CrawlerScreenShotTaskDto crawlerScreenShotTaskDto = new CrawlerScreenShotTaskDto();crawlerScreenShotTaskDto.setId(0L);crawlerScreenShotTaskDto.setUrl("url");crawlerScreenShotTaskDto.setSkuCode("skuCode");crawlerScreenShotTaskDto.setPcFlag(false);crawlerScreenShotTaskDto.setMemberGroup("memberGroup");crawlerScreenShotTaskDto.setUrlType(0);crawlerScreenShotTaskDto.setFileName("fileName");crawlerScreenShotTaskDto.setTaskType(0);crawlerScreenShotTaskDto.setState("state");final ListcrawlerScreenShotTaskDtos = Arrays.asList(crawlerScreenShotTaskDto); when(mockCrawerScreenShotTaskMapper.listJobScreenShotTask(new GregorianCalendar(2019, Calendar.JANUARY, 1).getTime())).thenReturn(crawlerScreenShotTaskDtos);// Run the testfinal Listresult = crawlerScreenShotServiceImplUnderTest.listJobScreenShotTask(); // Verify the results}public void testQuery() {// Setupfinal NikeScreenShotListRequestVo requestVo = new NikeScreenShotListRequestVo();requestVo.setUrl("url");requestVo.setUrlType(0);requestVo.setStartTime(new GregorianCalendar(2019, Calendar.JANUARY, 1).getTime());requestVo.setEndTime(new GregorianCalendar(2019, Calendar.JANUARY, 1).getTime());requestVo.setStatus(0);requestVo.setPcFlag(0);requestVo.setPageNum(0);requestVo.setPageSize(0);// Configure CrawerScreenShotTaskMapper.query(...).final PimScreenShotVo pimScreenShotVo = new PimScreenShotVo();pimScreenShotVo.setId(0L);pimScreenShotVo.setUrl("url");pimScreenShotVo.setImageOssUrl("imageOssUrl");pimScreenShotVo.setStatus(0);pimScreenShotVo.setPcFlag(false);pimScreenShotVo.setCreateTime(new GregorianCalendar(2019, Calendar.JANUARY, 1).getTime());pimScreenShotVo.setUrlType(0);pimScreenShotVo.setMsg("msg");final ListpimScreenShotVos = Arrays.asList(pimScreenShotVo); when(mockCrawerScreenShotTaskMapper.query(any(NikeScreenShotListRequestVo.class))).thenReturn(pimScreenShotVos);// Run the testfinal PageInforesult = crawlerScreenShotServiceImplUnderTest.query(requestVo); // Verify the results}public void testQuerySelectBoxData() {// Setup// Configure CrawerScreenShotTaskMapper.query(...).final PimScreenShotVo pimScreenShotVo = new PimScreenShotVo();pimScreenShotVo.setId(0L);pimScreenShotVo.setUrl("url");pimScreenShotVo.setImageOssUrl("imageOssUrl");pimScreenShotVo.setStatus(0);pimScreenShotVo.setPcFlag(false);pimScreenShotVo.setCreateTime(new GregorianCalendar(2019, Calendar.JANUARY, 1).getTime());pimScreenShotVo.setUrlType(0);pimScreenShotVo.setMsg("msg");final ListpimScreenShotVos = Arrays.asList(pimScreenShotVo); when(mockCrawerScreenShotTaskMapper.query(any(NikeScreenShotListRequestVo.class))).thenReturn(pimScreenShotVos);// Run the testfinal PimScreenShotTaskParamsDto result = crawlerScreenShotServiceImplUnderTest.querySelectBoxData();// Verify the results}public void testFindExecutionScreenShotTaskCount() {// Setupwhen(mockCrawerScreenShotTaskMapper.findExecutionScreenShotTaskCount()).thenReturn(0);// Run the testfinal Integer result = crawlerScreenShotServiceImplUnderTest.findExecutionScreenShotTaskCount();// Verify the resultsassertEquals(0, result);}public void testFindCrawerScreenshotTaskByCreateTime() {// Setupfinal CrawlerScreenShotTaskSyncDto crawlerScreenShotTaskSyncDto = new CrawlerScreenShotTaskSyncDto();crawlerScreenShotTaskSyncDto.setId(0L);crawlerScreenShotTaskSyncDto.setUrl("url");crawlerScreenShotTaskSyncDto.setSkuCode("skuCode");crawlerScreenShotTaskSyncDto.setTaskType(0);crawlerScreenShotTaskSyncDto.setStatus(0);crawlerScreenShotTaskSyncDto.setLastModifyTime(new GregorianCalendar(2019, Calendar.JANUARY, 1).getTime());crawlerScreenShotTaskSyncDto.setOperater("operater");crawlerScreenShotTaskSyncDto.setMsg("msg");final ListexpectedResult = Arrays.asList(crawlerScreenShotTaskSyncDto); // Configure CrawerScreenShotTaskMapper.findCrawerScreenshotTaskByCreateTime(...).final CrawlerScreenShotTaskSyncDto crawlerScreenShotTaskSyncDto1 = new CrawlerScreenShotTaskSyncDto();crawlerScreenShotTaskSyncDto1.setId(0L);crawlerScreenShotTaskSyncDto1.setUrl("url");crawlerScreenShotTaskSyncDto1.setSkuCode("skuCode");crawlerScreenShotTaskSyncDto1.setTaskType(0);crawlerScreenShotTaskSyncDto1.setStatus(0);crawlerScreenShotTaskSyncDto1.setLastModifyTime(new GregorianCalendar(2019, Calendar.JANUARY, 1).getTime());crawlerScreenShotTaskSyncDto1.setOperater("operater");crawlerScreenShotTaskSyncDto1.setMsg("msg");final ListcrawlerScreenShotTaskSyncDtos = Arrays.asList(crawlerScreenShotTaskSyncDto1); when(mockCrawerScreenShotTaskMapper.findCrawerScreenshotTaskByCreateTime(new GregorianCalendar(2019, Calendar.JANUARY, 1).getTime())).thenReturn(crawlerScreenShotTaskSyncDtos);// Run the testfinal Listresult = crawlerScreenShotServiceImplUnderTest.findCrawerScreenshotTaskByCreateTime(new GregorianCalendar(2019, Calendar.JANUARY, 1).getTime()); // Verify the resultsassertEquals(expectedResult, result);}public void testQueryCrawlerDashboard() {// Setupwhen(mockCrawerScreenShotTaskMapper.queryCrawlerDashboard(0, 0, 0, new GregorianCalendar(2019, Calendar.JANUARY, 1).getTime(), new GregorianCalendar(2019, Calendar.JANUARY, 1).getTime())).thenReturn(0);// Run the testfinal Integer result = crawlerScreenShotServiceImplUnderTest.queryCrawlerDashboard(0, 0, 0, new GregorianCalendar(2019, Calendar.JANUARY, 1).getTime(), new GregorianCalendar(2019, Calendar.JANUARY, 1).getTime());// Verify the resultsassertEquals(0, result);}}



(MockitoJUnitRunner.class)public abstract class BaseVoEntityTest<T> {protected abstract T getT();private void testGetAndSet() throws IllegalAccessException, InstantiationException, IntrospectionException,InvocationTargetException {T t = getT();Class modelClass = t.getClass();Object obj = modelClass.newInstance();Field[] fields = modelClass.getDeclaredFields();for (Field f : fields) {boolean isStatic = Modifier.isStatic(f.getModifiers());// 过滤字段if (f.getName().equals("isSerialVersionUID") || f.getName().equals("serialVersionUID") || isStatic || f.getGenericType().toString().equals("boolean")|| f.isSynthetic()) {continue;}PropertyDescriptor pd = new PropertyDescriptor(f.getName(), modelClass);Method get = pd.getReadMethod();Method set = pd.getWriteMethod();set.invoke(obj, get.invoke(obj));}}public void getAndSetTest() throws InvocationTargetException, IntrospectionException,InstantiationException, IllegalAccessException {this.testGetAndSet();}}


推荐阅读:
内容包含Java基础、JavaWeb、MySQL性能优化、JVM、锁、百万并发、消息队列、高性能缓存、反射、Spring全家桶原理、微服务、Zookeeper......等技术栈!
⬇戳阅读原文领取!                                  朕已阅 
评论

