gpt4 book ai didi

java - Junit 测试中的 RestTemplate 模拟,mockRestTemplate.postForObject() 返回 null

转载 作者:太空宇宙 更新时间:2023-11-04 10:37:45 24 4
gpt4 key购买 nike

在 Spring Boot Junit 测试中,当调用 when(mockRestTemplate.postForObject(Mockito.any(), Mockito.any(),Mockito.any())).thenReturn(searchDTO) 时,返回 null 而不是 searchDTO。

@RunWith(SpringJUnit4ClassRunner.class)
public class UserServiceImplTest {

@InjectMocks
private UserServiceImpl userService;

@Mock
private RestTemplate mockRestTemplate;

@Mock
private BeanMapperUtil mockBeanMapperUtil;

@Mock
private UserDomain mockUserDomain;

/**
* This method will test find by SSN method running as per code or not
*
* @throws Exception
*/
@Test
public void findBySSNTest() throws Exception {
UserDTO userDTO = getUserDTO("93ff6e85-fcf4-40c3-9ae8-566791daafa6");
SearchDTO searchDTO = new SearchDTO();
searchDTO.setUid("93ff6e85-fcf4-40c3-9ae8-566791daafa6");
when(mockUserDomain.findByUID("93ff6e85-fcf4-40c3-9ae8-566791daafa6"))
.thenReturn(getUser("93ff6e85-fcf4-40c3-9ae8-566791daafa6"));
when(mockRestTemplate.postForObject(Mockito.any(), Mockito.any(), Mockito.any())).thenReturn(searchDTO);
when(mockBeanMapperUtil.createDtoFromEntity(Mockito.any(), Mockito.any())).thenReturn(userDTO);
UserDTO mockUserDTO = userService.findBySSN("123-45-6022");
Assert.assertEquals(mockUserDTO, userDTO);
}

/**
*
* This method is used to get User in methods for testing
*
* @return User
*
*/
private User getUser(String id) {
User mockUser = new User();
mockUser.setUid(id);
mockUser.setFirstName("test");
mockUser.setLastName("test");
mockUser.setEmail("Test@test.com");
mockUser.setAddressLine1("Temp Address");
mockUser.setCity("Temp");
return mockUser;
}

private UserDTO getUserDTO(String id) throws ParseException {
UserDTO mockUserDTO = new UserDTO();
mockUserDTO.setUid(id);
mockUserDTO.setFirstName("test");
mockUserDTO.setLastName("test");
mockUserDTO.setEmail("Test@test.com");
mockUserDTO.setAddressLine1("Temp Address");
mockUserDTO.setCity("Temp");
return mockUserDTO;
}

}

此测试与 UserServiceImpl 类链接,这里是 UserServiceImpl 类的实现,我在其中发现 null 而不是 searchDTOSearchDTO searchDTO = restTemplate.postForObject( REST_END_POINT_STAFF_MASTER_FIND_SSN , requestBody,SearchDTO.class);

调用它的 UserServiceImpl 方法,所有其他变量都在类中定义,在通过 RestTemplate 获取 searchDTO 时发现 NullpointerException 错误。

public UserDTO findBySSN(String id) throws UserServiceException {
try {
logger.logMethodStart(MessageConstant.METHOD_FIND_BY_USER_WORK_DAY_ID);
Map<String, String> ssnMap = new HashMap<String, String>();
ssnMap.put("ssn", id);
HttpEntity<Map<String, String>> requestBody = new HttpEntity<>(ssnMap);
SearchDTO searchDTO = restTemplate.postForObject(REST_END_POINT_STAFF_MASTER_FIND_SSN, requestBody,
SearchDTO.class);
System.out.println(searchDTO);
UserDTO userDTO = null;
if (searchDTO != null && searchDTO.getUid() != null) {
logger.logMethodFlow(MessageConstant.METHOD_FIND_BY_USER_SSN, "SearchDTO :", searchDTO);
userDTO = findByUID(searchDTO.getUid());
logger.logMethodFlow(MessageConstant.METHOD_FIND_BY_USER_SSN, "Find user by UID", userDTO);
// If the DTO is null, it means request for new external user, so
// it'll create a new external user with returned UID and return it.
if (userDTO != null) {
userDTO.setNewExternalUser(false);
logger.logMethodEnd(MessageConstant.METHOD_FIND_BY_USER_SSN, userDTO);
return userDTO;
}
// If the userDTO is not null, it means found userDTO is not new external user,
// So, set corresponding field to false
userDTO = new UserDTO();
userDTO.setUid(searchDTO.getUid());
userDTO.setNewExternalUser(true);
logger.logMethodEnd(MessageConstant.METHOD_FIND_BY_USER_SSN, userDTO);
return userDTO;
} else {
String msg = messageSource.getMessage(ErrorMessageEnum.USER_STAFF_MASTER_ERROR.toString(), null,
Locale.getDefault());
logger.logMethodFlow(MessageConstant.METHOD_FIND_BY_USER_SSN, msg);
throw new UserServiceException(msg, UserServiceExceptionEnum.GET_USER_BY_SSN.toString(),
searchDTO.toString(), null);
}
} catch (UserServiceException e) {
throw e;
} catch (Exception e) {
String msg = messageSource.getMessage(ErrorMessageEnum.USER_FIND_BY_SSN_ERROR.toString(), null,
Locale.getDefault());
logger.logException(MessageConstant.METHOD_FIND_BY_USER_SSN, e, msg);
throw new UserServiceException(msg, UserServiceExceptionEnum.GET_USER_BY_SSN.toString(), e.getMessage(),
null);

}
}

最佳答案

试试这个:

when(mockRestTemplate.postForObject(Mockito.any(String.class), 
Mockito.any(HttpEntity.class),
Mockito.eq(SearchDTO.class)))
.thenReturn(searchDTO);

关于java - Junit 测试中的 RestTemplate 模拟,mockRestTemplate.postForObject() 返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49301747/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com