【Java】DT怎么写?
创始人
2024-05-30 20:42:04
0
  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

相关内容

热门资讯

中证A500ETF摩根(560... 8月22日,截止午间收盘,中证A500ETF摩根(560530)涨1.19%,报1.106元,成交额...
A500ETF易方达(1593... 8月22日,截止午间收盘,A500ETF易方达(159361)涨1.28%,报1.104元,成交额1...
何小鹏斥资约2.5亿港元增持小... 每经记者|孙磊    每经编辑|裴健如 8月21日晚间,小鹏汽车发布公告称,公司联...
中证500ETF基金(1593... 8月22日,截止午间收盘,中证500ETF基金(159337)涨0.94%,报1.509元,成交额2...
中证A500ETF华安(159... 8月22日,截止午间收盘,中证A500ETF华安(159359)涨1.15%,报1.139元,成交额...
科创AIETF(588790)... 8月22日,截止午间收盘,科创AIETF(588790)涨4.83%,报0.760元,成交额6.98...
创业板50ETF嘉实(1593... 8月22日,截止午间收盘,创业板50ETF嘉实(159373)涨2.61%,报1.296元,成交额1...
港股异动丨航空股大幅走低 中国... 港股航空股大幅下跌,其中,中国国航跌近7%表现最弱,中国东方航空跌近5%,中国南方航空跌超3%,美兰...
电网设备ETF(159326)... 8月22日,截止午间收盘,电网设备ETF(159326)跌0.25%,报1.198元,成交额409....
红利ETF国企(530880)... 8月22日,截止午间收盘,红利ETF国企(530880)跌0.67%,报1.034元,成交额29.0...