【Java】DT怎么写?
创始人
2024-05-30 20:42:04
  1. 几个重要的注解
    在这里插入图片描述

  2. 怎么用mockito写单元测试?

package Biz;import Client.FileIOClient;
import Req.FileRequest;
import Res.FileResponse;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.when;/*** @Author:TieJiang* @Date: 2021/1/19 8:44 下午* 人一能之,己十之,人十能之,己百之,果能此道矣,虽愚必明,虽柔必强。*/
@RunWith(MockitoJUnitRunner.class)
public class FileIOBizTest {@InjectMocksFileIOBiz fileIOBiz;@MockFileIOClient fileIOClient;@Testpublic void getFile() {
//        Mock阶段
//        构造参数FileRequest request = new FileRequest();request.setName("Title");
//        Mock Clientwhen(fileIOClient.FileRead(any())).thenReturn(5);when(fileIOClient.FileWrite(any())).thenReturn(5);//        Mock结果,进行校验:结果一定是根据现有逻辑能判断出来的FileResponse response = fileIOBiz.GetFile(request);Assert.assertNotNull(response);Assert.assertEquals(response.getValue(),"5 5");}
}

参考代码:https://github.com/OriKey/MockTutorials/tree/master

  1. 怎么写SpringRunner的单元测试?
@RunWith(SpringRunner.class) //14.版本之前用的是SpringJUnit4ClassRunner.class
@SpringBootTest(classes = Application.class) //1.4版本之前用的是//@SpringApplicationConfiguration(classes = Application.class)
public class SystemInfoServiceImplTest {@Autowiredprivate ISystemInfoService systemInfoservice;@Testpublic void add() throws Exception {}@Testpublic void findAll() throws Exception {}}

pom文件:

org.springframeworkspring-test4.3.7.RELEASEorg.springframework.bootspring-boot-test1.5.9.RELEASEjunitjunit4.12

参考:
https://blog.csdn.net/qq_43547991/article/details/119997923
https://blog.csdn.net/sliping123/article/details/83817737

相关内容

热门资讯

“最快女护士”张水华开直播:白... 2026年1月8日晚,“最快女护士”张水华与丈夫王岢一同现身社交媒体直播,这也是她自1月2日宣布辞职...
清华2026年首篇Scienc...   炒股就看金麒麟分析师研报,权威,专业,及时,全面,助您挖掘潜力主题机会! (来源:IT之家)I...
成交额3265.29亿元,内蒙... 转自:草原云1月9日,记者从内蒙古自治区公共资源交易中心获悉,2025年,全区完成公共资源交易项目2...
曲光吉职务调整!毕业于西安一高... 据中航集团网站消息,2026年1月9日上午,中国航空集团有限公司召开领导班子(扩大)会议。受中央组织...
不到20万起售!2026款小鹏...  今日,在2026小鹏全球新品发布会上,小鹏汽车正式发布2026 款小鹏 P7+、小鹏 G7 超级增...