-------------

This commit is contained in:
2025-06-25 16:48:42 +08:00
parent 42aff09dae
commit bbd063c4cd
2 changed files with 75 additions and 75 deletions

View File

@ -1,4 +1,4 @@
spring: spring:
profiles: profiles:
active: dev active: test

View File

@ -1,74 +1,74 @@
package com.greenorange.promotion.junit; //package com.greenorange.promotion.junit;
//
import com.greenorange.promotion.model.dto.CommonRequest; //import com.greenorange.promotion.model.dto.CommonRequest;
import com.greenorange.promotion.model.entity.Project; //import com.greenorange.promotion.model.entity.Project;
import com.greenorange.promotion.model.vo.project.ProjectVO; //import com.greenorange.promotion.model.vo.project.ProjectVO;
import com.greenorange.promotion.service.common.CommonService; //import com.greenorange.promotion.service.common.CommonService;
import com.greenorange.promotion.service.project.ProjectService; //import com.greenorange.promotion.service.project.ProjectService;
import com.greenorange.promotion.service.project.impl.ProjectServiceImpl; //import com.greenorange.promotion.service.project.impl.ProjectServiceImpl;
import org.junit.jupiter.api.Test; //import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith; //import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks; //import org.mockito.InjectMocks;
import org.mockito.Mock; //import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension; //import org.mockito.junit.jupiter.MockitoExtension;
//
import static org.junit.jupiter.api.Assertions.*; //import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.*; //import static org.mockito.Mockito.*;
//
@ExtendWith(MockitoExtension.class) //@ExtendWith(MockitoExtension.class)
class ProjectServiceImplTest { //class ProjectServiceImplTest {
//
@InjectMocks // @InjectMocks
private ProjectServiceImpl service; // private ProjectServiceImpl service;
// 把真正的业务实现类注入进来 // // 把真正的业务实现类注入进来
//
@Mock // @Mock
private CommonService commonService; // private CommonService commonService;
// 用于 copyProperties // // 用于 copyProperties
//
@Mock // @Mock
private ProjectService projectService; // private ProjectService projectService;
// 用于 getById // // 用于 getById
//
@Test // @Test
void queryProjectById_notFound_throwsException() { // void queryProjectById_notFound_throwsException() {
// Arrange // // Arrange
CommonRequest req = new CommonRequest(); // CommonRequest req = new CommonRequest();
req.setId(10L); // req.setId(10L);
when(projectService.getById(10L)).thenReturn(null); // when(projectService.getById(10L)).thenReturn(null);
// Act & Assert // // Act & Assert
RuntimeException ex = assertThrows(RuntimeException.class, () -> // RuntimeException ex = assertThrows(RuntimeException.class, () ->
service.queryProjectById(req) // service.queryProjectById(req)
); // );
assertTrue(ex.getMessage().equals("当前项目不存在")); // assertTrue(ex.getMessage().equals("当前项目不存在"));
//
// commonService.copyProperties 不应被调用 // // commonService.copyProperties 不应被调用
verify(commonService, never()).copyProperties(any(), any()); // verify(commonService, never()).copyProperties(any(), any());
} // }
//
@Test // @Test
void queryProjectById_found_returnsVO() { // void queryProjectById_found_returnsVO() {
// Arrange // // Arrange
Long projectId = 20L; // Long projectId = 20L;
CommonRequest req = new CommonRequest(); // CommonRequest req = new CommonRequest();
req.setId(projectId); // req.setId(projectId);
//
Project project = new Project(); // Project project = new Project();
project.setId(projectId); // project.setId(projectId);
project.setProjectName("示例项目"); // project.setProjectName("示例项目");
when(projectService.getById(projectId)).thenReturn(project); // when(projectService.getById(projectId)).thenReturn(project);
//
ProjectVO vo = new ProjectVO(); // ProjectVO vo = new ProjectVO();
vo.setId(projectId); // vo.setId(projectId);
vo.setProjectName("示例项目"); // vo.setProjectName("示例项目");
when(commonService.copyProperties(project, ProjectVO.class)) // when(commonService.copyProperties(project, ProjectVO.class))
.thenReturn(vo); // .thenReturn(vo);
//
// Act // // Act
ProjectVO result = service.queryProjectById(req); // ProjectVO result = service.queryProjectById(req);
//
// Assert // // Assert
assertSame(vo, result, "应返回 commonService.copyProperties 的结果"); // assertSame(vo, result, "应返回 commonService.copyProperties 的结果");
verify(commonService, times(1)).copyProperties(project, ProjectVO.class); // verify(commonService, times(1)).copyProperties(project, ProjectVO.class);
} // }
} //}