Try with resources/assert Throws
import static org.apache.commons.io.test.TestUtils.checkFile;
import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;
import java.nio.charset.Charset;
import java.nio.charset.CharsetEncoder;
-import org.apache.commons.io.IOUtils;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
@@ -171,64 +171,44 @@ private void writeTestPayload(final FileWriter fw1, final FileWriterWithEncoding
//-----------------------------------------------------------------------
@Test
public void constructor_File_encoding_badEncoding() {
- Writer writer = null;
- try {
- writer = new FileWriterWithEncoding(file1, "BAD-ENCODE");
- fail();
- } catch (final IOException ex) {
- // expected
- assertFalse(file1.exists());
- } finally {
- IOUtils.closeQuietly(writer);
- }
+ assertThrows(IOException.class, () -> {
+ try (
+ Writer writer = new FileWriterWithEncoding(file1, "BAD-ENCODE");
+ ){ }
+ });
assertFalse(file1.exists());
}
//-----------------------------------------------------------------------
@Test
public void constructor_File_directory() {
- Writer writer = null;
- try {
- writer = new FileWriterWithEncoding(temporaryFolder, defaultEncoding);
- fail();
- } catch (final IOException ex) {
- // expected
- assertFalse(file1.exists());
- } finally {
- IOUtils.closeQuietly(writer);
- }
+ assertThrows(IOException.class, () -> {
+ try (
+ Writer writer = new FileWriterWithEncoding(temporaryFolder, defaultEncoding);
+ ){ }
+ });
assertFalse(file1.exists());
}
//-----------------------------------------------------------------------
@Test
public void constructor_File_nullFile() throws IOException {
- Writer writer = null;
- try {
- writer = new FileWriterWithEncoding((File) null, defaultEncoding);
- fail();
- } catch (final NullPointerException ex) {
- // expected
- assertFalse(file1.exists());
- } finally {
- IOUtils.closeQuietly(writer);
- }
+ assertThrows(NullPointerException.class, () -> {
+ try (
+ Writer writer = new FileWriterWithEncoding((File) null, defaultEncoding);
+ ){ }
+ });
assertFalse(file1.exists());
}
//-----------------------------------------------------------------------
@Test
public void constructor_fileName_nullFile() throws IOException {
- Writer writer = null;
- try {
- writer = new FileWriterWithEncoding((String) null, defaultEncoding);
- fail();
- } catch (final NullPointerException ex) {
- // expected
- assertFalse(file1.exists());
- } finally {
- IOUtils.closeQuietly(writer);
- }
+ assertThrows(NullPointerException.class, () -> {
+ try (
+ Writer writer = new FileWriterWithEncoding((String) null, defaultEncoding);
+ ){ }
+ });
assertFalse(file1.exists());
}
