0


单元测试 java

public Map<String,String> excuteUplocad(JSONObject json,long time,String timeTag,String sm4Key){ JSONObject json1=json; json1.put("base64长度为",json.getString(ImpServiceConstants.BASE64).length()); try{ MAP<String,SmParam> paramMap=InitParamsUtil.getInitParamsUtil().getSmParam(); if(!validateParam(json)){ throw new NullParamException(TransCode.NUll_EMTY.getCode()); } String checkfilename=paramMap.get(ImpServiceConstants.ISCHECKFILENAME).getValue(); if(checkfilename,equals("true")){ if(!validateParams2(json)){ throw new NullParamException(TransCode.ERROR_SAVEIMG.getCode()); } } Map<String,String> reportInfo=packReportInfo(json); if(!InitParamsUtil.getInitParamsUtil().getAccessSystem().containsKey(reportInfo.get(ImpServiceConstants.SYSID))){ throw new ErrorDataException(TransCode.ERROR_NOSYSTEM.getCode()); } String fileCode=reportInfo.get(ImpServiceConstants.BASE64); int fileSize=Base64Util.getFileSizeBBase64(fileCode); if(fileSize>(Integer.parseInt(paramMap.get(ImpServiceConstants.MAX).getValue()10241024))){ throw new ErrorDataException("TansCode"); } ImUser imUser=this.saveUserInfo(json); if(null!=inUser){ if(!StringUtil.isNull(imUser.getId))){ reportInfo.put(ImpServiceConstants.USERID,imUser.getId()); } } if(IMgTypeUtil.INSTANCE.validateIfImg(reportInfo.get(ImpServiceConstansts.fileTYpe))){ Map<String,String> ocrMap=updateImg(reportInfo,hdfsPath,tempFileName,imbatch,ibill,time,timeTag,reportInfo.get(ImpServiceConstansts.WRITE_DB),TRUE); long upImgTIme=System.currentTImeMillis(); log.info(timeTag+"耗时"+(upImgTime-time)); result=sendMessage(TransCode,url,returnMap.get(ImServiceContants.FIleId)); ImgSceneServiceUtil.saveScene(restTemplate,ImgSceneServiceUtil.toSaveParam(json,reportInfo)); return result; } }这个方法给我写个单测

为了编写这个方法的单元测试,首先需要确定代码中每个部分的行为以及它们的期望结果。以下是一个简单的测试框架示例,使用 JUnit 和 Mockito 进行测试。假设

  1. InitParamsUtil

  1. TransCode

  1. ImpServiceConstants

  1. Base64Util

  1. StringUtil

等类都已被正确引入。

  1. import org.junit.jupiter.api.BeforeEach;
  2. import org.junit.jupiter.api.Test;
  3. import org.mockito.InjectMocks;
  4. import org.mockito.Mock;
  5. import org.mockito.MockitoAnnotations;
  6. import static org.junit.jupiter.api.Assertions.*;
  7. import static org.mockito.Mockito.*;
  8. import java.util.HashMap;
  9. import java.util.Map;
  10. public class YourClassNameTest {
  11. @InjectMocks
  12. private YourClassName yourClass; // Replace with the actual class name containing the excuteUplocad method
  13. @Mock
  14. private InitParamsUtil initParamsUtil;
  15. @Mock
  16. private ImpServiceConstants impServiceConstants;
  17. @Mock
  18. private Base64Util base64Util;
  19. @Mock
  20. private StringUtil stringUtil;
  21. @BeforeEach
  22. public void setUp() {
  23. MockitoAnnotations.openMocks(this);
  24. }
  25. @Test
  26. public void testExcuteUplocad_Success() {
  27. // Arrange
  28. JSONObject json = new JSONObject();
  29. json.put("base64", "someBase64String");
  30. Map<String, SmParam> paramMap = new HashMap<>();
  31. SmParam mockParam = new SmParam();
  32. mockParam.setValue("true");
  33. paramMap.put(ImpServiceConstants.ISCHECKFILENAME, mockParam);
  34. when(initParamsUtil.getInitParamsUtil()).thenReturn(initParamsUtil);
  35. when(initParamsUtil.getSmParam()).thenReturn(paramMap);
  36. when(base64Util.getFileSizeBBase64(anyString())).thenReturn(1024);
  37. when(stringUtil.isNull(anyString())).thenReturn(false);
  38. when(initParamsUtil.getAccessSystem().containsKey(anyString())).thenReturn(true);
  39. // Act
  40. Map<String, String> result = yourClass.excuteUplocad(json, System.currentTimeMillis(), "timeTag", "sm4Key");
  41. // Assert
  42. assertNotNull(result);
  43. // Add additional assertions based on your logic
  44. }
  45. @Test
  46. public void testExcuteUplocad_NullParamException() {
  47. // Arrange
  48. JSONObject json = new JSONObject();
  49. when(initParamsUtil.getInitParamsUtil()).thenReturn(initParamsUtil);
  50. // Act & Assert
  51. assertThrows(NullParamException.class, () -> {
  52. yourClass.excuteUplocad(json, System.currentTimeMillis(), "timeTag", "sm4Key");
  53. });
  54. }
  55. @Test
  56. public void testExcuteUplocad_ErrorDataException() {
  57. // Arrange
  58. JSONObject json = new JSONObject();
  59. json.put("base64", "someBase64String");
  60. Map<String, SmParam> paramMap = new HashMap<>();
  61. SmParam mockParam = new SmParam();
  62. mockParam.setValue("true");
  63. paramMap.put(ImpServiceConstants.ISCHECKFILENAME, mockParam);
  64. when(initParamsUtil.getInitParamsUtil()).thenReturn(initParamsUtil);
  65. when(initParamsUtil.getSmParam()).thenReturn(paramMap);
  66. when(base64Util.getFileSizeBBase64(anyString())).thenReturn(2048 * 1024 * 1024); // Simulating a very large file
  67. // Act & Assert
  68. assertThrows(ErrorDataException.class, () -> {
  69. yourClass.excuteUplocad(json, System.currentTimeMillis(), "timeTag", "sm4Key");
  70. });
  71. }
  72. // More tests covering different scenarios can be added here
  73. }

