示例:springboot单元测试

public void testQueryIndexPatternByUserName_Success() throws Exception {
// Arrange
String userName = "testUser";

    QueryUserIndexPatternRequest request = new QueryUserIndexPatternRequest();
    request.setUserName(userName);
    UserConfig user = new UserConfig();
    user.setUserName(userName);
    UserInfo userInfo = new UserInfo();
    userInfo.setUserName(userName);
    userInfo.setOrgId("1");
    UserInfoResponse userInfoResponse = new UserInfoResponse();
    userInfoResponse.setUserList(Arrays.asList(userInfo));
    List<IndexPattern> indexPatterns = new ArrayList<>();
    indexPatterns.add(new IndexPattern().setId(1L).setIndexPatternName("pattern1"));
    indexPatterns.add(new IndexPattern().setId(2L).setIndexPatternName("pattern2"));
    when(userConfigMapper.selectOneByExample(any())).thenReturn(user);
    when(finTechDataProviderService.queryFinTechUserInfo(any())).thenReturn(userInfoResponse);
    when(indexPatternService.getUserIndexPatternByUserName(userName)).thenReturn(indexPatterns);
    QueryUserIndexPatternResponse expectedResponse = new QueryUserIndexPatternResponse();
    expectedResponse.setIndexPatternItemList(indexPatterns);
    expectedResponse.setSuccess(true);
    // Act
    QueryUserIndexPatternResponse actualResponse = userService.queryIndexPatternByUserName(request);
    // Assert
    verify(userConfigMapper, times(1)).selectOneByExample(any());
    verify(finTechDataProviderService, times(1)).queryFinTechUserInfo(any());
    verify(indexPatternService, times(1)).getUserIndexPatternByUserName(userName);
    assertTrue(actualResponse.isSuccess());
    assertEquals(expectedResponse.getIndexPatternItemList(), actualResponse.getIndexPatternItemList());
}****
posted @ 2024-11-22 09:39  一曲微茫  阅读(3)  评论(0编辑  收藏  举报