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

@@ -19,9 +19,6 @@ package org.springframework.boot.configurationmetadata;
import java.io.IOException;
import java.io.InputStream;
import org.junit.Rule;
import org.junit.rules.ExpectedException;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
@@ -34,9 +31,6 @@ import static org.assertj.core.api.Assertions.assertThat;
*/
public abstract class AbstractConfigurationMetadataTests {
@Rule
public final ExpectedException thrown = ExpectedException.none();
protected void assertSource(ConfigurationMetadataSource actual, String groupId,
String type, String sourceType) {
assertThat(actual).isNotNull();

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.
@@ -23,6 +23,7 @@ import java.util.Map;
import org.junit.Test;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
/**
* Tests for {@link ConfigurationMetadataRepository}.
@@ -34,8 +35,9 @@ public class ConfigurationMetadataRepositoryJsonBuilderTests
@Test
public void nullResource() throws IOException {
this.thrown.expect(IllegalArgumentException.class);
ConfigurationMetadataRepositoryJsonBuilder.create().withJsonResource(null);
assertThatIllegalArgumentException()
.isThrownBy(() -> ConfigurationMetadataRepositoryJsonBuilder.create()
.withJsonResource(null));
}
@Test

View File

@@ -21,11 +21,11 @@ import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.List;
import org.hamcrest.CoreMatchers;
import org.json.JSONException;
import org.junit.Test;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
/**
* Tests for {@link JsonReader}.
@@ -47,8 +47,8 @@ public class JsonReaderTests extends AbstractConfigurationMetadataTests {
@Test
public void invalidMetadata() throws IOException {
this.thrown.expectCause(CoreMatchers.instanceOf(JSONException.class));
readFor("invalid");
assertThatIllegalStateException().isThrownBy(() -> readFor("invalid"))
.withCauseInstanceOf(JSONException.class);
}
@Test

View File

@@ -28,7 +28,6 @@ import java.util.stream.Collectors;
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.configurationprocessor.json.JSONArray;
@@ -95,6 +94,7 @@ import org.springframework.boot.testsupport.compiler.TestCompiler;
import org.springframework.util.FileCopyUtils;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
/**
* Tests for {@link ConfigurationMetadataAnnotationProcessor}.
@@ -110,9 +110,6 @@ public class ConfigurationMetadataAnnotationProcessorTests {
@Rule
public TemporaryFolder temporaryFolder = new TemporaryFolder();
@Rule
public ExpectedException thrown = ExpectedException.none();
private TestCompiler compiler;
@Before
@@ -502,9 +499,9 @@ public class ConfigurationMetadataAnnotationProcessorTests {
@Test
public void invalidDoubleRegistration() {
this.thrown.expect(IllegalStateException.class);
this.thrown.expectMessage("Compilation failed");
compile(InvalidDoubleRegistrationProperties.class);
assertThatIllegalStateException()
.isThrownBy(() -> compile(InvalidDoubleRegistrationProperties.class))
.withMessageContaining("Compilation failed");
}
@Test
@@ -904,10 +901,9 @@ public class ConfigurationMetadataAnnotationProcessorTests {
public void mergeOfInvalidAdditionalMetadata() throws IOException {
File additionalMetadataFile = createAdditionalMetadataFile();
FileCopyUtils.copy("Hello World", new FileWriter(additionalMetadataFile));
this.thrown.expect(IllegalStateException.class);
this.thrown.expectMessage("Compilation failed");
compile(SimpleProperties.class);
assertThatIllegalStateException()
.isThrownBy(() -> compile(SimpleProperties.class))
.withMessage("Compilation failed");
}
@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.
@@ -18,11 +18,10 @@ package org.springframework.boot.loader.tools;
import java.io.File;
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.assertThatIllegalStateException;
/**
* Tests for {@link Layouts}.
@@ -32,9 +31,6 @@ import static org.assertj.core.api.Assertions.assertThat;
*/
public class LayoutsTests {
@Rule
public ExpectedException thrown = ExpectedException.none();
@Test
public void jarFile() {
assertThat(Layouts.forFile(new File("test.jar"))).isInstanceOf(Layouts.Jar.class);
@@ -55,9 +51,9 @@ public class LayoutsTests {
@Test
public void unknownFile() {
this.thrown.expect(IllegalStateException.class);
this.thrown.expectMessage("Unable to deduce layout for 'test.txt'");
Layouts.forFile(new File("test.txt"));
assertThatIllegalStateException()
.isThrownBy(() -> Layouts.forFile(new File("test.txt")))
.withMessageContaining("Unable to deduce layout for 'test.txt'");
}
@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.
@@ -23,7 +23,6 @@ import java.util.List;
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.tools.MainClassFinder.MainClass;
@@ -33,6 +32,7 @@ import org.springframework.boot.loader.tools.sample.ClassWithMainMethod;
import org.springframework.boot.loader.tools.sample.ClassWithoutMainMethod;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
/**
* Tests for {@link MainClassFinder}.
@@ -44,9 +44,6 @@ public class MainClassFinderTests {
@Rule
public TemporaryFolder temporaryFolder = new TemporaryFolder();
@Rule
public ExpectedException thrown = ExpectedException.none();
private TestJarFile testJarFile;
@Before
@@ -83,10 +80,11 @@ public class MainClassFinderTests {
public void findSingleJarSearch() throws Exception {
this.testJarFile.addClass("a/B.class", ClassWithMainMethod.class);
this.testJarFile.addClass("a/b/c/E.class", ClassWithMainMethod.class);
this.thrown.expect(IllegalStateException.class);
this.thrown.expectMessage("Unable to find a single main class "
+ "from the following candidates [a.B, a.b.c.E]");
MainClassFinder.findSingleMainClass(this.testJarFile.getJarFile(), "");
assertThatIllegalStateException()
.isThrownBy(() -> MainClassFinder
.findSingleMainClass(this.testJarFile.getJarFile(), ""))
.withMessageContaining("Unable to find a single main class "
+ "from the following candidates [a.B, a.b.c.E]");
}
@Test
@@ -138,10 +136,11 @@ public class MainClassFinderTests {
public void findSingleFolderSearch() throws Exception {
this.testJarFile.addClass("a/B.class", ClassWithMainMethod.class);
this.testJarFile.addClass("a/b/c/E.class", ClassWithMainMethod.class);
this.thrown.expect(IllegalStateException.class);
this.thrown.expectMessage("Unable to find a single main class "
+ "from the following candidates [a.B, a.b.c.E]");
MainClassFinder.findSingleMainClass(this.testJarFile.getJarSource());
assertThatIllegalStateException()
.isThrownBy(() -> MainClassFinder
.findSingleMainClass(this.testJarFile.getJarSource()))
.withMessageContaining("Unable to find a single main class "
+ "from the following candidates [a.B, a.b.c.E]");
}
@Test

View File

@@ -40,7 +40,6 @@ import org.apache.commons.compress.archivers.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.zeroturnaround.zip.ZipUtil;
@@ -49,6 +48,8 @@ import org.springframework.boot.loader.tools.sample.ClassWithoutMainMethod;
import org.springframework.util.FileCopyUtils;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.BDDMockito.given;
@@ -81,9 +82,6 @@ public class RepackagerTests {
@Rule
public TemporaryFolder temporaryFolder = new TemporaryFolder();
@Rule
public ExpectedException thrown = ExpectedException.none();
private TestJarFile testJarFile;
@Before
@@ -93,20 +91,19 @@ public class RepackagerTests {
@Test
public void nullSource() {
this.thrown.expect(IllegalArgumentException.class);
new Repackager(null);
assertThatIllegalArgumentException().isThrownBy(() -> new Repackager(null));
}
@Test
public void missingSource() {
this.thrown.expect(IllegalArgumentException.class);
new Repackager(new File("missing"));
assertThatIllegalArgumentException()
.isThrownBy(() -> new Repackager(new File("missing")));
}
@Test
public void directorySource() {
this.thrown.expect(IllegalArgumentException.class);
new Repackager(this.temporaryFolder.getRoot());
assertThatIllegalArgumentException()
.isThrownBy(() -> new Repackager(this.temporaryFolder.getRoot()));
}
@Test
@@ -177,18 +174,18 @@ public class RepackagerTests {
this.testJarFile.addClass("a/b/D.class", ClassWithMainMethod.class);
File file = this.testJarFile.getFile();
Repackager repackager = new Repackager(file);
this.thrown.expect(IllegalStateException.class);
this.thrown.expectMessage("Unable to find a single main class "
+ "from the following candidates [a.b.C, a.b.D]");
repackager.repackage(NO_LIBRARIES);
assertThatIllegalStateException()
.isThrownBy(() -> repackager.repackage(NO_LIBRARIES))
.withMessageContaining("Unable to find a single main class "
+ "from the following candidates [a.b.C, a.b.D]");
}
@Test
public void noMainClass() throws Exception {
this.testJarFile.addClass("a/b/C.class", ClassWithoutMainMethod.class);
this.thrown.expect(IllegalStateException.class);
this.thrown.expectMessage("Unable to find main class");
new Repackager(this.testJarFile.getFile()).repackage(NO_LIBRARIES);
assertThatIllegalStateException().isThrownBy(
() -> new Repackager(this.testJarFile.getFile()).repackage(NO_LIBRARIES))
.withMessageContaining("Unable to find main class");
}
@Test
@@ -255,18 +252,18 @@ public class RepackagerTests {
public void nullDestination() throws Exception {
this.testJarFile.addClass("a/b/C.class", ClassWithMainMethod.class);
Repackager repackager = new Repackager(this.testJarFile.getFile());
this.thrown.expect(IllegalArgumentException.class);
this.thrown.expectMessage("Invalid destination");
repackager.repackage(null, NO_LIBRARIES);
assertThatIllegalArgumentException()
.isThrownBy(() -> repackager.repackage(null, NO_LIBRARIES))
.withMessageContaining("Invalid destination");
}
@Test
public void destinationIsDirectory() throws Exception {
this.testJarFile.addClass("a/b/C.class", ClassWithMainMethod.class);
Repackager repackager = new Repackager(this.testJarFile.getFile());
this.thrown.expect(IllegalArgumentException.class);
this.thrown.expectMessage("Invalid destination");
repackager.repackage(this.temporaryFolder.getRoot(), NO_LIBRARIES);
assertThatIllegalArgumentException().isThrownBy(
() -> repackager.repackage(this.temporaryFolder.getRoot(), NO_LIBRARIES))
.withMessageContaining("Invalid destination");
}
@Test
@@ -284,9 +281,9 @@ public class RepackagerTests {
this.testJarFile.addClass("a/b/C.class", ClassWithMainMethod.class);
File file = this.testJarFile.getFile();
Repackager repackager = new Repackager(file);
this.thrown.expect(IllegalArgumentException.class);
this.thrown.expectMessage("Libraries must not be null");
repackager.repackage(file, null);
assertThatIllegalArgumentException()
.isThrownBy(() -> repackager.repackage(file, null))
.withMessageContaining("Libraries must not be null");
}
@Test
@@ -327,12 +324,13 @@ public class RepackagerTests {
this.testJarFile.addClass("a/b/C.class", ClassWithMainMethod.class);
File file = this.testJarFile.getFile();
Repackager repackager = new Repackager(file);
this.thrown.expect(IllegalStateException.class);
this.thrown.expectMessage("Duplicate library");
repackager.repackage((callback) -> {
callback.library(new Library(libJarFile, LibraryScope.COMPILE, false));
callback.library(new Library(libJarFile, LibraryScope.COMPILE, false));
});
assertThatIllegalStateException()
.isThrownBy(() -> repackager.repackage((callback) -> {
callback.library(
new Library(libJarFile, LibraryScope.COMPILE, false));
callback.library(
new Library(libJarFile, LibraryScope.COMPILE, false));
})).withMessageContaining("Duplicate library");
}
@Test
@@ -421,9 +419,8 @@ public class RepackagerTests {
public void nullCustomLayout() throws Exception {
this.testJarFile.addClass("a/b/C.class", ClassWithoutMainMethod.class);
Repackager repackager = new Repackager(this.testJarFile.getFile());
this.thrown.expect(IllegalArgumentException.class);
this.thrown.expectMessage("Layout must not be null");
repackager.setLayout(null);
assertThatIllegalArgumentException().isThrownBy(() -> repackager.setLayout(null))
.withMessageContaining("Layout must not be null");
}
@Test

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

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,10 @@
package org.springframework.boot.testsupport.assertj;
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.hamcrest.Matchers.startsWith;
/**
@@ -30,9 +29,6 @@ import static org.hamcrest.Matchers.startsWith;
*/
public class MatchedTests {
@Rule
public ExpectedException thrown = ExpectedException.none();
@Test
public void byMatcherMatches() {
assertThat("1234").is(Matched.by(startsWith("12")));
@@ -40,9 +36,9 @@ public class MatchedTests {
@Test
public void byMatcherDoesNotMatch() {
this.thrown.expect(AssertionError.class);
this.thrown.expectMessage("a string starting with \"23\"");
assertThat("1234").is(Matched.by(startsWith("23")));
assertThatExceptionOfType(AssertionError.class)
.isThrownBy(() -> assertThat("1234").is(Matched.by(startsWith("23"))))
.withMessageContaining("a string starting with \"23\"");
}
@Test
@@ -52,9 +48,9 @@ public class MatchedTests {
@Test
public void whenMatcherDoesNotMatch() {
this.thrown.expect(AssertionError.class);
this.thrown.expectMessage("a string starting with \"23\"");
assertThat("1234").is(Matched.when(startsWith("23")));
assertThatExceptionOfType(AssertionError.class)
.isThrownBy(() -> assertThat("1234").is(Matched.when(startsWith("23"))))
.withMessageContaining("a string starting with \"23\"");
}
}

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,9 +16,8 @@
package org.springframework.boot.testsupport.runner.classpath;
import org.junit.Rule;
import org.hamcrest.Matcher;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.runner.RunWith;
import static org.assertj.core.api.Assertions.assertThat;
@@ -36,9 +35,6 @@ public class ModifiedClassPathRunnerExclusionsTests {
private static final String EXCLUDED_RESOURCE = "META-INF/services/"
+ "javax.validation.spi.ValidationProvider";
@Rule
public ExpectedException thrown = ExpectedException.none();
@Test
public void entriesAreFilteredFromTestClassClassLoader() {
assertThat(getClass().getClassLoader().getResource(EXCLUDED_RESOURCE)).isNull();
@@ -52,8 +48,8 @@ public class ModifiedClassPathRunnerExclusionsTests {
@Test
public void testsThatUseHamcrestWorkCorrectly() {
this.thrown.expect(isA(IllegalStateException.class));
throw new IllegalStateException();
Matcher<IllegalStateException> matcher = isA(IllegalStateException.class);
assertThat(matcher.matches(new IllegalStateException())).isTrue();
}
}