Add support for AssertJ
Add AssertJ as a managed dependency and also include it in spring-boot-starter-test. Also provide a simple adapter class to allow Hamcrest matchers to be used as AssertJ Conditions. Fixes gh-5048
This commit is contained in:
parent
516afcd2ca
commit
8b4d801dd6
|
@ -47,6 +47,7 @@
|
|||
<antlr2.version>2.7.7</antlr2.version>
|
||||
<artemis.version>1.1.0</artemis.version>
|
||||
<aspectj.version>1.8.8</aspectj.version>
|
||||
<assertj.version>2.3.0</assertj.version>
|
||||
<atomikos.version>3.9.3</atomikos.version>
|
||||
<bitronix.version>2.1.4</bitronix.version>
|
||||
<cassandra-driver.version>2.1.9</cassandra-driver.version>
|
||||
|
@ -1173,6 +1174,11 @@
|
|||
<artifactId>aspectjweaver</artifactId>
|
||||
<version>${aspectj.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.assertj</groupId>
|
||||
<artifactId>assertj-core</artifactId>
|
||||
<version>${assertj.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.codehaus.btm</groupId>
|
||||
<artifactId>btm</artifactId>
|
||||
|
|
|
@ -125,6 +125,11 @@
|
|||
<artifactId>commons-pool2</artifactId>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.assertj</groupId>
|
||||
<artifactId>assertj-core</artifactId>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.dropwizard.metrics</groupId>
|
||||
<artifactId>metrics-core</artifactId>
|
||||
|
|
|
@ -211,6 +211,11 @@
|
|||
<artifactId>junit</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.assertj</groupId>
|
||||
<artifactId>assertj-core</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.mockito</groupId>
|
||||
<artifactId>mockito-core</artifactId>
|
||||
|
|
|
@ -22,6 +22,10 @@
|
|||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.assertj</groupId>
|
||||
<artifactId>assertj-core</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.mockito</groupId>
|
||||
<artifactId>mockito-core</artifactId>
|
||||
|
|
|
@ -124,6 +124,11 @@
|
|||
<artifactId>velocity-tools</artifactId>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.assertj</groupId>
|
||||
<artifactId>assertj-core</artifactId>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.codehaus.btm</groupId>
|
||||
<artifactId>btm</artifactId>
|
||||
|
|
|
@ -0,0 +1,67 @@
|
|||
/*
|
||||
* Copyright 2012-2015 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.boot.test.assertj;
|
||||
|
||||
import org.assertj.core.api.Condition;
|
||||
import org.hamcrest.Matcher;
|
||||
import org.hamcrest.StringDescription;
|
||||
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
/**
|
||||
* Adapter class allowing a Hamcrest {@link Matcher} to be used as an AssertJ
|
||||
* {@link Condition}.
|
||||
* <p>
|
||||
* Usually used with the {@code is} method of {@code assertThat}, for example:
|
||||
*
|
||||
* <pre class="code">
|
||||
* assertThat("1234").is(Matched.when(startsWith("12")));
|
||||
* </pre>
|
||||
*
|
||||
* @param <T> The type of object that the condition accepts
|
||||
* @author Phillip Webb
|
||||
* @since 1.4
|
||||
*/
|
||||
public final class Matched<T> extends Condition<T> {
|
||||
|
||||
private final Matcher<? extends T> matcher;
|
||||
|
||||
private Matched(Matcher<? extends T> matcher) {
|
||||
Assert.notNull(matcher, "Matcher must not be null");
|
||||
this.matcher = matcher;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean matches(final T value) {
|
||||
if (this.matcher.matches(value)) {
|
||||
return true;
|
||||
}
|
||||
StringDescription description = new StringDescription();
|
||||
this.matcher.describeTo(description);
|
||||
describedAs(description.toString());
|
||||
return false;
|
||||
}
|
||||
|
||||
public static <T> Condition<T> when(Matcher<? extends T> matcher) {
|
||||
return by(matcher);
|
||||
}
|
||||
|
||||
public static <T> Condition<T> by(Matcher<? extends T> matcher) {
|
||||
return new Matched<T>(matcher);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,60 @@
|
|||
/*
|
||||
* Copyright 2012-2015 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.boot.test.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.hamcrest.Matchers.startsWith;
|
||||
|
||||
/**
|
||||
* Tests for {@link Matched}.
|
||||
*
|
||||
* @author Phillip Webb
|
||||
*/
|
||||
public class MatchedTests {
|
||||
|
||||
@Rule
|
||||
public ExpectedException thrown = ExpectedException.none();
|
||||
|
||||
@Test
|
||||
public void byMatcherMatches() {
|
||||
assertThat("1234").is(Matched.by(startsWith("12")));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void byMatcherDoesNotMatch() {
|
||||
this.thrown.expect(AssertionError.class);
|
||||
this.thrown.expectMessage("a string starting with \"23\"");
|
||||
assertThat("1234").is(Matched.by(startsWith("23")));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenMatcherMatches() {
|
||||
assertThat("1234").is(Matched.when(startsWith("12")));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenMatcherDoesNotMatch() {
|
||||
this.thrown.expect(AssertionError.class);
|
||||
this.thrown.expectMessage("a string starting with \"23\"");
|
||||
assertThat("1234").is(Matched.when(startsWith("23")));
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue