Skip to content

Commit 750a8b3

Browse files
committed
Stop using Mockito to spy() on JDK I/O streams
When running on JDK 16+, we are not able to spy() on JDK types. To address this, this commit stops using Mockito to spy on JDK I/O streams (such as ByteArrayInputStream and ByteArrayOutputStream).
1 parent c838bcf commit 750a8b3

16 files changed

+175
-299
lines changed

spring-web/src/test/java/org/springframework/http/MockHttpOutputMessage.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 the original author or authors.
2+
* Copyright 2002-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -21,8 +21,6 @@
2121
import java.io.OutputStream;
2222
import java.nio.charset.Charset;
2323

24-
import static org.mockito.Mockito.spy;
25-
2624
/**
2725
* @author Arjen Poutsma
2826
* @author Rossen Stoyanchev
@@ -31,7 +29,7 @@ public class MockHttpOutputMessage implements HttpOutputMessage {
3129

3230
private final HttpHeaders headers = new HttpHeaders();
3331

34-
private final ByteArrayOutputStream body = spy(new ByteArrayOutputStream());
32+
private final ByteArrayOutputStream body = new ByteArrayOutputStream();
3533

3634
private boolean headersWritten = false;
3735

spring-web/src/test/java/org/springframework/http/converter/BufferedImageHttpMessageConverterTests.java

Lines changed: 13 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -19,68 +19,57 @@
1919
import java.awt.image.BufferedImage;
2020
import java.io.ByteArrayInputStream;
2121
import java.io.IOException;
22-
import java.io.InputStream;
2322

2423
import javax.imageio.ImageIO;
2524

26-
import org.junit.jupiter.api.BeforeEach;
2725
import org.junit.jupiter.api.Test;
2826

2927
import org.springframework.core.io.ClassPathResource;
3028
import org.springframework.core.io.Resource;
3129
import org.springframework.http.MediaType;
3230
import org.springframework.http.MockHttpInputMessage;
3331
import org.springframework.http.MockHttpOutputMessage;
34-
import org.springframework.util.FileCopyUtils;
3532

3633
import static org.assertj.core.api.Assertions.assertThat;
37-
import static org.mockito.Mockito.never;
38-
import static org.mockito.Mockito.spy;
39-
import static org.mockito.Mockito.verify;
4034

4135
/**
42-
* Unit tests for BufferedImageHttpMessageConverter.
36+
* Unit tests for {@link BufferedImageHttpMessageConverter}.
37+
*
4338
* @author Arjen Poutsma
4439
* @author Rossen Stoyanchev
40+
* @author Sam Brannen
4541
*/
46-
public class BufferedImageHttpMessageConverterTests {
42+
class BufferedImageHttpMessageConverterTests {
4743

48-
private BufferedImageHttpMessageConverter converter;
44+
private final BufferedImageHttpMessageConverter converter = new BufferedImageHttpMessageConverter();
45+
46+
private final Resource logo = new ClassPathResource("logo.jpg", getClass());
4947

50-
@BeforeEach
51-
public void setUp() {
52-
converter = new BufferedImageHttpMessageConverter();
53-
}
5448

5549
@Test
56-
public void canRead() {
50+
void canRead() {
5751
assertThat(converter.canRead(BufferedImage.class, null)).as("Image not supported").isTrue();
5852
assertThat(converter.canRead(BufferedImage.class, new MediaType("image", "png"))).as("Image not supported").isTrue();
5953
}
6054

6155
@Test
62-
public void canWrite() {
56+
void canWrite() {
6357
assertThat(converter.canWrite(BufferedImage.class, null)).as("Image not supported").isTrue();
6458
assertThat(converter.canWrite(BufferedImage.class, new MediaType("image", "png"))).as("Image not supported").isTrue();
6559
assertThat(converter.canWrite(BufferedImage.class, new MediaType("*", "*"))).as("Image not supported").isTrue();
6660
}
6761

6862
@Test
69-
public void read() throws IOException {
70-
Resource logo = new ClassPathResource("logo.jpg", BufferedImageHttpMessageConverterTests.class);
71-
byte[] body = FileCopyUtils.copyToByteArray(logo.getInputStream());
72-
InputStream inputStream = spy(new ByteArrayInputStream(body));
73-
MockHttpInputMessage inputMessage = new MockHttpInputMessage(inputStream);
63+
void read() throws IOException {
64+
MockHttpInputMessage inputMessage = new MockHttpInputMessage(logo.getInputStream());
7465
inputMessage.getHeaders().setContentType(new MediaType("image", "jpeg"));
7566
BufferedImage result = converter.read(BufferedImage.class, inputMessage);
7667
assertThat(result.getHeight()).as("Invalid height").isEqualTo(500);
7768
assertThat(result.getWidth()).as("Invalid width").isEqualTo(750);
78-
verify(inputStream, never()).close();
7969
}
8070

8171
@Test
82-
public void write() throws IOException {
83-
Resource logo = new ClassPathResource("logo.jpg", BufferedImageHttpMessageConverterTests.class);
72+
void write() throws IOException {
8473
BufferedImage body = ImageIO.read(logo.getFile());
8574
MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
8675
MediaType contentType = new MediaType("image", "png");
@@ -90,12 +79,10 @@ public void write() throws IOException {
9079
BufferedImage result = ImageIO.read(new ByteArrayInputStream(outputMessage.getBodyAsBytes()));
9180
assertThat(result.getHeight()).as("Invalid height").isEqualTo(500);
9281
assertThat(result.getWidth()).as("Invalid width").isEqualTo(750);
93-
verify(outputMessage.getBody(), never()).close();
9482
}
9583

9684
@Test
97-
public void writeDefaultContentType() throws IOException {
98-
Resource logo = new ClassPathResource("logo.jpg", BufferedImageHttpMessageConverterTests.class);
85+
void writeDefaultContentType() throws IOException {
9986
MediaType contentType = new MediaType("image", "png");
10087
converter.setDefaultContentType(contentType);
10188
BufferedImage body = ImageIO.read(logo.getFile());

spring-web/src/test/java/org/springframework/http/converter/FormHttpMessageConverterTests.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2020 the original author or authors.
2+
* Copyright 2002-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -48,8 +48,6 @@
4848
import org.springframework.util.MultiValueMap;
4949

5050
import static org.assertj.core.api.Assertions.assertThat;
51-
import static org.mockito.Mockito.never;
52-
import static org.mockito.Mockito.verify;
5351
import static org.springframework.http.MediaType.APPLICATION_FORM_URLENCODED;
5452
import static org.springframework.http.MediaType.MULTIPART_FORM_DATA;
5553
import static org.springframework.http.MediaType.MULTIPART_MIXED;
@@ -229,7 +227,6 @@ public String getFilename() {
229227
item = items.get(5);
230228
assertThat(item.getFieldName()).isEqualTo("xml");
231229
assertThat(item.getContentType()).isEqualTo("text/xml");
232-
verify(outputMessage.getBody(), never()).close();
233230
}
234231

235232
@Test // SPR-13309

spring-web/src/test/java/org/springframework/http/converter/feed/AtomFeedHttpMessageConverterTests.java

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,6 @@
3838

3939
import static java.util.Collections.singletonMap;
4040
import static org.assertj.core.api.Assertions.assertThat;
41-
import static org.mockito.Mockito.never;
42-
import static org.mockito.Mockito.spy;
43-
import static org.mockito.Mockito.verify;
4441

4542
/**
4643
* @author Arjen Poutsma
@@ -74,7 +71,7 @@ public void canWrite() {
7471

7572
@Test
7673
public void read() throws IOException {
77-
InputStream inputStream = spy(getClass().getResourceAsStream("atom.xml"));
74+
InputStream inputStream = getClass().getResourceAsStream("atom.xml");
7875
MockHttpInputMessage inputMessage = new MockHttpInputMessage(inputStream);
7976
inputMessage.getHeaders().setContentType(ATOM_XML_UTF8);
8077
Feed result = converter.read(Feed.class, inputMessage);
@@ -90,7 +87,6 @@ public void read() throws IOException {
9087
Entry entry2 = (Entry) entries.get(1);
9188
assertThat(entry2.getId()).isEqualTo("id2");
9289
assertThat(entry2.getTitle()).isEqualTo("title2");
93-
verify(inputStream, never()).close();
9490
}
9591

9692
@Test
@@ -123,7 +119,6 @@ public void write() throws IOException {
123119
NodeMatcher nm = new DefaultNodeMatcher(ElementSelectors.byName);
124120
assertThat(XmlContent.of(outputMessage.getBodyAsString(StandardCharsets.UTF_8)))
125121
.isSimilarToIgnoringWhitespace(expected, nm);
126-
verify(outputMessage.getBody(), never()).close();
127122
}
128123

129124
@Test

spring-web/src/test/java/org/springframework/http/converter/feed/RssChannelHttpMessageConverterTests.java

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,6 @@
3434

3535
import static java.util.Collections.singletonMap;
3636
import static org.assertj.core.api.Assertions.assertThat;
37-
import static org.mockito.Mockito.never;
38-
import static org.mockito.Mockito.spy;
39-
import static org.mockito.Mockito.verify;
4037

4138
/**
4239
* @author Arjen Poutsma
@@ -59,7 +56,7 @@ public void canReadAndWrite() {
5956

6057
@Test
6158
public void read() throws IOException {
62-
InputStream inputStream = spy(getClass().getResourceAsStream("rss.xml"));
59+
InputStream inputStream = getClass().getResourceAsStream("rss.xml");
6360
MockHttpInputMessage inputMessage = new MockHttpInputMessage(inputStream);
6461
inputMessage.getHeaders().setContentType(RSS_XML_UTF8);
6562
Channel result = converter.read(Channel.class, inputMessage);
@@ -75,7 +72,6 @@ public void read() throws IOException {
7572

7673
Item item2 = (Item) items.get(1);
7774
assertThat(item2.getTitle()).isEqualTo("title2");
78-
verify(inputStream, never()).close();
7975
}
8076

8177
@Test
@@ -109,7 +105,6 @@ public void write() throws IOException {
109105
"</channel></rss>";
110106
assertThat(XmlContent.of(outputMessage.getBodyAsString(StandardCharsets.UTF_8)))
111107
.isSimilarToIgnoringWhitespace(expected);
112-
verify(outputMessage.getBody(), never()).close();
113108
}
114109

115110
@Test

spring-web/src/test/java/org/springframework/http/converter/json/GsonHttpMessageConverterTests.java

Lines changed: 16 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,7 @@
1616

1717
package org.springframework.http.converter.json;
1818

19-
import java.io.ByteArrayInputStream;
2019
import java.io.IOException;
21-
import java.io.InputStream;
2220
import java.lang.reflect.Field;
2321
import java.lang.reflect.Type;
2422
import java.nio.charset.Charset;
@@ -40,45 +38,41 @@
4038
import static org.assertj.core.api.Assertions.assertThat;
4139
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
4240
import static org.assertj.core.api.Assertions.within;
43-
import static org.mockito.Mockito.never;
44-
import static org.mockito.Mockito.spy;
45-
import static org.mockito.Mockito.verify;
4641

4742
/**
4843
* Gson 2.x converter tests.
4944
*
5045
* @author Roy Clarkson
5146
* @author Juergen Hoeller
5247
*/
53-
public class GsonHttpMessageConverterTests {
48+
class GsonHttpMessageConverterTests {
5449

5550
private final GsonHttpMessageConverter converter = new GsonHttpMessageConverter();
5651

5752

5853
@Test
59-
public void canRead() {
54+
void canRead() {
6055
assertThat(this.converter.canRead(MyBean.class, new MediaType("application", "json"))).isTrue();
6156
assertThat(this.converter.canRead(Map.class, new MediaType("application", "json"))).isTrue();
6257
}
6358

6459
@Test
65-
public void canWrite() {
60+
void canWrite() {
6661
assertThat(this.converter.canWrite(MyBean.class, new MediaType("application", "json"))).isTrue();
6762
assertThat(this.converter.canWrite(Map.class, new MediaType("application", "json"))).isTrue();
6863
}
6964

7065
@Test
71-
public void canReadAndWriteMicroformats() {
66+
void canReadAndWriteMicroformats() {
7267
assertThat(this.converter.canRead(MyBean.class, new MediaType("application", "vnd.test-micro-type+json"))).isTrue();
7368
assertThat(this.converter.canWrite(MyBean.class, new MediaType("application", "vnd.test-micro-type+json"))).isTrue();
7469
}
7570

7671
@Test
77-
public void readTyped() throws IOException {
72+
void readTyped() throws IOException {
7873
String body = "{\"bytes\":[1,2],\"array\":[\"Foo\",\"Bar\"]," +
7974
"\"number\":42,\"string\":\"Foo\",\"bool\":true,\"fraction\":42.0}";
80-
InputStream inputStream = spy(new ByteArrayInputStream(body.getBytes(StandardCharsets.UTF_8)));
81-
MockHttpInputMessage inputMessage = new MockHttpInputMessage(inputStream);
75+
MockHttpInputMessage inputMessage = new MockHttpInputMessage(body.getBytes(StandardCharsets.UTF_8));
8276
inputMessage.getHeaders().setContentType(new MediaType("application", "json"));
8377
MyBean result = (MyBean) this.converter.read(MyBean.class, inputMessage);
8478

@@ -89,12 +83,11 @@ public void readTyped() throws IOException {
8983
assertThat(result.getArray()).isEqualTo(new String[] {"Foo", "Bar"});
9084
assertThat(result.isBool()).isTrue();
9185
assertThat(result.getBytes()).isEqualTo(new byte[] {0x1, 0x2});
92-
verify(inputStream, never()).close();
9386
}
9487

9588
@Test
9689
@SuppressWarnings("unchecked")
97-
public void readUntyped() throws IOException {
90+
void readUntyped() throws IOException {
9891
String body = "{\"bytes\":[1,2],\"array\":[\"Foo\",\"Bar\"]," +
9992
"\"number\":42,\"string\":\"Foo\",\"bool\":true,\"fraction\":42.0}";
10093
MockHttpInputMessage inputMessage = new MockHttpInputMessage(body.getBytes("UTF-8"));
@@ -120,7 +113,7 @@ public void readUntyped() throws IOException {
120113
}
121114

122115
@Test
123-
public void write() throws IOException {
116+
void write() throws IOException {
124117
MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
125118
MyBean body = new MyBean();
126119
body.setString("Foo");
@@ -140,11 +133,10 @@ public void write() throws IOException {
140133
assertThat(result.contains("\"bytes\":[1,2]")).isTrue();
141134
assertThat(outputMessage.getHeaders().getContentType())
142135
.as("Invalid content-type").isEqualTo(new MediaType("application", "json", utf8));
143-
verify(outputMessage.getBody(), never()).close();
144136
}
145137

146138
@Test
147-
public void writeWithBaseType() throws IOException {
139+
void writeWithBaseType() throws IOException {
148140
MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
149141
MyBean body = new MyBean();
150142
body.setString("Foo");
@@ -167,7 +159,7 @@ public void writeWithBaseType() throws IOException {
167159
}
168160

169161
@Test
170-
public void writeUTF16() throws IOException {
162+
void writeUTF16() throws IOException {
171163
MediaType contentType = new MediaType("application", "json", StandardCharsets.UTF_16BE);
172164
MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
173165
String body = "H\u00e9llo W\u00f6rld";
@@ -177,7 +169,7 @@ public void writeUTF16() throws IOException {
177169
}
178170

179171
@Test
180-
public void readInvalidJson() throws IOException {
172+
void readInvalidJson() throws IOException {
181173
String body = "FooBar";
182174
MockHttpInputMessage inputMessage = new MockHttpInputMessage(body.getBytes(StandardCharsets.UTF_8));
183175
inputMessage.getHeaders().setContentType(new MediaType("application", "json"));
@@ -187,7 +179,7 @@ public void readInvalidJson() throws IOException {
187179

188180
@Test
189181
@SuppressWarnings("unchecked")
190-
public void readAndWriteGenerics() throws Exception {
182+
void readAndWriteGenerics() throws Exception {
191183
Field beansList = ListHolder.class.getField("listField");
192184

193185
String body = "[{\"bytes\":[1,2],\"array\":[\"Foo\",\"Bar\"]," +
@@ -214,7 +206,7 @@ public void readAndWriteGenerics() throws Exception {
214206

215207
@Test
216208
@SuppressWarnings("unchecked")
217-
public void readAndWriteParameterizedType() throws Exception {
209+
void readAndWriteParameterizedType() throws Exception {
218210
ParameterizedTypeReference<List<MyBean>> beansList = new ParameterizedTypeReference<List<MyBean>>() {
219211
};
220212

@@ -241,7 +233,7 @@ public void readAndWriteParameterizedType() throws Exception {
241233

242234
@Test
243235
@SuppressWarnings("unchecked")
244-
public void writeParameterizedBaseType() throws Exception {
236+
void writeParameterizedBaseType() throws Exception {
245237
ParameterizedTypeReference<List<MyBean>> beansList = new ParameterizedTypeReference<List<MyBean>>() {};
246238
ParameterizedTypeReference<List<MyBase>> baseList = new ParameterizedTypeReference<List<MyBase>>() {};
247239

@@ -267,15 +259,15 @@ public void writeParameterizedBaseType() throws Exception {
267259
}
268260

269261
@Test
270-
public void prefixJson() throws IOException {
262+
void prefixJson() throws IOException {
271263
MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
272264
this.converter.setPrefixJson(true);
273265
this.converter.writeInternal("foo", null, outputMessage);
274266
assertThat(outputMessage.getBodyAsString(StandardCharsets.UTF_8)).isEqualTo(")]}', \"foo\"");
275267
}
276268

277269
@Test
278-
public void prefixJsonCustom() throws IOException {
270+
void prefixJsonCustom() throws IOException {
279271
MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
280272
this.converter.setJsonPrefix(")))");
281273
this.converter.writeInternal("foo", null, outputMessage);

0 commit comments

Comments
 (0)