Skip to content

Commit 4c419d6

Browse files
committed
Unit test fixes and improvements
1 parent 2cb44d1 commit 4c419d6

9 files changed

Lines changed: 74 additions & 253 deletions

File tree

image-processing/src/main/java/com/baeldung/image/resize/core/Graphics2DExample.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
public class Graphics2DExample {
1111

12-
static BufferedImage resizeImage(BufferedImage originalImage, int targetWidth, int targetHeight) throws IOException {
12+
static BufferedImage resizeImage(BufferedImage originalImage, int targetWidth, int targetHeight) {
1313
BufferedImage resizedImage = new BufferedImage(targetWidth, targetHeight, BufferedImage.TYPE_INT_RGB);
1414
Graphics2D graphics2D = resizedImage.createGraphics();
1515
graphics2D.drawImage(originalImage, 0, 0, targetWidth, targetHeight, null);

image-processing/src/main/java/com/baeldung/image/resize/core/ImageScaledInstanceExample.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import javax.imageio.ImageIO;
99

1010
public class ImageScaledInstanceExample {
11-
static BufferedImage resizeImage(BufferedImage originalImage, int targetWidth, int targetHeight) throws IOException {
11+
static BufferedImage resizeImage(BufferedImage originalImage, int targetWidth, int targetHeight) {
1212
Image resultingImage = originalImage.getScaledInstance(targetWidth, targetHeight, Image.SCALE_DEFAULT);
1313
BufferedImage bufferedImage = new BufferedImage(targetWidth, targetHeight, BufferedImage.TYPE_INT_RGB);
1414
bufferedImage.getGraphics()

image-processing/src/main/java/com/baeldung/image/resize/imgscalr/ImgscalrExample.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@
88
import org.imgscalr.Scalr;
99

1010
public class ImgscalrExample {
11-
public static BufferedImage simpleResizeImage(BufferedImage originalImage, int targetWidth) throws Exception {
11+
public static BufferedImage simpleResizeImage(BufferedImage originalImage, int targetWidth) {
1212
return Scalr.resize(originalImage, targetWidth);
1313
}
1414

15-
public static BufferedImage resizeImage(BufferedImage originalImage, int targetWidth, int targetHeight) throws Exception {
15+
public static BufferedImage resizeImage(BufferedImage originalImage, int targetWidth, int targetHeight) {
1616
return Scalr.resize(originalImage, Scalr.Method.AUTOMATIC, Scalr.Mode.AUTOMATIC, targetWidth, targetHeight, Scalr.OP_ANTIALIAS);
1717
}
1818

image-processing/src/main/java/com/baeldung/image/resize/thumbnailator/ThumbnailatorExample.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,14 @@
44
import java.io.ByteArrayInputStream;
55
import java.io.ByteArrayOutputStream;
66
import java.io.File;
7+
import java.io.IOException;
78

89
import javax.imageio.ImageIO;
910

1011
import net.coobird.thumbnailator.Thumbnails;
1112

1213
public class ThumbnailatorExample {
13-
static BufferedImage resizeImage(BufferedImage originalImage, int targetWidth, int targetHeight) throws Exception {
14+
static BufferedImage resizeImage(BufferedImage originalImage, int targetWidth, int targetHeight) throws IOException {
1415
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
1516
Thumbnails.of(originalImage)
1617
.size(targetWidth, targetHeight)
Lines changed: 16 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
package com.baeldung.image.resize.core;
22

3-
import static org.junit.Assert.assertFalse;
43
import static org.junit.Assert.assertNotNull;
54
import static org.junit.Assert.assertNull;
6-
import static org.junit.Assert.assertTrue;
75
import static org.junit.jupiter.api.Assertions.assertEquals;
86
import static org.junit.jupiter.api.Assertions.assertNotEquals;
97

@@ -16,90 +14,58 @@
1614
import org.junit.Test;
1715

1816
public class Graphics2DExampleTest {
19-
@Test
17+
18+
@Test(expected = Test.None.class)
2019
public void whenOriginalImageExistsAndTargetSizesAreNotZero_thenImageGeneratedWithoutError() throws IOException {
2120
int targetWidth = 200;
2221
int targetHeight = 200;
23-
BufferedImage outputImage = null;
24-
boolean errorThrown = false;
2522
BufferedImage originalImage = ImageIO.read(new File("src/main/resources/images/sampleImage.jpg"));
26-
try {
27-
outputImage = Graphics2DExample.resizeImage(originalImage, targetWidth, targetHeight);
28-
} catch (Exception e) {
29-
errorThrown = true;
30-
}
23+
BufferedImage outputImage = Graphics2DExample.resizeImage(originalImage, targetWidth, targetHeight);
3124

32-
assertFalse(errorThrown);
3325
assertNotNull(outputImage);
3426
}
3527

36-
@Test
37-
public void whenOriginalImageExistsAndTargetSizesAreNotZero_thenOutputImageSizeIsValid() throws Exception {
28+
@Test(expected = Test.None.class)
29+
public void whenOriginalImageExistsAndTargetSizesAreNotZero_thenOutputImageSizeIsValid() throws IOException {
3830
int targetWidth = 200;
3931
int targetHeight = 200;
40-
boolean errorThrown = false;
41-
BufferedImage outputImage = null;
4232
BufferedImage originalImage = ImageIO.read(new File("src/main/resources/images/sampleImage.jpg"));
4333
assertNotEquals(originalImage.getWidth(), targetWidth);
4434
assertNotEquals(originalImage.getHeight(), targetHeight);
45-
try {
46-
outputImage = Graphics2DExample.resizeImage(originalImage, targetWidth, targetHeight);
47-
} catch (Exception e) {
48-
errorThrown = true;
49-
}
35+
BufferedImage outputImage = Graphics2DExample.resizeImage(originalImage, targetWidth, targetHeight);
5036

51-
assertFalse(errorThrown);
5237
assertEquals(outputImage.getWidth(), targetWidth);
5338
assertEquals(outputImage.getHeight(), targetHeight);
5439
}
5540

56-
@Test
41+
@Test(expected = Exception.class)
5742
public void whenTargetWidthIsZero_thenErrorIsThrown() throws IOException {
5843
int targetWidth = 0;
5944
int targetHeight = 200;
60-
boolean errorThrown = false;
6145
BufferedImage originalImage = ImageIO.read(new File("src/main/resources/images/sampleImage.jpg"));
62-
BufferedImage outputImage = null;
63-
try {
64-
outputImage = Graphics2DExample.resizeImage(originalImage, targetWidth, targetHeight);
65-
} catch (Exception e) {
66-
errorThrown = true;
67-
}
46+
BufferedImage outputImage = Graphics2DExample.resizeImage(originalImage, targetWidth, targetHeight);
6847

69-
assertTrue(errorThrown);
7048
assertNull(outputImage);
7149
}
7250

73-
@Test
51+
@Test(expected = Exception.class)
7452
public void whenTargetHeightIsZero_thenErrorIsThrown() throws IOException {
7553
int targetWidth = 200;
7654
int targetHeight = 0;
77-
boolean errorThrown = false;
7855
BufferedImage originalImage = ImageIO.read(new File("src/main/resources/images/sampleImage.jpg"));
79-
BufferedImage outputImage = null;
80-
try {
81-
outputImage = Graphics2DExample.resizeImage(originalImage, targetWidth, targetHeight);
82-
} catch (Exception e) {
83-
errorThrown = true;
84-
}
56+
BufferedImage outputImage = Graphics2DExample.resizeImage(originalImage, targetWidth, targetHeight);
8557

86-
assertTrue(errorThrown);
8758
assertNull(outputImage);
8859
}
8960

90-
@Test
91-
public void whenOriginalImageDoesNotExist_thenErrorIsThrown() throws IOException {
61+
@Test(expected = Test.None.class)
62+
public void whenOriginalImageDoesNotExist_thenErrorIsNotThrownAndImageIsGenerated() throws IOException {
9263
int targetWidth = 200;
9364
int targetHeight = 200;
94-
boolean errorThrown = false;
95-
BufferedImage outputImage = null;
96-
try {
97-
outputImage = Graphics2DExample.resizeImage(null, targetWidth, targetHeight);
98-
} catch (Exception e) {
99-
errorThrown = true;
100-
}
65+
BufferedImage outputImage = Graphics2DExample.resizeImage(null, targetWidth, targetHeight);
10166

102-
assertTrue(errorThrown);
103-
assertNull(outputImage);
67+
assertNotNull(outputImage);
68+
assertEquals(outputImage.getWidth(), targetWidth);
69+
assertEquals(outputImage.getHeight(), targetHeight);
10470
}
10571
}
Lines changed: 12 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
package com.baeldung.image.resize.core;
22

3-
import static org.junit.Assert.assertFalse;
43
import static org.junit.Assert.assertNotNull;
54
import static org.junit.Assert.assertNull;
6-
import static org.junit.Assert.assertTrue;
75
import static org.junit.jupiter.api.Assertions.assertEquals;
86
import static org.junit.jupiter.api.Assertions.assertNotEquals;
97

@@ -16,90 +14,56 @@
1614
import org.junit.Test;
1715

1816
public class ImageScaledInstanceExampleTest {
19-
@Test
17+
18+
@Test(expected = Test.None.class)
2019
public void whenOriginalImageExistsAndTargetSizesAreNotZero_thenImageGeneratedWithoutError() throws IOException {
2120
int targetWidth = 200;
2221
int targetHeight = 200;
23-
BufferedImage outputImage = null;
24-
boolean errorThrown = false;
2522
BufferedImage originalImage = ImageIO.read(new File("src/main/resources/images/sampleImage.jpg"));
26-
try {
27-
outputImage = ImageScaledInstanceExample.resizeImage(originalImage, targetWidth, targetHeight);
28-
} catch (Exception e) {
29-
errorThrown = true;
30-
}
23+
BufferedImage outputImage = ImageScaledInstanceExample.resizeImage(originalImage, targetWidth, targetHeight);
3124

32-
assertFalse(errorThrown);
3325
assertNotNull(outputImage);
3426
}
3527

36-
@Test
37-
public void whenOriginalImageExistsAndTargetSizesAreNotZero_thenOutputImageSizeIsValid() throws Exception {
28+
@Test(expected = Test.None.class)
29+
public void whenOriginalImageExistsAndTargetSizesAreNotZero_thenOutputImageSizeIsValid() throws IOException {
3830
int targetWidth = 200;
3931
int targetHeight = 200;
40-
boolean errorThrown = false;
41-
BufferedImage outputImage = null;
4232
BufferedImage originalImage = ImageIO.read(new File("src/main/resources/images/sampleImage.jpg"));
4333
assertNotEquals(originalImage.getWidth(), targetWidth);
4434
assertNotEquals(originalImage.getHeight(), targetHeight);
45-
try {
46-
outputImage = ImageScaledInstanceExample.resizeImage(originalImage, targetWidth, targetHeight);
47-
} catch (Exception e) {
48-
errorThrown = true;
49-
}
35+
BufferedImage outputImage = ImageScaledInstanceExample.resizeImage(originalImage, targetWidth, targetHeight);
5036

51-
assertFalse(errorThrown);
5237
assertEquals(outputImage.getWidth(), targetWidth);
5338
assertEquals(outputImage.getHeight(), targetHeight);
5439
}
5540

56-
@Test
41+
@Test(expected = Exception.class)
5742
public void whenTargetWidthIsZero_thenErrorIsThrown() throws IOException {
5843
int targetWidth = 0;
5944
int targetHeight = 200;
60-
boolean errorThrown = false;
6145
BufferedImage originalImage = ImageIO.read(new File("src/main/resources/images/sampleImage.jpg"));
62-
BufferedImage outputImage = null;
63-
try {
64-
outputImage = ImageScaledInstanceExample.resizeImage(originalImage, targetWidth, targetHeight);
65-
} catch (Exception e) {
66-
errorThrown = true;
67-
}
46+
BufferedImage outputImage = ImageScaledInstanceExample.resizeImage(originalImage, targetWidth, targetHeight);
6847

69-
assertTrue(errorThrown);
7048
assertNull(outputImage);
7149
}
7250

73-
@Test
51+
@Test(expected = Exception.class)
7452
public void whenTargetHeightIsZero_thenErrorIsThrown() throws IOException {
7553
int targetWidth = 200;
7654
int targetHeight = 0;
77-
boolean errorThrown = false;
7855
BufferedImage originalImage = ImageIO.read(new File("src/main/resources/images/sampleImage.jpg"));
79-
BufferedImage outputImage = null;
80-
try {
81-
outputImage = ImageScaledInstanceExample.resizeImage(originalImage, targetWidth, targetHeight);
82-
} catch (Exception e) {
83-
errorThrown = true;
84-
}
56+
BufferedImage outputImage = ImageScaledInstanceExample.resizeImage(originalImage, targetWidth, targetHeight);
8557

86-
assertTrue(errorThrown);
8758
assertNull(outputImage);
8859
}
8960

90-
@Test
61+
@Test(expected = Exception.class)
9162
public void whenOriginalImageDoesNotExist_thenErrorIsThrown() throws IOException {
9263
int targetWidth = 200;
9364
int targetHeight = 200;
94-
boolean errorThrown = false;
95-
BufferedImage outputImage = null;
96-
try {
97-
outputImage = ImageScaledInstanceExample.resizeImage(null, targetWidth, targetHeight);
98-
} catch (Exception e) {
99-
errorThrown = true;
100-
}
65+
BufferedImage outputImage = ImageScaledInstanceExample.resizeImage(null, targetWidth, targetHeight);
10166

102-
assertTrue(errorThrown);
10367
assertNull(outputImage);
10468
}
10569
}
Lines changed: 17 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
package com.baeldung.image.resize.imgscalr;
22

3-
import static org.junit.Assert.assertFalse;
43
import static org.junit.Assert.assertNotNull;
54
import static org.junit.Assert.assertNull;
6-
import static org.junit.Assert.assertTrue;
75
import static org.junit.jupiter.api.Assertions.assertEquals;
86
import static org.junit.jupiter.api.Assertions.assertNotEquals;
97

@@ -16,90 +14,56 @@
1614
import org.junit.Test;
1715

1816
public class ImgscalrExampleTest {
19-
@Test
17+
18+
@Test(expected = Test.None.class)
2019
public void whenOriginalImageExistsAndTargetSizesAreNotZero_thenImageGeneratedWithoutError() throws IOException {
2120
int targetWidth = 200;
2221
int targetHeight = 200;
23-
BufferedImage outputImage = null;
24-
boolean errorThrown = false;
2522
BufferedImage originalImage = ImageIO.read(new File("src/main/resources/images/sampleImage.jpg"));
26-
try {
27-
outputImage = ImgscalrExample.resizeImage(originalImage, targetWidth, targetHeight);
28-
} catch (Exception e) {
29-
errorThrown = true;
30-
}
23+
BufferedImage outputImage = ImgscalrExample.resizeImage(originalImage, targetWidth, targetHeight);
3124

32-
assertFalse(errorThrown);
3325
assertNotNull(outputImage);
3426
}
3527

36-
@Test
37-
public void whenOriginalImageExistsAndTargetSizesAreNotZero_thenOutputImageSizeIsValid() throws Exception {
28+
@Test(expected = Test.None.class)
29+
public void whenOriginalImageExistsAndTargetSizesAreNotZero_thenOutputImageSizeIsValid() throws IOException {
3830
int targetWidth = 200;
3931
int targetHeight = 200;
40-
boolean errorThrown = false;
41-
BufferedImage outputImage = null;
4232
BufferedImage originalImage = ImageIO.read(new File("src/main/resources/images/sampleImage.jpg"));
4333
assertNotEquals(originalImage.getWidth(), targetWidth);
4434
assertNotEquals(originalImage.getHeight(), targetHeight);
45-
try {
46-
outputImage = ImgscalrExample.resizeImage(originalImage, targetWidth, targetHeight);
47-
} catch (Exception e) {
48-
errorThrown = true;
49-
}
35+
BufferedImage outputImage = ImgscalrExample.resizeImage(originalImage, targetWidth, targetHeight);
5036

51-
assertFalse(errorThrown);
5237
assertEquals(outputImage.getWidth(), targetWidth);
5338
assertEquals(outputImage.getHeight(), targetHeight);
5439
}
5540

56-
@Test
57-
public void whenTargetWidthIsZero_thenErrorIsThrown() throws IOException {
41+
@Test(expected = Test.None.class)
42+
public void whenTargetWidthIsZero_thenImageIsCreated() throws IOException {
5843
int targetWidth = 0;
5944
int targetHeight = 200;
60-
boolean errorThrown = false;
6145
BufferedImage originalImage = ImageIO.read(new File("src/main/resources/images/sampleImage.jpg"));
62-
BufferedImage outputImage = null;
63-
try {
64-
outputImage = ImgscalrExample.resizeImage(originalImage, targetWidth, targetHeight);
65-
} catch (Exception e) {
66-
errorThrown = true;
67-
}
46+
BufferedImage outputImage = ImgscalrExample.resizeImage(originalImage, targetWidth, targetHeight);
6847

69-
assertTrue(errorThrown);
70-
assertNull(outputImage);
48+
assertNotNull(outputImage);
7149
}
7250

73-
@Test
74-
public void whenTargetHeightIsZero_thenErrorIsThrown() throws IOException {
51+
@Test(expected = Test.None.class)
52+
public void whenTargetHeightIsZero_thenImageIsCreated() throws IOException {
7553
int targetWidth = 200;
7654
int targetHeight = 0;
77-
boolean errorThrown = false;
7855
BufferedImage originalImage = ImageIO.read(new File("src/main/resources/images/sampleImage.jpg"));
79-
BufferedImage outputImage = null;
80-
try {
81-
outputImage = ImgscalrExample.resizeImage(originalImage, targetWidth, targetHeight);
82-
} catch (Exception e) {
83-
errorThrown = true;
84-
}
56+
BufferedImage outputImage = ImgscalrExample.resizeImage(originalImage, targetWidth, targetHeight);
8557

86-
assertTrue(errorThrown);
87-
assertNull(outputImage);
58+
assertNotNull(outputImage);
8859
}
8960

90-
@Test
91-
public void whenOriginalImageDoesNotExist_thenErrorIsThrown() throws IOException {
61+
@Test(expected = Exception.class)
62+
public void whenOriginalImageDoesNotExist_thenErrorIsThrown() {
9263
int targetWidth = 200;
9364
int targetHeight = 200;
94-
boolean errorThrown = false;
95-
BufferedImage outputImage = null;
96-
try {
97-
outputImage = ImgscalrExample.resizeImage(null, targetWidth, targetHeight);
98-
} catch (Exception e) {
99-
errorThrown = true;
100-
}
65+
BufferedImage outputImage = ImgscalrExample.resizeImage(null, targetWidth, targetHeight);
10166

102-
assertTrue(errorThrown);
10367
assertNull(outputImage);
10468
}
10569
}

0 commit comments

Comments
 (0)