### Question:
CloudStorageService { @SuppressWarnings("SpellCheckingInspection") public byte[] chooseImage() { var images = cloudStorageProperties.getImages(); if (CollectionUtils.isEmpty(images)) { throw new RuntimeException("`cloudstorage`.`images` should not be empty"); } int chosen = random.nextInt(images.size()); var chosenImage = images.get(chosen); return cloudStorage.get(chosenImage.getBucket(), chosenImage.getName()); }  @SuppressWarnings("SpellCheckingInspection") byte[] chooseImage();  }### Answer:
@Test public void chooseImageTest() { final List<CloudStorageProperties.Image> images = List.of( new CloudStorageProperties.Image("bucket1", "name1"), new CloudStorageProperties.Image("bucket2", "name2"), new CloudStorageProperties.Image("bucket3", "name3") ); final byte[] imageBytes = new byte[]{}; when(cloudStorageProperties.getImages()).thenReturn(images); when(random.nextInt(images.size())).thenReturn(2); when(cloudStorage.get("bucket3", "name3")).thenReturn(imageBytes); assertSame(imageBytes, cloudStorageService.chooseImage()); verify(cloudStorageProperties).getImages(); verify(random).nextInt(images.size()); verify(cloudStorage).get("bucket3", "name3"); }
@Test public void chooseImageNullImagesTest() { expectedException.expect(RuntimeException.class); expectedException.expectMessage("`cloudstorage`.`images` should not be empty"); when(cloudStorageProperties.getImages()).thenReturn(null); cloudStorageService.chooseImage(); verify(cloudStorageProperties).getImages(); }
@Test public void chooseImageEmptyImagesTest() { expectedException.expect(RuntimeException.class); expectedException.expectMessage("`cloudstorage`.`images` should not be empty"); when(cloudStorageProperties.getImages()).thenReturn(Collections.emptyList()); cloudStorageService.chooseImage(); verify(cloudStorageProperties).getImages(); }