gpt4 book ai didi

java - JUnit 测试。使用ModelMapper库将entity转换为DTO时出现的问题

转载 作者:行者123 更新时间:2023-11-29 04:12:32 24 4
gpt4 key购买 nike

我正在开发 Spring Boot Web 应用程序,我有一个 ModelMapper 库的自定义实现,它允许我转换单个对象和对象列表。

@Component
public class ObjectMapperUtils {

@Autowired
private static ModelMapper modelMapper;

static {
modelMapper = new ModelMapper();
modelMapper.getConfiguration().setMatchingStrategy(MatchingStrategies.STRICT);
}

private ObjectMapperUtils() {
}

public <D, T> D map(final T entity, Class<D> outClass) {
return modelMapper.map(entity, outClass);
}

public <D, T> List<D> mapAll(final Collection<T> entityList, Class<D> outCLass) {
return entityList.stream().map(entity -> map(entity, outCLass)).collect(Collectors.toList());
}
}

在服务层,我有一个从 DB UserEntity 对象返回并将其转换为 UserDTO 的方法。

@Autowired
private UserRepository userRepository;

@Autowired
private ObjectMapperUtils modelMapper;

@Override
public UserDTO getByUserId(String userId) {
UserEntity userEntity = userRepository.findByUserId(userId)
.orElseThrow(() -> new NotFoundException("User with userId[" + userId + "] not found"));
//UserDTO userDTO = new UserDTO();
//BeanUtils.copyProperties(userEntity, userDTO);
return modelMapper.map(userEntity, UserDTO.class); // userDTO;
}

当我尝试为此方法创建测试时出现问题。 UserDTO 总是返回 NULL 值。

class UserServiceImplTest {

@InjectMocks
private UserServiceImpl userService;

@Mock
private UserRepository userRepository;

@Mock
private ObjectMapperUtils modelMapper;

@BeforeEach
void setUp() throws Exception {
MockitoAnnotations.initMocks(this);
}

@Test
void testGetByUserId() {
UserEntity userEntity = new UserEntity();
userEntity.setId(1L);
userEntity.setUsername("zavada");
userEntity.setUserId("33b4c069-e907-45a9-8d49-2042044c56e0");

when(userRepository.findByUserId(anyString()))
.thenReturn(Optional.of(userEntity));

UserDTO userDTO = userService.getByUserId("33b4c069-e907-45a9-8d49-2042044c56e0");
System.out.println(userDTO); <--- NULL

assertEquals("zavada", userDTO.getUsername());
assertNotNull(userDTO);

}
}

当我在 Service 层上使用 BeanUtils.copyProperties(obj1, obj2) 转换时; - 测试成功通过。使用 ModelMapper 我得到 NULL。任何想法如何解决这个错误或重构代码?提前致谢

最佳答案

要以 user268396 的回答为基础,您需要以下内容才能使其正常工作:

@RunWith(MockitoJUnitRunner.class)
public class StackOverflowTest {

@InjectMocks
private StackOverflow userService = new StackOverflow();

@Mock
private UserRepository userRepository;

@Mock
private ObjectMapperUtils modelMapper;
private UserDTO userDTO = new UserDTO();
private UserEntity userEntity = new UserEntity();

@Before
public void setUp() {
when(modelMapper.map(any(), any())).thenReturn(userDTO);

userDTO.setId(1L);
userDTO.setUsername("zavada");
userDTO.setUserId("33b4c069-e907-45a9-8d49-2042044c56e0");
}

@Test
public void testGetByUserId() throws Throwable {
when(userRepository.findByUserId(anyString())).thenReturn(Optional.of(userEntity));

UserDTO result = userService.getByUserId("33b4c069-e907-45a9-8d49-2042044c56e0");
System.out.println(result);

assertEquals("zavada", result.getUsername());
assertNotNull(result);

}
}

这是一个很容易犯的错误,重要的是要记住所有你 @mocked 的对象都不再是真正的实现,如果你期望任何行为回来,你需要预先定义它.

关于java - JUnit 测试。使用ModelMapper库将entity转换为DTO时出现的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54197442/

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