【Java】DT怎么写?
-
几个重要的注解
-
怎么用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
- 怎么写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文件:
<!--spring-test测试=--><dependency><groupId>org.springframework</groupId><artifactId>spring-test</artifactId><version>4.3.7.RELEASE</version></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-test</artifactId><version>1.5.9.RELEASE</version></dependency><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.12</version></dependency>
参考:
https://blog.csdn.net/qq_43547991/article/details/119997923
https://blog.csdn.net/sliping123/article/details/83817737