From 8b4d801dd6dd6ffc9a2366b5f70bab59cb45235a Mon Sep 17 00:00:00 2001 From: Phillip Webb Date: Sat, 6 Feb 2016 14:47:41 -0800 Subject: [PATCH] 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 --- spring-boot-dependencies/pom.xml | 6 ++ spring-boot-docs/pom.xml | 5 ++ spring-boot-parent/pom.xml | 5 ++ .../spring-boot-starter-test/pom.xml | 4 ++ spring-boot/pom.xml | 5 ++ .../boot/test/assertj/Matched.java | 67 +++++++++++++++++++ .../boot/test/assertj/MatchedTests.java | 60 +++++++++++++++++ 7 files changed, 152 insertions(+) mode change 100755 => 100644 spring-boot-parent/pom.xml create mode 100644 spring-boot/src/main/java/org/springframework/boot/test/assertj/Matched.java create mode 100644 spring-boot/src/test/java/org/springframework/boot/test/assertj/MatchedTests.java diff --git a/spring-boot-dependencies/pom.xml b/spring-boot-dependencies/pom.xml index 9532d9e3a3..01c66455d9 100644 --- a/spring-boot-dependencies/pom.xml +++ b/spring-boot-dependencies/pom.xml @@ -47,6 +47,7 @@ 2.7.7 1.1.0 1.8.8 + 2.3.0 3.9.3 2.1.4 2.1.9 @@ -1173,6 +1174,11 @@ aspectjweaver ${aspectj.version} + + org.assertj + assertj-core + ${assertj.version} + org.codehaus.btm btm diff --git a/spring-boot-docs/pom.xml b/spring-boot-docs/pom.xml index 30ea7fae87..15e4bbf4cf 100644 --- a/spring-boot-docs/pom.xml +++ b/spring-boot-docs/pom.xml @@ -125,6 +125,11 @@ commons-pool2 true + + org.assertj + assertj-core + true + io.dropwizard.metrics metrics-core diff --git a/spring-boot-parent/pom.xml b/spring-boot-parent/pom.xml old mode 100755 new mode 100644 index 4f8696511b..11c3c9d548 --- a/spring-boot-parent/pom.xml +++ b/spring-boot-parent/pom.xml @@ -211,6 +211,11 @@ junit test + + org.assertj + assertj-core + test + org.mockito mockito-core diff --git a/spring-boot-starters/spring-boot-starter-test/pom.xml b/spring-boot-starters/spring-boot-starter-test/pom.xml index 5ff1f0adcf..d7ca69b320 100644 --- a/spring-boot-starters/spring-boot-starter-test/pom.xml +++ b/spring-boot-starters/spring-boot-starter-test/pom.xml @@ -22,6 +22,10 @@ junit junit + + org.assertj + assertj-core + org.mockito mockito-core diff --git a/spring-boot/pom.xml b/spring-boot/pom.xml index 0de33cba3b..903c315d1a 100644 --- a/spring-boot/pom.xml +++ b/spring-boot/pom.xml @@ -124,6 +124,11 @@ velocity-tools true + + org.assertj + assertj-core + true + org.codehaus.btm btm diff --git a/spring-boot/src/main/java/org/springframework/boot/test/assertj/Matched.java b/spring-boot/src/main/java/org/springframework/boot/test/assertj/Matched.java new file mode 100644 index 0000000000..525ec20083 --- /dev/null +++ b/spring-boot/src/main/java/org/springframework/boot/test/assertj/Matched.java @@ -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}. + *

+ * Usually used with the {@code is} method of {@code assertThat}, for example: + * + *

+ * assertThat("1234").is(Matched.when(startsWith("12")));
+ * 
+ * + * @param The type of object that the condition accepts + * @author Phillip Webb + * @since 1.4 + */ +public final class Matched extends Condition { + + private final Matcher matcher; + + private Matched(Matcher 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 Condition when(Matcher matcher) { + return by(matcher); + } + + public static Condition by(Matcher matcher) { + return new Matched(matcher); + } + +} diff --git a/spring-boot/src/test/java/org/springframework/boot/test/assertj/MatchedTests.java b/spring-boot/src/test/java/org/springframework/boot/test/assertj/MatchedTests.java new file mode 100644 index 0000000000..a07a7501d5 --- /dev/null +++ b/spring-boot/src/test/java/org/springframework/boot/test/assertj/MatchedTests.java @@ -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"))); + } + +}