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