Migrate from ExpectedException rule to AssertJ
Replace ExpectedException JUnit rules with AssertJ exception assertions. Closes gh-14336
This commit is contained in:
@@ -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\"");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user