### Question:
FeatureFlagsService { public boolean shouldApplyFeatureWithFlag(long flagId) { final Optional<FlagDTO> flag = featureFlagsRepo.getFlag(flagId); return flag.map(FlagDTO::getPortionIn).map(this::randomWithinPortion) .orElse(false); } @Autowired  FeatureFlagsService(FeatureFlagsRepo featureFlagsRepo, @Value("#{new java.util.Random()}") Random random); boolean shouldApplyFeatureWithFlag(long flagId);  }### Answer:
@Test public void notApplyIfRandomIsHigherThanFlag() { when(featureFlagsRepo.getFlag(anyLong())).thenReturn(Optional.of(new FlagDTO(randomLong(), randomString(), 50))); when(random.nextInt(100)).thenReturn(70); assertFalse(featureFlagsService.shouldApplyFeatureWithFlag(randomLong())); }
@Test public void neverApplyIfFlagNotFound() { when(featureFlagsRepo.getFlag(anyLong())).thenReturn(Optional.empty()); assertFalse(featureFlagsService.shouldApplyFeatureWithFlag(randomLong())); }
@Test public void neverApplyIfFlagSetToZero() { when(featureFlagsRepo.getFlag(anyLong())).thenReturn(Optional.of(new FlagDTO(randomLong(), randomString(), 0))); assertFalse(featureFlagsService.shouldApplyFeatureWithFlag(randomLong())); }
@Test public void alwaysApplyIfFlagSetToHundred() { when(featureFlagsRepo.getFlag(anyLong())).thenReturn(Optional.of(new FlagDTO(randomLong(), randomString(), 100))); assertTrue(featureFlagsService.shouldApplyFeatureWithFlag(randomLong())); }
@Test public void applyIfRandomIsLowerThanFlag() { when(featureFlagsRepo.getFlag(anyLong())).thenReturn(Optional.of(new FlagDTO(randomLong(), randomString(), 50))); when(random.nextInt(100)).thenReturn(20); assertTrue(featureFlagsService.shouldApplyFeatureWithFlag(randomLong())); }