public RestResponse<PageInfo<CreditHrtActivityDTO>> queryCreditHrtActivity (CreditHrtActivityQueryDTO creditHrtActivityQueryDTO){ creditHrtActivityQueryDTO.fillDefaultPageParam(); PageInfo<CreditHrtActivityDTO> pageInfo = creditHrtActivityService.queryCreditHrtActivity(creditHrtActivityQueryDTO) return RestResponse.ok(pageInfo,message:"分页查询 华润通赠积分活动明细报表 成功!“); 请根据以上代码编写单元测试,需要使用mockito.lenient().when(xx).thenReturn,直接mock掉Serivice层的内容,
时间: 2024-03-16 14:41:11 浏览: 187
好的,以下是可能的单元测试代码:
```java
@RunWith(MockitoJUnitRunner.class)
public class CreditHrtActivityControllerTest {
@InjectMocks
private CreditHrtActivityController creditHrtActivityController;
@Mock(lenient = true)
private CreditHrtActivityService creditHrtActivityService;
@Test
public void testQueryCreditHrtActivity() {
// 构造测试数据
CreditHrtActivityQueryDTO queryDTO = new CreditHrtActivityQueryDTO();
queryDTO.setPageIndex(1);
queryDTO.setPageSize(10);
CreditHrtActivityDTO activityDTO1 = new CreditHrtActivityDTO();
activityDTO1.setId(1L);
activityDTO1.setName("Activity 1");
CreditHrtActivityDTO activityDTO2 = new CreditHrtActivityDTO();
activityDTO2.setId(2L);
activityDTO2.setName("Activity 2");
List<CreditHrtActivityDTO> activityDTOList = new ArrayList<>();
activityDTOList.add(activityDTO1);
activityDTOList.add(activityDTO2);
PageInfo<CreditHrtActivityDTO> pageInfo = new PageInfo<>(activityDTOList);
// mock Service 层的行为
Mockito.lenient().when(creditHrtActivityService.queryCreditHrtActivity(queryDTO)).thenReturn(pageInfo);
// 调用 Controller 的方法
RestResponse<PageInfo<CreditHrtActivityDTO>> response = creditHrtActivityController.queryCreditHrtActivity(queryDTO);
// 验证结果是否符合预期
Assert.assertEquals(0, response.getCode());
Assert.assertEquals("分页查询 华润通赠积分活动明细报表 成功!", response.getMessage());
Assert.assertEquals(2, response.getData().getList().size());
Assert.assertEquals(1L, response.getData().getList().get(0).getId().longValue());
Assert.assertEquals("Activity 1", response.getData().getList().get(0).getName());
Assert.assertEquals(2L, response.getData().getList().get(1).getId().longValue());
Assert.assertEquals("Activity 2", response.getData().getList().get(1).getName());
}
}
```
阅读全文