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

@@ -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,13 @@
package org.springframework.boot.cli;
import java.util.concurrent.ExecutionException;
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;
/**
* Integration tests to exercise and reproduce specific issues.
@@ -34,9 +36,6 @@ public class ReproIntegrationTests {
@Rule
public CliTester cli = new CliTester("src/test/resources/repro-samples/");
@Rule
public ExpectedException thrown = ExpectedException.none();
@Test
public void grabAntBuilder() throws Exception {
this.cli.run("grab-ant-builder.groovy");
@@ -57,9 +56,9 @@ public class ReproIntegrationTests {
@Test
public void jarFileExtensionNeeded() throws Exception {
this.thrown.expect(IllegalStateException.class);
this.thrown.expectMessage("is not a JAR file");
this.cli.jar("secure.groovy", "data-jpa.groovy");
assertThatExceptionOfType(ExecutionException.class)
.isThrownBy(() -> this.cli.jar("secure.groovy", "data-jpa.groovy"))
.withMessageContaining("is not a JAR file");
}
}

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.
@@ -21,9 +21,7 @@ import java.util.Set;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
@@ -31,6 +29,7 @@ import org.springframework.boot.cli.command.core.HelpCommand;
import org.springframework.boot.cli.command.core.HintCommand;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.mockito.BDDMockito.given;
import static org.mockito.BDDMockito.willThrow;
import static org.mockito.Mockito.verify;
@@ -43,9 +42,6 @@ import static org.mockito.Mockito.verify;
*/
public class CommandRunnerTests {
@Rule
public ExpectedException thrown = ExpectedException.none();
private CommandRunner commandRunner;
@Mock
@@ -101,8 +97,8 @@ public class CommandRunnerTests {
@Test
public void runWithoutArguments() throws Exception {
this.thrown.expect(NoArgumentsException.class);
this.commandRunner.run();
assertThatExceptionOfType(NoArgumentsException.class)
.isThrownBy(this.commandRunner::run);
}
@Test
@@ -113,8 +109,8 @@ public class CommandRunnerTests {
@Test
public void missingCommand() throws Exception {
this.thrown.expect(NoSuchCommandException.class);
this.commandRunner.run("missing");
assertThatExceptionOfType(NoSuchCommandException.class)
.isThrownBy(() -> this.commandRunner.run("missing"));
}
@Test
@@ -188,14 +184,14 @@ public class CommandRunnerTests {
@Test
public void helpNoCommand() throws Exception {
this.thrown.expect(NoHelpCommandArgumentsException.class);
this.commandRunner.run("help");
assertThatExceptionOfType(NoHelpCommandArgumentsException.class)
.isThrownBy(() -> this.commandRunner.run("help"));
}
@Test
public void helpUnknownCommand() throws Exception {
this.thrown.expect(NoSuchCommandException.class);
this.commandRunner.run("help", "missing");
assertThatExceptionOfType(NoSuchCommandException.class)
.isThrownBy(() -> this.commandRunner.run("help", "missing"));
}
private enum Call {

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.cli.command.init;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
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.mockito.ArgumentMatchers.isA;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;
@@ -34,9 +33,6 @@ import static org.mockito.Mockito.mock;
*/
public class InitializrServiceTests extends AbstractHttpClientMockTests {
@Rule
public final ExpectedException thrown = ExpectedException.none();
private final InitializrService invoker = new InitializrService(this.http);
@Test
@@ -81,18 +77,18 @@ public class InitializrServiceTests extends AbstractHttpClientMockTests {
mockProjectGenerationError(400, jsonMessage);
ProjectGenerationRequest request = new ProjectGenerationRequest();
request.getDependencies().add("foo:bar");
this.thrown.expect(ReportableException.class);
this.thrown.expectMessage(jsonMessage);
this.invoker.generate(request);
assertThatExceptionOfType(ReportableException.class)
.isThrownBy(() -> this.invoker.generate(request))
.withMessageContaining(jsonMessage);
}
@Test
public void generateProjectBadRequestNoExtraMessage() throws Exception {
mockProjectGenerationError(400, null);
ProjectGenerationRequest request = new ProjectGenerationRequest();
this.thrown.expect(ReportableException.class);
this.thrown.expectMessage("unexpected 400 error");
this.invoker.generate(request);
assertThatExceptionOfType(ReportableException.class)
.isThrownBy(() -> this.invoker.generate(request))
.withMessageContaining("unexpected 400 error");
}
@Test
@@ -102,9 +98,9 @@ public class InitializrServiceTests extends AbstractHttpClientMockTests {
mockStatus(response, 500);
given(this.http.execute(isA(HttpGet.class))).willReturn(response);
ProjectGenerationRequest request = new ProjectGenerationRequest();
this.thrown.expect(ReportableException.class);
this.thrown.expectMessage("No content received from server");
this.invoker.generate(request);
assertThatExceptionOfType(ReportableException.class)
.isThrownBy(() -> this.invoker.generate(request))
.withMessageContaining("No content received from server");
}
@Test
@@ -112,9 +108,9 @@ public class InitializrServiceTests extends AbstractHttpClientMockTests {
String jsonMessage = "whatever error on the server";
mockMetadataGetError(500, jsonMessage);
ProjectGenerationRequest request = new ProjectGenerationRequest();
this.thrown.expect(ReportableException.class);
this.thrown.expectMessage(jsonMessage);
this.invoker.generate(request);
assertThatExceptionOfType(ReportableException.class)
.isThrownBy(() -> this.invoker.generate(request))
.withMessageContaining(jsonMessage);
}
@Test
@@ -124,9 +120,9 @@ public class InitializrServiceTests extends AbstractHttpClientMockTests {
mockStatus(response, 200);
given(this.http.execute(isA(HttpGet.class))).willReturn(response);
ProjectGenerationRequest request = new ProjectGenerationRequest();
this.thrown.expect(ReportableException.class);
this.thrown.expectMessage("Invalid content received from server");
this.invoker.generate(request);
assertThatExceptionOfType(ReportableException.class)
.isThrownBy(() -> this.invoker.generate(request))
.withMessageContaining("Invalid content received from server");
}
@Test
@@ -135,9 +131,9 @@ public class InitializrServiceTests extends AbstractHttpClientMockTests {
mockStatus(response, 500);
given(this.http.execute(isA(HttpGet.class))).willReturn(response);
ProjectGenerationRequest request = new ProjectGenerationRequest();
this.thrown.expect(ReportableException.class);
this.thrown.expectMessage("No content received from server");
this.invoker.generate(request);
assertThatExceptionOfType(ReportableException.class)
.isThrownBy(() -> this.invoker.generate(request))
.withMessageContaining("No content received from server");
}
private ProjectGenerationResponse generateProject(ProjectGenerationRequest request,

View File

@@ -25,15 +25,14 @@ import java.util.Map;
import org.json.JSONException;
import org.json.JSONObject;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.util.StreamUtils;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
/**
* Tests for {@link ProjectGenerationRequest}.
@@ -45,9 +44,6 @@ public class ProjectGenerationRequestTests {
public static final Map<String, String> EMPTY_TAGS = Collections.emptyMap();
@Rule
public final ExpectedException thrown = ExpectedException.none();
private final ProjectGenerationRequest request = new ProjectGenerationRequest();
@Test
@@ -172,19 +168,19 @@ public class ProjectGenerationRequestTests {
public void buildNoMatch() throws Exception {
InitializrServiceMetadata metadata = readMetadata();
setBuildAndFormat("does-not-exist", null);
this.thrown.expect(ReportableException.class);
this.thrown.expectMessage("does-not-exist");
this.request.generateUrl(metadata);
assertThatExceptionOfType(ReportableException.class)
.isThrownBy(() -> this.request.generateUrl(metadata))
.withMessageContaining("does-not-exist");
}
@Test
public void buildMultipleMatch() throws Exception {
InitializrServiceMetadata metadata = readMetadata("types-conflict");
setBuildAndFormat("gradle", null);
this.thrown.expect(ReportableException.class);
this.thrown.expectMessage("gradle-project");
this.thrown.expectMessage("gradle-project-2");
this.request.generateUrl(metadata);
assertThatExceptionOfType(ReportableException.class)
.isThrownBy(() -> this.request.generateUrl(metadata))
.withMessageContaining("gradle-project")
.withMessageContaining("gradle-project-2");
}
@Test
@@ -207,15 +203,16 @@ public class ProjectGenerationRequestTests {
@Test
public void invalidType() {
this.request.setType("does-not-exist");
this.thrown.expect(ReportableException.class);
this.request.generateUrl(createDefaultMetadata());
assertThatExceptionOfType(ReportableException.class)
.isThrownBy(() -> this.request.generateUrl(createDefaultMetadata()));
}
@Test
public void noTypeAndNoDefault() throws Exception {
this.thrown.expect(ReportableException.class);
this.thrown.expectMessage("no default is defined");
this.request.generateUrl(readMetadata("types-conflict"));
assertThatExceptionOfType(ReportableException.class)
.isThrownBy(
() -> this.request.generateUrl(readMetadata("types-conflict")))
.withMessageContaining("no default is defined");
}
private static URI createUrl(String actionAndParam) {

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,9 @@ package org.springframework.boot.cli.command.run;
import java.util.logging.Level;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import static org.hamcrest.Matchers.equalTo;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;
@@ -33,19 +31,16 @@ import static org.mockito.Mockito.mock;
*/
public class SpringApplicationRunnerTests {
@Rule
public ExpectedException thrown = ExpectedException.none();
@Test
public void exceptionMessageWhenSourcesContainsNoClasses() throws Exception {
SpringApplicationRunnerConfiguration configuration = mock(
SpringApplicationRunnerConfiguration.class);
given(configuration.getClasspath()).willReturn(new String[] { "foo", "bar" });
given(configuration.getLogLevel()).willReturn(Level.INFO);
this.thrown.expect(RuntimeException.class);
this.thrown.expectMessage(equalTo("No classes found in '[foo, bar]'"));
new SpringApplicationRunner(configuration, new String[] { "foo", "bar" })
.compileAndRun();
assertThatExceptionOfType(RuntimeException.class)
.isThrownBy(() -> new SpringApplicationRunner(configuration,
new String[] { "foo", "bar" }).compileAndRun())
.withMessage("No classes found in '[foo, bar]'");
}
}

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.
@@ -17,11 +17,10 @@
package org.springframework.boot.cli.compiler;
import org.junit.Before;
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 ExtendedGroovyClassLoader}.
@@ -30,9 +29,6 @@ import static org.assertj.core.api.Assertions.assertThat;
*/
public class ExtendedGroovyClassLoaderTests {
@Rule
public ExpectedException thrown = ExpectedException.none();
private ClassLoader contextClassLoader;
private ExtendedGroovyClassLoader defaultScopeGroovyClassLoader;
@@ -54,9 +50,9 @@ public class ExtendedGroovyClassLoaderTests {
@Test
public void filtersNonGroovy() throws Exception {
this.contextClassLoader.loadClass("org.springframework.util.StringUtils");
this.thrown.expect(ClassNotFoundException.class);
this.defaultScopeGroovyClassLoader
.loadClass("org.springframework.util.StringUtils");
assertThatExceptionOfType(ClassNotFoundException.class)
.isThrownBy(() -> this.defaultScopeGroovyClassLoader
.loadClass("org.springframework.util.StringUtils"));
}
@Test