说明:

  1. Mock对象的创建和设置:- @Mock 用于创建模拟的依赖对象。- @InjectMocks 用于注入被测类(包含方法 excuteUplocad 的类)的实例。- MockitoAnnotations.openMocks(this); 在每个测试之前初始化 Mockito
  2. 不同测试场景:- 测试方法 testExcuteUplocad_Success 模拟了一个成功的场景,并验证了方法的正常运行。- 测试方法 testExcuteUplocad_NullParamExceptiontestExcuteUplocad_ErrorDataException 用于测试异常情况。
  3. 断言:- 使用 assertThrows 断言是否抛出预期的异常。- 使用 assertNotNull 验证结果是否为非空。

这些只是基本的单元测试示例,可以根据实际业务逻辑和需求来完善和扩展测试用例。

public Map<String,String> excuteUplocad(JSONObject json,long time,String timeTag,String sm4Key){ if(IMgTypeUtil.INSTANCE.validateIfImg(reportInfo.get(ImpServiceConstansts.fileTYpe))){ Map<String,String> ocrMap=updateImg(reportInfo,hdfsPath,tempFileName,imbatch,ibill,time,timeTag,reportInfo.get(ImpServiceConstansts.WRITE_DB),TRUE); long upImgTIme=System.currentTImeMillis(); log.info(timeTag+"耗时"+(upImgTime-time)); result=sendMessage(TransCode,url,returnMap.get(ImServiceContants.FIleId)); ImgSceneServiceUtil.saveScene(restTemplate,ImgSceneServiceUtil.toSaveParam(json,reportInfo)); return result; } }这个写单侧怎么写

对于这个方法,你可以使用 JUnit 和 Mockito 来编写单元测试。以下是一个简单的示例,展示了如何为这个方法编写单元测试。请根据你的实际情况调整代码。

首先,确保你已经添加了 JUnit 和 Mockito 依赖到你的项目中。如果你使用的是 Maven,你可以在

  1. pom.xml

中添加如下依赖:

  1. <dependencies>
  2. <!-- JUnit 5 -->
  3. <dependency>
  4. <groupId>org.junit.jupiter</groupId>
  5. <artifactId>junit-jupiter-api</artifactId>
  6. <version>5.8.2</version>
  7. <scope>test</scope>
  8. </dependency>
  9. <dependency>
  10. <groupId>org.junit.jupiter</groupId>
  11. <artifactId>junit-jupiter-engine</artifactId>
  12. <version>5.8.2</version>
  13. <scope>test</scope>
  14. </dependency>
  15. <!-- Mockito -->
  16. <dependency>
  17. <groupId>org.mockito</groupId>
  18. <artifactId>mockito-core</artifactId>
  19. <version>4.7.0</version>
  20. <scope>test</scope>
  21. </dependency>
  22. </dependencies>

接下来,编写单元测试代码。假设你的方法所在的类名为

  1. YourService

,你可以创建一个测试类

  1. YourServiceTest

来编写单元测试。以下是一个简单的示例:

  1. import static org.mockito.Mockito.*;
  2. import static org.junit.jupiter.api.Assertions.*;
  3. import org.junit.jupiter.api.BeforeEach;
  4. import org.junit.jupiter.api.Test;
  5. import org.mockito.InjectMocks;
  6. import org.mockito.Mock;
  7. import org.mockito.MockitoAnnotations;
  8. import org.json.JSONObject;
  9. import java.util.Map;
  10. public class YourServiceTest {
  11. @InjectMocks
  12. private YourService yourService;
  13. @Mock
  14. private IMgTypeUtil imgTypeUtil;
  15. @Mock
  16. private SomeOtherService someOtherService; // 假设你在方法中使用了其他服务
  17. @BeforeEach
  18. public void setUp() {
  19. MockitoAnnotations.openMocks(this);
  20. }
  21. @Test
  22. public void testExecute
标签: 单元测试

本文转载自: https://blog.csdn.net/qq_33192454/article/details/144273530
版权归原作者 qq_33192454 所有, 如有侵权,请联系我们删除。

“单元测试 java”的评论:

还没有评论