Below is the code
@Service public class RefreshTokenServiceImpl implements RefreshTokenService {
@Autowired
private RefreshTokenRepository refreshTokenRepository;
@Override
public RefreshToken generateRefreshToken() {
RefreshTokenEntity tokenEntity = new RefreshTokenEntity();
tokenEntity.setToken(UUID.randomUUID().toString());
tokenEntity.setCreatedDate(Instant.now());
tokenEntity = refreshTokenRepository.save(tokenEntity);
return RefreshTokenBeanMapper.REFRESH_TOKEN_BEAN_MAPPER.refreshTokenEntityToModel(tokenEntity);
}
RefreshToken
public class RefreshToken {
private Long id;
private String token;
private Instant createdDate;
}
RefreshTokenBeanMapper
@Mapper
public interface RefreshTokenBeanMapper {
RefreshTokenBeanMapper REFRESH_TOKEN_BEAN_MAPPER = Mappers.getMapper(RefreshTokenBeanMapper.class);
RefreshToken refreshTokenEntityToModel(RefreshTokenEntity refreshTokenEntity);
}
Trying to write a suitable JUnit 5 Mockito test
As i understand, you need always generate the only one UUID and Date for yours token testing. For this, you can use experimental mockito-inline which contains functionality for static methods.
I guess you need something like this:
UUID testUUID = UUID.randomUUID();
Instant n = Instant.now();
try {
MockedStatic<UUID> uuid = Mockito.mockStatic(UUID.class)
uuid.when(UUID::randomUUID).thenReturn(testUUID);
MockedStatic<Instant> inst = Mockito.mockStatic(Instant.class)
inst.when(Instant::now).thenReturn(n);
...
}
...
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.