Migrate from ExpectedException rule to AssertJ

Replace ExpectedException JUnit rules with AssertJ exception
assertions.

Closes gh-14336
This commit is contained in:
Phillip Webb
2018-10-01 11:18:16 -07:00
parent 42cb0effc4
commit d76bba5e6f
273 changed files with 2752 additions and 3624 deletions

View File

@@ -32,7 +32,6 @@ import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.rules.TemporaryFolder;
import org.springframework.boot.loader.archive.Archive;
@@ -43,6 +42,7 @@ import org.springframework.core.io.FileSystemResource;
import org.springframework.test.util.ReflectionTestUtils;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
/**
* Tests for {@link PropertiesLauncher}.
@@ -55,9 +55,6 @@ public class PropertiesLauncherTests {
@Rule
public OutputCapture output = new OutputCapture();
@Rule
public ExpectedException expected = ExpectedException.none();
@Rule
public TemporaryFolder temporaryFolder = new TemporaryFolder();
@@ -102,10 +99,9 @@ public class PropertiesLauncherTests {
@Test
public void testNonExistentHome() {
System.setProperty("loader.home", "src/test/resources/nonexistent");
this.expected.expectMessage("Invalid source folder");
PropertiesLauncher launcher = new PropertiesLauncher();
assertThat(launcher.getHomeDirectory())
.isNotEqualTo(new File(System.getProperty("loader.home")));
assertThatIllegalStateException().isThrownBy(PropertiesLauncher::new)
.withMessageContaining("Invalid source folder")
.withCauseInstanceOf(IllegalArgumentException.class);
}
@Test

View File

@@ -31,7 +31,6 @@ import java.util.zip.ZipEntry;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.rules.TemporaryFolder;
import org.springframework.boot.loader.TestJarCreator;
@@ -39,7 +38,7 @@ import org.springframework.boot.loader.archive.Archive.Entry;
import org.springframework.util.FileCopyUtils;
import static org.assertj.core.api.Assertions.assertThat;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
/**
* Tests for {@link JarFileArchive}.
@@ -52,9 +51,6 @@ public class JarFileArchiveTests {
@Rule
public TemporaryFolder temporaryFolder = new TemporaryFolder();
@Rule
public ExpectedException thrown = ExpectedException.none();
private File rootJarFile;
private JarFileArchive archive;
@@ -135,8 +131,8 @@ public class JarFileArchiveTests {
public void zip64ArchivesAreHandledGracefully() throws IOException {
File file = this.temporaryFolder.newFile("test.jar");
FileCopyUtils.copy(writeZip64Jar(), file);
this.thrown.expectMessage(equalTo("Zip64 archives are not supported"));
new JarFileArchive(file);
assertThatIllegalStateException().isThrownBy(() -> new JarFileArchive(file))
.withMessageContaining("Zip64 archives are not supported");
}
@Test
@@ -156,10 +152,11 @@ public class JarFileArchiveTests {
output.closeEntry();
output.close();
JarFileArchive jarFileArchive = new JarFileArchive(file);
this.thrown.expectMessage(
equalTo("Failed to get nested archive for entry nested/zip64.jar"));
jarFileArchive
.getNestedArchive(getEntriesMap(jarFileArchive).get("nested/zip64.jar"));
assertThatIllegalStateException()
.isThrownBy(() -> jarFileArchive.getNestedArchive(
getEntriesMap(jarFileArchive).get("nested/zip64.jar")))
.withMessageContaining(
"Failed to get nested archive for entry nested/zip64.jar");
}
private byte[] writeZip64Jar() throws IOException {

View File

@@ -31,10 +31,12 @@ import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.rules.TemporaryFolder;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
import static org.assertj.core.api.Assertions.assertThatNullPointerException;
/**
* Tests for {@link RandomAccessDataFile}.
@@ -53,9 +55,6 @@ public class RandomAccessDataFileTests {
}
}
@Rule
public ExpectedException thrown = ExpectedException.none();
@Rule
public TemporaryFolder temporaryFolder = new TemporaryFolder();
@@ -83,18 +82,17 @@ public class RandomAccessDataFileTests {
@Test
public void fileNotNull() {
this.thrown.expect(IllegalArgumentException.class);
this.thrown.expectMessage("File must not be null");
new RandomAccessDataFile(null);
assertThatIllegalArgumentException()
.isThrownBy(() -> new RandomAccessDataFile(null))
.withMessageContaining("File must not be null");
}
@Test
public void fileExists() {
File file = new File("/does/not/exist");
this.thrown.expect(IllegalArgumentException.class);
this.thrown.expectMessage(
String.format("File %s must exist", file.getAbsolutePath()));
new RandomAccessDataFile(file);
assertThatIllegalArgumentException()
.isThrownBy(() -> new RandomAccessDataFile(file)).withMessageContaining(
String.format("File %s must exist", file.getAbsolutePath()));
}
@Test
@@ -105,31 +103,31 @@ public class RandomAccessDataFileTests {
@Test
public void readWhenOffsetIsBeyondEOFShouldThrowException() throws Exception {
this.thrown.expect(IndexOutOfBoundsException.class);
this.file.read(257, 0);
assertThatExceptionOfType(IndexOutOfBoundsException.class)
.isThrownBy(() -> this.file.read(257, 0));
}
@Test
public void readWhenOffsetIsBeyondEndOfSubsectionShouldThrowException()
throws Exception {
this.thrown.expect(IndexOutOfBoundsException.class);
RandomAccessData subsection = this.file.getSubsection(0, 10);
subsection.read(11, 0);
assertThatExceptionOfType(IndexOutOfBoundsException.class)
.isThrownBy(() -> subsection.read(11, 0));
}
@Test
public void readWhenOffsetPlusLengthGreaterThanEOFShouldThrowException()
throws Exception {
this.thrown.expect(EOFException.class);
this.file.read(256, 1);
assertThatExceptionOfType(EOFException.class)
.isThrownBy(() -> this.file.read(256, 1));
}
@Test
public void readWhenOffsetPlusLengthGreaterThanEndOfSubsectionShouldThrowException()
throws Exception {
this.thrown.expect(EOFException.class);
RandomAccessData subsection = this.file.getSubsection(0, 10);
subsection.read(10, 1);
assertThatExceptionOfType(EOFException.class)
.isThrownBy(() -> subsection.read(10, 1));
}
@Test
@@ -141,16 +139,15 @@ public class RandomAccessDataFileTests {
@Test
public void inputStreamReadNullBytes() throws Exception {
this.thrown.expect(NullPointerException.class);
this.thrown.expectMessage("Bytes must not be null");
this.inputStream.read(null);
assertThatNullPointerException().isThrownBy(() -> this.inputStream.read(null))
.withMessage("Bytes must not be null");
}
@Test
public void inputStreamReadNullBytesWithOffset() throws Exception {
this.thrown.expect(NullPointerException.class);
this.thrown.expectMessage("Bytes must not be null");
this.inputStream.read(null, 0, 1);
assertThatNullPointerException()
.isThrownBy(() -> this.inputStream.read(null, 0, 1))
.withMessage("Bytes must not be null");
}
@Test
@@ -218,14 +215,14 @@ public class RandomAccessDataFileTests {
@Test
public void subsectionNegativeOffset() {
this.thrown.expect(IndexOutOfBoundsException.class);
this.file.getSubsection(-1, 1);
assertThatExceptionOfType(IndexOutOfBoundsException.class)
.isThrownBy(() -> this.file.getSubsection(-1, 1));
}
@Test
public void subsectionNegativeLength() {
this.thrown.expect(IndexOutOfBoundsException.class);
this.file.getSubsection(0, -1);
assertThatExceptionOfType(IndexOutOfBoundsException.class)
.isThrownBy(() -> this.file.getSubsection(0, -1));
}
@Test
@@ -237,15 +234,15 @@ public class RandomAccessDataFileTests {
@Test
public void subsectionTooBig() {
this.file.getSubsection(0, 256);
this.thrown.expect(IndexOutOfBoundsException.class);
this.file.getSubsection(0, 257);
assertThatExceptionOfType(IndexOutOfBoundsException.class)
.isThrownBy(() -> this.file.getSubsection(0, 257));
}
@Test
public void subsectionTooBigWithOffset() {
this.file.getSubsection(1, 255);
this.thrown.expect(IndexOutOfBoundsException.class);
this.file.getSubsection(1, 256);
assertThatExceptionOfType(IndexOutOfBoundsException.class)
.isThrownBy(() -> this.file.getSubsection(1, 256));
}
@Test

View File

@@ -16,11 +16,10 @@
package org.springframework.boot.loader.jar;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
/**
* Tests for {@link AsciiBytes}.
@@ -32,9 +31,6 @@ public class AsciiBytesTests {
private static final char NO_SUFFIX = 0;
@Rule
public ExpectedException thrown = ExpectedException.none();
@Test
public void createFromBytes() {
AsciiBytes bytes = new AsciiBytes(new byte[] { 65, 66 });
@@ -93,8 +89,8 @@ public class AsciiBytesTests {
assertThat(abcd.substring(2).toString()).isEqualTo("CD");
assertThat(abcd.substring(3).toString()).isEqualTo("D");
assertThat(abcd.substring(4).toString()).isEqualTo("");
this.thrown.expect(IndexOutOfBoundsException.class);
abcd.substring(5);
assertThatExceptionOfType(IndexOutOfBoundsException.class)
.isThrownBy(() -> abcd.substring(5));
}
@Test
@@ -104,8 +100,8 @@ public class AsciiBytesTests {
assertThat(abcd.substring(1, 3).toString()).isEqualTo("BC");
assertThat(abcd.substring(3, 4).toString()).isEqualTo("D");
assertThat(abcd.substring(3, 3).toString()).isEqualTo("");
this.thrown.expect(IndexOutOfBoundsException.class);
abcd.substring(3, 5);
assertThatExceptionOfType(IndexOutOfBoundsException.class)
.isThrownBy(() -> abcd.substring(3, 5));
}
@Test

View File

@@ -21,7 +21,6 @@ import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FilePermission;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLClassLoader;
@@ -36,7 +35,6 @@ import java.util.zip.ZipFile;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.rules.TemporaryFolder;
import org.springframework.boot.loader.TestJarCreator;
@@ -45,6 +43,8 @@ import org.springframework.util.FileCopyUtils;
import org.springframework.util.StreamUtils;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.assertj.core.api.Assertions.assertThatIOException;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;
@@ -61,9 +61,6 @@ public class JarFileTests {
private static final String HANDLERS_PACKAGE = "org.springframework.boot.loader";
@Rule
public ExpectedException thrown = ExpectedException.none();
@Rule
public TemporaryFolder temporaryFolder = new TemporaryFolder();
@@ -231,16 +228,15 @@ public class JarFileTests {
URL url = new URL(this.jarFile.getUrl(), "missing.dat");
assertThat(url.toString())
.isEqualTo("jar:" + this.rootJarFile.toURI() + "!/missing.dat");
this.thrown.expect(FileNotFoundException.class);
((JarURLConnection) url.openConnection()).getJarEntry();
assertThatExceptionOfType(FileNotFoundException.class)
.isThrownBy(((JarURLConnection) url.openConnection())::getJarEntry);
}
@Test
public void getUrlStream() throws Exception {
URL url = this.jarFile.getUrl();
url.openConnection();
this.thrown.expect(IOException.class);
url.openStream();
assertThatIOException().isThrownBy(url::openStream);
}
@Test
@@ -432,8 +428,8 @@ public class JarFileTests {
.getNestedJarFile(this.jarFile.getEntry("nested.jar"));
URL nestedUrl = nestedJarFile.getUrl();
URL url = new URL(nestedUrl, nestedJarFile.getUrl() + "missing.jar!/3.dat");
this.thrown.expect(FileNotFoundException.class);
url.openConnection().getInputStream();
assertThatExceptionOfType(FileNotFoundException.class)
.isThrownBy(url.openConnection()::getInputStream);
}
@Test
@@ -494,9 +490,9 @@ public class JarFileTests {
URL context = nested.getUrl();
new URL(context, "jar:" + this.rootJarFile.toURI() + "!/nested.jar!/3.dat")
.openConnection().getInputStream().close();
this.thrown.expect(FileNotFoundException.class);
new URL(context, "jar:" + this.rootJarFile.toURI() + "!/no.dat")
.openConnection().getInputStream();
assertThatExceptionOfType(FileNotFoundException.class).isThrownBy(
new URL(context, "jar:" + this.rootJarFile.toURI() + "!/no.dat")
.openConnection()::getInputStream);
}
finally {
JarURLConnection.setUseFastExceptions(false);

View File

@@ -24,13 +24,13 @@ import java.net.URL;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.rules.TemporaryFolder;
import org.springframework.boot.loader.TestJarCreator;
import org.springframework.boot.loader.jar.JarURLConnection.JarEntryName;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
/**
* Tests for {@link JarURLConnection}.
@@ -44,9 +44,6 @@ public class JarURLConnectionTests {
@Rule
public TemporaryFolder temporaryFolder = new TemporaryFolder(new File("target"));
@Rule
public ExpectedException thrown = ExpectedException.none();
private File rootJarFile;
private JarFile jarFile;
@@ -160,8 +157,8 @@ public class JarURLConnectionTests {
URL url = new URL("jar:file:" + getAbsolutePath() + "!/w.jar!/3.dat");
JarFile nested = this.jarFile
.getNestedJarFile(this.jarFile.getEntry("nested.jar"));
this.thrown.expect(FileNotFoundException.class);
JarURLConnection.get(url, nested).getInputStream();
assertThatExceptionOfType(FileNotFoundException.class)
.isThrownBy(JarURLConnection.get(url, nested)::getInputStream);
}
@Test

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2017 the original author or authors.
* Copyright 2012-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -16,11 +16,11 @@
package org.springframework.boot.loader.jar;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.assertj.core.api.Assertions.assertThatNullPointerException;
/**
* Tests for {@link StringSequence}.
@@ -29,33 +29,28 @@ import static org.assertj.core.api.Assertions.assertThat;
*/
public class StringSequenceTests {
@Rule
public ExpectedException thrown = ExpectedException.none();
@Test
public void createWhenSourceIsNullShouldThrowException() {
this.thrown.expect(NullPointerException.class);
this.thrown.expectMessage("Source must not be null");
new StringSequence(null);
assertThatNullPointerException().isThrownBy(() -> new StringSequence(null))
.withMessage("Source must not be null");
}
@Test
public void createWithIndexWhenSourceIsNullShouldThrowException() {
this.thrown.expect(NullPointerException.class);
this.thrown.expectMessage("Source must not be null");
new StringSequence(null, 0, 0);
assertThatNullPointerException().isThrownBy(() -> new StringSequence(null, 0, 0))
.withMessage("Source must not be null");
}
@Test
public void createWhenStartIsLessThanZeroShouldThrowException() {
this.thrown.expect(StringIndexOutOfBoundsException.class);
new StringSequence("x", -1, 0);
assertThatExceptionOfType(StringIndexOutOfBoundsException.class)
.isThrownBy(() -> new StringSequence("x", -1, 0));
}
@Test
public void createWhenEndIsGreaterThanLengthShouldThrowException() {
this.thrown.expect(StringIndexOutOfBoundsException.class);
new StringSequence("x", 0, 2);
assertThatExceptionOfType(StringIndexOutOfBoundsException.class)
.isThrownBy(() -> new StringSequence("x", 0, 2));
}
@Test
@@ -88,8 +83,8 @@ public class StringSequenceTests {
StringSequence sequence = new StringSequence("abcde").subSequence(1, 4);
assertThat(sequence.toString()).isEqualTo("bcd");
assertThat(sequence.subSequence(2, 3).toString()).isEqualTo("d");
this.thrown.expect(IndexOutOfBoundsException.class);
sequence.subSequence(3, 4);
assertThatExceptionOfType(IndexOutOfBoundsException.class)
.isThrownBy(() -> sequence.subSequence(3, 4));
}
@Test
@@ -97,8 +92,8 @@ public class StringSequenceTests {
StringSequence sequence = new StringSequence("abcde").subSequence(1, 4);
assertThat(sequence.toString()).isEqualTo("bcd");
assertThat(sequence.subSequence(2, 3).toString()).isEqualTo("d");
this.thrown.expect(IndexOutOfBoundsException.class);
sequence.subSequence(4, 3);
assertThatExceptionOfType(IndexOutOfBoundsException.class)
.isThrownBy(() -> sequence.subSequence(4, 3));
}
@Test