Skip to content

Commit 3ea9a2e

Browse files
committed
Try with resources/assert Throws
1 parent fc3e627 commit 3ea9a2e

1 file changed

Lines changed: 21 additions & 41 deletions

File tree

src/test/java/org/apache/commons/io/output/FileWriterWithEncodingTest.java

Lines changed: 21 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import static org.apache.commons.io.test.TestUtils.checkFile;
2020
import static org.junit.jupiter.api.Assertions.assertFalse;
21+
import static org.junit.jupiter.api.Assertions.assertThrows;
2122
import static org.junit.jupiter.api.Assertions.assertTrue;
2223
import static org.junit.jupiter.api.Assertions.fail;
2324

@@ -30,7 +31,6 @@
3031
import java.nio.charset.Charset;
3132
import java.nio.charset.CharsetEncoder;
3233

33-
import org.apache.commons.io.IOUtils;
3434
import org.junit.jupiter.api.BeforeEach;
3535
import org.junit.jupiter.api.Test;
3636
import org.junit.jupiter.api.io.TempDir;
@@ -171,64 +171,44 @@ private void writeTestPayload(final FileWriter fw1, final FileWriterWithEncoding
171171
//-----------------------------------------------------------------------
172172
@Test
173173
public void constructor_File_encoding_badEncoding() {
174-
Writer writer = null;
175-
try {
176-
writer = new FileWriterWithEncoding(file1, "BAD-ENCODE");
177-
fail();
178-
} catch (final IOException ex) {
179-
// expected
180-
assertFalse(file1.exists());
181-
} finally {
182-
IOUtils.closeQuietly(writer);
183-
}
174+
assertThrows(IOException.class, () -> {
175+
try (
176+
Writer writer = new FileWriterWithEncoding(file1, "BAD-ENCODE");
177+
){ }
178+
});
184179
assertFalse(file1.exists());
185180
}
186181

187182
//-----------------------------------------------------------------------
188183
@Test
189184
public void constructor_File_directory() {
190-
Writer writer = null;
191-
try {
192-
writer = new FileWriterWithEncoding(temporaryFolder, defaultEncoding);
193-
fail();
194-
} catch (final IOException ex) {
195-
// expected
196-
assertFalse(file1.exists());
197-
} finally {
198-
IOUtils.closeQuietly(writer);
199-
}
185+
assertThrows(IOException.class, () -> {
186+
try (
187+
Writer writer = new FileWriterWithEncoding(temporaryFolder, defaultEncoding);
188+
){ }
189+
});
200190
assertFalse(file1.exists());
201191
}
202192

203193
//-----------------------------------------------------------------------
204194
@Test
205195
public void constructor_File_nullFile() throws IOException {
206-
Writer writer = null;
207-
try {
208-
writer = new FileWriterWithEncoding((File) null, defaultEncoding);
209-
fail();
210-
} catch (final NullPointerException ex) {
211-
// expected
212-
assertFalse(file1.exists());
213-
} finally {
214-
IOUtils.closeQuietly(writer);
215-
}
196+
assertThrows(NullPointerException.class, () -> {
197+
try (
198+
Writer writer = new FileWriterWithEncoding((File) null, defaultEncoding);
199+
){ }
200+
});
216201
assertFalse(file1.exists());
217202
}
218203

219204
//-----------------------------------------------------------------------
220205
@Test
221206
public void constructor_fileName_nullFile() throws IOException {
222-
Writer writer = null;
223-
try {
224-
writer = new FileWriterWithEncoding((String) null, defaultEncoding);
225-
fail();
226-
} catch (final NullPointerException ex) {
227-
// expected
228-
assertFalse(file1.exists());
229-
} finally {
230-
IOUtils.closeQuietly(writer);
231-
}
207+
assertThrows(NullPointerException.class, () -> {
208+
try (
209+
Writer writer = new FileWriterWithEncoding((String) null, defaultEncoding);
210+
){ }
211+
});
232212
assertFalse(file1.exists());
233213
}
234214

0 commit comments

Comments
 (0)