Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge pull request #404 from marcphilipp/hamcrest-1.3

Upgrade to Hamcrest 1.3
  • Loading branch information...
commit 185a219148564ff905fc8d5d721115313d431c9f 2 parents fbdf793 + fe10c8a
@dsaff dsaff authored
Showing with 370 additions and 504 deletions.
  1. +1 −1  .classpath
  2. +1 −1  build.xml
  3. +1 −1  build/maven/junit-pom-template.xml
  4. +1 −1  build/maven/sample_project_template/pom.xml
  5. +1 −1  build/profile_junit.sh
  6. BIN  lib/hamcrest-core-1.1.jar
  7. BIN  lib/hamcrest-core-1.3-sources.jar
  8. BIN  lib/hamcrest-core-1.3.jar
  9. +4 −19 src/main/java/org/junit/Assert.java
  10. +2 −2 src/main/java/org/junit/Assume.java
  11. +1 −1  src/main/java/org/junit/experimental/results/ResultMatchers.java
  12. +0 −22 src/main/java/org/junit/internal/matchers/CauseMatcher.java
  13. +0 −34 src/main/java/org/junit/internal/matchers/CombinableMatcher.java
  14. +0 −24 src/main/java/org/junit/internal/matchers/Each.java
  15. +0 −67 src/main/java/org/junit/internal/matchers/IsCollectionContaining.java
  16. +56 −0 src/main/java/org/junit/internal/matchers/StacktracePrintingMatcher.java
  17. +0 −31 src/main/java/org/junit/internal/matchers/StringContains.java
  18. +0 −28 src/main/java/org/junit/internal/matchers/SubstringMatcher.java
  19. +37 −0 src/main/java/org/junit/internal/matchers/ThrowableCauseMatcher.java
  20. +37 −0 src/main/java/org/junit/internal/matchers/ThrowableMessageMatcher.java
  21. +3 −0  src/main/java/org/junit/internal/matchers/TypeSafeMatcher.java
  22. +60 −27 src/main/java/org/junit/matchers/JUnitMatchers.java
  23. +17 −48 src/main/java/org/junit/rules/ExpectedException.java
  24. +46 −0 src/main/java/org/junit/rules/ExpectedExceptionMatcherBuilder.java
  25. +37 −0 src/test/java/org/junit/internal/matchers/StacktracePrintingMatcherTest.java
  26. +4 −8 src/test/java/org/junit/tests/AllTests.java
  27. +6 −16 src/test/java/org/junit/tests/assertion/AssertionTest.java
  28. +0 −71 src/test/java/org/junit/tests/assertion/BothTest.java
  29. +0 −18 src/test/java/org/junit/tests/assertion/CauseMatcherTest.java
  30. +0 −13 src/test/java/org/junit/tests/assertion/EachTest.java
  31. +1 −1  src/test/java/org/junit/tests/experimental/AssumptionTest.java
  32. +1 −1  src/test/java/org/junit/tests/experimental/AssumptionViolatedExceptionTest.java
  33. +1 −1  src/test/java/org/junit/tests/experimental/max/MaxStarterTest.java
  34. +1 −1  src/test/java/org/junit/tests/experimental/parallel/ParallelClassTest.java
  35. +1 −1  src/test/java/org/junit/tests/experimental/parallel/ParallelMethodTest.java
  36. +1 −2  src/test/java/org/junit/tests/experimental/results/PrintableResultTest.java
  37. +1 −1  src/test/java/org/junit/tests/experimental/results/ResultMatchersTest.java
  38. +3 −3 src/test/java/org/junit/tests/experimental/rules/EventCollector.java
  39. +15 −28 src/test/java/org/junit/tests/experimental/rules/ExpectedExceptionTest.java
  40. +2 −2 src/test/java/org/junit/tests/experimental/rules/MethodRulesTest.java
  41. +1 −1  src/test/java/org/junit/tests/experimental/rules/TempFolderRuleTest.java
  42. +1 −1  src/test/java/org/junit/tests/experimental/rules/TestRuleTest.java
  43. +1 −1  src/test/java/org/junit/tests/experimental/rules/TimeoutRuleTest.java
  44. +4 −4 src/test/java/org/junit/tests/experimental/rules/VerifierRuleTest.java
  45. +4 −5 src/test/java/org/junit/tests/experimental/theories/ParameterSignatureTest.java
  46. +1 −1  src/test/java/org/junit/tests/experimental/theories/ParameterizedAssertionErrorTest.java
  47. +3 −2 src/test/java/org/junit/tests/experimental/theories/runner/UnsuccessfulWithDataPointFields.java
  48. +1 −1  src/test/java/org/junit/tests/experimental/theories/runner/WhenNoParametersMatch.java
  49. +3 −4 src/test/java/org/junit/tests/experimental/theories/runner/WithDataPointMethod.java
  50. +1 −1  src/test/java/org/junit/tests/experimental/theories/runner/WithOnlyTestAnnotations.java
  51. +1 −1  src/test/java/org/junit/tests/junit3compatibility/AllTestsTest.java
  52. +2 −2 src/test/java/org/junit/tests/running/classes/ParameterizedTestTest.java
  53. +1 −1  src/test/java/org/junit/tests/running/classes/ParentRunnerTest.java
  54. +3 −3 src/test/java/org/junit/tests/running/methods/AnnotationTest.java
  55. +1 −1  src/test/java/org/junit/tests/running/methods/TimeoutTest.java
View
2  .classpath
@@ -3,6 +3,6 @@
<classpathentry kind="src" path="src/main/java"/>
<classpathentry kind="src" path="src/test/java"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/J2SE-1.5"/>
- <classpathentry exported="true" kind="lib" path="lib/hamcrest-core-1.1.jar"/>
+ <classpathentry exported="true" kind="lib" path="lib/hamcrest-core-1.3.jar" sourcepath="lib/hamcrest-core-1.3-sources.jar"/>
<classpathentry kind="output" path="bin"/>
</classpath>
View
2  build.xml
@@ -28,7 +28,7 @@
<property name="javadocdir" location="${dist}/javadoc" />
<property name="javadoczip" location="${dist}-javadoc.zip" />
- <property name="hamcrestlib" location="lib/hamcrest-core-1.1.jar" />
+ <property name="hamcrestlib" location="lib/hamcrest-core-1.3.jar" />
<property name="hamcrestsrc" location="${dist}/temp.hamcrest.source" />
<property name="maven.deploy.goal" value="org.apache.maven.plugins:maven-gpg-plugin:1.1:sign-and-deploy-file" />
View
2  build/maven/junit-pom-template.xml
@@ -58,7 +58,7 @@
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-core</artifactId>
- <version>1.1</version>
+ <version>1.3</version>
<scope>compile</scope>
</dependency>
</dependencies>
View
2  build/maven/sample_project_template/pom.xml
@@ -9,7 +9,7 @@
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
- <version.hamcrest>1.2.1</version.hamcrest>
+ <version.hamcrest>1.3</version.hamcrest>
</properties>
<dependencies>
View
2  build/profile_junit.sh
@@ -1,2 +1,2 @@
-java -classpath ../bin:../lib/hamcrest-core-1.1.jar -agentlib:hprof=cpu=samples,depth=18 org.junit.runner.JUnitCore org.junit.tests.AllTests
+java -classpath ../bin:../lib/hamcrest-core-1.3.jar -agentlib:hprof=cpu=samples,depth=18 org.junit.runner.JUnitCore org.junit.tests.AllTests
cat java.hprof.txt
View
BIN  lib/hamcrest-core-1.1.jar
Binary file not shown
View
BIN  lib/hamcrest-core-1.3-sources.jar
Binary file not shown
View
BIN  lib/hamcrest-core-1.3.jar
Binary file not shown
View
23 src/main/java/org/junit/Assert.java
@@ -1,8 +1,7 @@
package org.junit;
-import org.hamcrest.Description;
import org.hamcrest.Matcher;
-import org.hamcrest.StringDescription;
+import org.hamcrest.MatcherAssert;
import org.junit.internal.ArrayComparisonFailure;
import org.junit.internal.ExactComparisonCriteria;
import org.junit.internal.InexactComparisonCriteria;
@@ -919,7 +918,7 @@ public static void assertEquals(Object[] expecteds, Object[] actuals) {
* values
*
* @see org.hamcrest.CoreMatchers
- * @see org.junit.matchers.JUnitMatchers
+ * @see org.hamcrest.MatcherAssert
*/
public static <T> void assertThat(T actual, Matcher<? super T> matcher) {
assertThat("", actual, matcher);
@@ -931,7 +930,6 @@ public static void assertEquals(Object[] expecteds, Object[] actuals) {
* the reason and information about the matcher and failing value. Example:
*
* <pre>
- * :
* assertThat(&quot;Help! Integers don't work&quot;, 0, is(1)); // fails:
* // failure message:
* // Help! Integers don't work
@@ -957,23 +955,10 @@ public static void assertEquals(Object[] expecteds, Object[] actuals) {
* values
*
* @see org.hamcrest.CoreMatchers
- * @see org.junit.matchers.JUnitMatchers
+ * @see org.hamcrest.MatcherAssert
*/
public static <T> void assertThat(String reason, T actual,
Matcher<? super T> matcher) {
- if (!matcher.matches(actual)) {
- Description description= new StringDescription();
- description.appendText(reason);
- description.appendText("\nExpected: ");
- description.appendDescriptionOf(matcher);
- description.appendText("\n got: ");
- description.appendValue(actual);
- description.appendText("\n");
- java.lang.AssertionError assertionError= new java.lang.AssertionError(
- description.toString());
- if (actual instanceof Throwable)
- assertionError.initCause((Throwable) actual);
- throw assertionError;
- }
+ MatcherAssert.assertThat(reason, actual, matcher);
}
}
View
4 src/main/java/org/junit/Assume.java
@@ -1,12 +1,12 @@
package org.junit;
import static java.util.Arrays.asList;
+import static org.hamcrest.CoreMatchers.everyItem;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.notNullValue;
import static org.hamcrest.CoreMatchers.nullValue;
import org.hamcrest.Matcher;
import org.junit.internal.AssumptionViolatedException;
-import org.junit.internal.matchers.Each;
/**
* A set of methods useful for stating assumptions about the conditions in which a test is meaningful.
@@ -45,7 +45,7 @@ public static void assumeTrue(boolean b) {
* @param objects
*/
public static void assumeNotNull(Object... objects) {
- assumeThat(asList(objects), Each.each(notNullValue()));
+ assumeThat(asList(objects), everyItem(notNullValue()));
}
/**
View
2  src/main/java/org/junit/experimental/results/ResultMatchers.java
@@ -3,7 +3,7 @@
import org.hamcrest.BaseMatcher;
import org.hamcrest.Description;
import org.hamcrest.Matcher;
-import org.junit.internal.matchers.TypeSafeMatcher;
+import org.hamcrest.TypeSafeMatcher;
/**
* Matchers on a PrintableResult, to enable JUnit self-tests.
View
22 src/main/java/org/junit/internal/matchers/CauseMatcher.java
@@ -1,22 +0,0 @@
-package org.junit.internal.matchers;
-
-import org.hamcrest.Description;
-import org.hamcrest.Matcher;
-
-public class CauseMatcher extends TypeSafeMatcher<Throwable> {
- private final Matcher<? super Throwable> fCauseMatcher;
-
- public CauseMatcher(final Matcher<? super Throwable> causeMatcher) {
- fCauseMatcher= causeMatcher;
- }
-
- public void describeTo(Description description) {
- description.appendText("caused by ");
- fCauseMatcher.describeTo(description);
- }
-
- @Override
- public boolean matchesSafely(Throwable item) {
- return fCauseMatcher.matches(item.getCause());
- }
-}
View
34 src/main/java/org/junit/internal/matchers/CombinableMatcher.java
@@ -1,34 +0,0 @@
-package org.junit.internal.matchers;
-
-import static org.hamcrest.CoreMatchers.allOf;
-import static org.hamcrest.CoreMatchers.anyOf;
-import org.hamcrest.BaseMatcher;
-import org.hamcrest.Description;
-import org.hamcrest.Matcher;
-
-public class CombinableMatcher<T> extends BaseMatcher<T> {
-
- private final Matcher<? extends T> fMatcher;
-
- public CombinableMatcher(Matcher<? extends T> matcher) {
- fMatcher= matcher;
- }
-
- public boolean matches(Object item) {
- return fMatcher.matches(item);
- }
-
- public void describeTo(Description description) {
- description.appendDescriptionOf(fMatcher);
- }
-
- @SuppressWarnings("unchecked")
- public CombinableMatcher<T> and(Matcher<? extends T> matcher) {
- return new CombinableMatcher<T>(allOf(matcher, fMatcher));
- }
-
- @SuppressWarnings("unchecked")
- public CombinableMatcher<T> or(Matcher<? extends T> matcher) {
- return new CombinableMatcher<T>(anyOf(matcher, fMatcher));
- }
-}
View
24 src/main/java/org/junit/internal/matchers/Each.java
@@ -1,24 +0,0 @@
-package org.junit.internal.matchers;
-
-import static org.hamcrest.CoreMatchers.not;
-import static org.junit.internal.matchers.IsCollectionContaining.hasItem;
-import org.hamcrest.BaseMatcher;
-import org.hamcrest.Description;
-import org.hamcrest.Matcher;
-
-public class Each {
- public static <T> Matcher<Iterable<T>> each(final Matcher<T> individual) {
- final Matcher<Iterable<T>> allItemsAre = not(hasItem(not(individual)));
-
- return new BaseMatcher<Iterable<T>>() {
- public boolean matches(Object item) {
- return allItemsAre.matches(item);
- }
-
- public void describeTo(Description description) {
- description.appendText("each ");
- individual.describeTo(description);
- }
- };
- }
-}
View
67 src/main/java/org/junit/internal/matchers/IsCollectionContaining.java
@@ -1,67 +0,0 @@
-package org.junit.internal.matchers;
-
-import static org.hamcrest.core.AllOf.allOf;
-import static org.hamcrest.core.IsEqual.equalTo;
-
-import java.util.ArrayList;
-import java.util.Collection;
-
-import org.hamcrest.Description;
-import org.hamcrest.Factory;
-import org.hamcrest.Matcher;
-
-// Copied (hopefully temporarily) from hamcrest-library
-public class IsCollectionContaining<T> extends TypeSafeMatcher<Iterable<T>> {
- private final Matcher<? extends T> elementMatcher;
-
- public IsCollectionContaining(Matcher<? extends T> elementMatcher) {
- this.elementMatcher = elementMatcher;
- }
-
- @Override
- public boolean matchesSafely(Iterable<T> collection) {
- for (T item : collection) {
- if (elementMatcher.matches(item)){
- return true;
- }
- }
- return false;
- }
-
- public void describeTo(Description description) {
- description
- .appendText("a collection containing ")
- .appendDescriptionOf(elementMatcher);
- }
-
- @Factory
- public static <T> Matcher<Iterable<T>> hasItem(Matcher<? extends T> elementMatcher) {
- return new IsCollectionContaining<T>(elementMatcher);
- }
-
- @Factory
- public static <T> Matcher<Iterable<T>> hasItem(T element) {
- return hasItem(equalTo(element));
- }
-
- @Factory
- public static <T> Matcher<Iterable<T>> hasItems(Matcher<? extends T>... elementMatchers) {
- Collection<Matcher<? extends Iterable<T>>> all
- = new ArrayList<Matcher<? extends Iterable<T>>>(elementMatchers.length);
- for (Matcher<? extends T> elementMatcher : elementMatchers) {
- all.add(hasItem(elementMatcher));
- }
- return allOf(all);
- }
-
- @Factory
- public static <T> Matcher<Iterable<T>> hasItems(T... elements) {
- Collection<Matcher<? extends Iterable<T>>> all
- = new ArrayList<Matcher<? extends Iterable<T>>>(elements.length);
- for (T element : elements) {
- all.add(hasItem(element));
- }
- return allOf(all);
- }
-
-}
View
56 src/main/java/org/junit/internal/matchers/StacktracePrintingMatcher.java
@@ -0,0 +1,56 @@
+package org.junit.internal.matchers;
+
+import java.io.PrintWriter;
+import java.io.StringWriter;
+
+import org.hamcrest.Description;
+import org.hamcrest.Factory;
+import org.hamcrest.Matcher;
+
+/**
+ * A matcher that delegates to throwableMatcher and in addition appends the
+ * stacktrace of the actual Throwable in case of a mismatch.
+ */
+public class StacktracePrintingMatcher<T extends Throwable> extends
+ org.hamcrest.TypeSafeMatcher<T> {
+
+ private final Matcher<T> fThrowableMatcher;
+
+ public StacktracePrintingMatcher(Matcher<T> throwableMatcher) {
+ fThrowableMatcher= throwableMatcher;
+ }
+
+ public void describeTo(Description description) {
+ fThrowableMatcher.describeTo(description);
+ }
+
+ @Override
+ protected boolean matchesSafely(T item) {
+ return fThrowableMatcher.matches(item);
+ }
+
+ @Override
+ protected void describeMismatchSafely(T item, Description description) {
+ fThrowableMatcher.describeMismatch(item, description);
+ description.appendText("\nStacktrace was: ");
+ description.appendText(readStacktrace(item));
+ }
+
+ private String readStacktrace(Throwable throwable) {
+ StringWriter stringWriter= new StringWriter();
+ throwable.printStackTrace(new PrintWriter(stringWriter));
+ return stringWriter.toString();
+ }
+
+ @Factory
+ public static <T extends Throwable> Matcher<T> isThrowable(
+ Matcher<T> throwableMatcher) {
+ return new StacktracePrintingMatcher<T>(throwableMatcher);
+ }
+
+ @Factory
+ public static <T extends Exception> Matcher<T> isException(
+ Matcher<T> exceptionMatcher) {
+ return new StacktracePrintingMatcher<T>(exceptionMatcher);
+ }
+}
View
31 src/main/java/org/junit/internal/matchers/StringContains.java
@@ -1,31 +0,0 @@
-/* Copyright (c) 2000-2006 hamcrest.org
- */
-package org.junit.internal.matchers;
-
-import org.hamcrest.Factory;
-import org.hamcrest.Matcher;
-
-/**
- * Tests if the argument is a string that contains a substring.
- */
-public class StringContains extends SubstringMatcher {
- public StringContains(String substring) {
- super(substring);
- }
-
- @Override
- protected boolean evalSubstringOf(String s) {
- return s.indexOf(substring) >= 0;
- }
-
- @Override
- protected String relationship() {
- return "containing";
- }
-
- @Factory
- public static Matcher<String> containsString(String substring) {
- return new StringContains(substring);
- }
-
-}
View
28 src/main/java/org/junit/internal/matchers/SubstringMatcher.java
@@ -1,28 +0,0 @@
-package org.junit.internal.matchers;
-
-import org.hamcrest.Description;
-
-public abstract class SubstringMatcher extends TypeSafeMatcher<String> {
-
- protected final String substring;
-
- protected SubstringMatcher(final String substring) {
- this.substring = substring;
- }
-
- @Override
- public boolean matchesSafely(String item) {
- return evalSubstringOf(item);
- }
-
- public void describeTo(Description description) {
- description.appendText("a string ")
- .appendText(relationship())
- .appendText(" ")
- .appendValue(substring);
- }
-
- protected abstract boolean evalSubstringOf(String string);
-
- protected abstract String relationship();
-}
View
37 src/main/java/org/junit/internal/matchers/ThrowableCauseMatcher.java
@@ -0,0 +1,37 @@
+package org.junit.internal.matchers;
+
+import org.hamcrest.Description;
+import org.hamcrest.Factory;
+import org.hamcrest.Matcher;
+import org.hamcrest.TypeSafeMatcher;
+
+public class ThrowableCauseMatcher<T extends Throwable> extends
+ TypeSafeMatcher<T> {
+
+ private final Matcher<T> fMatcher;
+
+ public ThrowableCauseMatcher(Matcher<T> matcher) {
+ fMatcher= matcher;
+ }
+
+ public void describeTo(Description description) {
+ description.appendText("exception with cause ");
+ description.appendDescriptionOf(fMatcher);
+ }
+
+ @Override
+ protected boolean matchesSafely(T item) {
+ return fMatcher.matches(item.getCause());
+ }
+
+ @Override
+ protected void describeMismatchSafely(T item, Description description) {
+ description.appendText("cause ");
+ fMatcher.describeMismatch(item.getCause(), description);
+ }
+
+ @Factory
+ public static <T extends Throwable> Matcher<T> hasCause(final Matcher<T> matcher) {
+ return new ThrowableCauseMatcher<T>(matcher);
+ }
+}
View
37 src/main/java/org/junit/internal/matchers/ThrowableMessageMatcher.java
@@ -0,0 +1,37 @@
+package org.junit.internal.matchers;
+
+import org.hamcrest.Description;
+import org.hamcrest.Factory;
+import org.hamcrest.Matcher;
+import org.hamcrest.TypeSafeMatcher;
+
+public class ThrowableMessageMatcher<T extends Throwable> extends
+ TypeSafeMatcher<T> {
+
+ private final Matcher<String> fMatcher;
+
+ public ThrowableMessageMatcher(Matcher<String> matcher) {
+ fMatcher= matcher;
+ }
+
+ public void describeTo(Description description) {
+ description.appendText("exception with message ");
+ description.appendDescriptionOf(fMatcher);
+ }
+
+ @Override
+ protected boolean matchesSafely(T item) {
+ return fMatcher.matches(item.getMessage());
+ }
+
+ @Override
+ protected void describeMismatchSafely(T item, Description description) {
+ description.appendText("message ");
+ fMatcher.describeMismatch(item.getMessage(), description);
+ }
+
+ @Factory
+ public static <T extends Throwable> Matcher<T> hasMessage(final Matcher<String> matcher) {
+ return new ThrowableMessageMatcher<T>(matcher);
+ }
+}
View
3  src/main/java/org/junit/internal/matchers/TypeSafeMatcher.java
@@ -10,7 +10,10 @@
* This simply implements the null check, checks the type and then casts.
*
* @author Joe Walnes
+ *
+ * @deprecated Please use {@link org.hamcrest.TypeSafeMatcher}.
*/
+@Deprecated
public abstract class TypeSafeMatcher<T> extends BaseMatcher<T> {
private Class<?> expectedType;
View
87 src/main/java/org/junit/matchers/JUnitMatchers.java
@@ -1,11 +1,10 @@
package org.junit.matchers;
+import org.hamcrest.CoreMatchers;
import org.hamcrest.Matcher;
-import org.junit.internal.matchers.CombinableMatcher;
-import org.junit.internal.matchers.Each;
-import org.junit.internal.matchers.IsCollectionContaining;
-import org.junit.internal.matchers.StringContains;
-import org.junit.internal.matchers.CauseMatcher;
+import org.hamcrest.core.CombinableMatcher.CombinableBothMatcher;
+import org.hamcrest.core.CombinableMatcher.CombinableEitherMatcher;
+import org.junit.internal.matchers.StacktracePrintingMatcher;
/**
* Convenience import class: these are useful matchers for use with the assertThat method, but they are
@@ -16,25 +15,34 @@
/**
* @param element
* @return A matcher matching any collection containing element
+ *
+ * @deprecated Please use {@link CoreMatchers#hasItem(Object)} instead.
*/
- public static <T> org.hamcrest.Matcher<java.lang.Iterable<T>> hasItem(T element) {
- return IsCollectionContaining.hasItem(element);
+ @Deprecated
+ public static <T> Matcher<Iterable<? super T>> hasItem(T element) {
+ return CoreMatchers.hasItem(element);
}
/**
* @param elementMatcher
* @return A matcher matching any collection containing an element matching elementMatcher
+ *
+ * @deprecated Please use {@link CoreMatchers#hasItem(Matcher)} instead.
*/
- public static <T> org.hamcrest.Matcher<java.lang.Iterable<T>> hasItem(org.hamcrest.Matcher<? extends T> elementMatcher) {
- return IsCollectionContaining.hasItem(elementMatcher);
+ @Deprecated
+ public static <T> Matcher<Iterable<? super T>> hasItem(Matcher<? super T> elementMatcher) {
+ return CoreMatchers.hasItem(elementMatcher);
}
/**
* @param elements
* @return A matcher matching any collection containing every element in elements
+ *
+ * @deprecated Please use {@link CoreMatchers#hasItems(Object...)} instead.
*/
- public static <T> org.hamcrest.Matcher<java.lang.Iterable<T>> hasItems(T... elements) {
- return IsCollectionContaining.hasItems(elements);
+ @Deprecated
+ public static <T> Matcher<Iterable<T>> hasItems(T... elements) {
+ return CoreMatchers.hasItems(elements);
}
/**
@@ -42,25 +50,34 @@
* @return A matcher matching any collection containing at least one element that matches
* each matcher in elementMatcher (this may be one element matching all matchers,
* or different elements matching each matcher)
+ *
+ * @deprecated Please use {@link CoreMatchers#hasItems(Matcher...)} instead.
*/
- public static <T> org.hamcrest.Matcher<java.lang.Iterable<T>> hasItems(org.hamcrest.Matcher<? extends T>... elementMatchers) {
- return IsCollectionContaining.hasItems(elementMatchers);
+ @Deprecated
+ public static <T> Matcher<Iterable<T>> hasItems(Matcher<? super T>... elementMatchers) {
+ return CoreMatchers.hasItems(elementMatchers);
}
/**
* @param elementMatcher
* @return A matcher matching any collection in which every element matches elementMatcher
+ *
+ * @deprecated Please use {@link CoreMatchers#everyItem(Matcher)} instead.
*/
+ @Deprecated
public static <T> Matcher<Iterable<T>> everyItem(final Matcher<T> elementMatcher) {
- return Each.each(elementMatcher);
+ return CoreMatchers.everyItem(elementMatcher);
}
/**
* @param substring
* @return a matcher matching any string that contains substring
+ *
+ * @deprecated Please use {@link CoreMatchers#containsString(String)} instead.
*/
- public static org.hamcrest.Matcher<java.lang.String> containsString(java.lang.String substring) {
- return StringContains.containsString(substring);
+ @Deprecated
+ public static Matcher<java.lang.String> containsString(java.lang.String substring) {
+ return CoreMatchers.containsString(substring);
}
/**
@@ -68,9 +85,12 @@
* <pre>
* assertThat(string, both(containsString("a")).and(containsString("b")));
* </pre>
+ *
+ * @deprecated Please use {@link CoreMatchers#both(Matcher)} instead.
*/
- public static <T> CombinableMatcher<T> both(Matcher<T> matcher) {
- return new CombinableMatcher<T>(matcher);
+ @Deprecated
+ public static <T> CombinableBothMatcher<T> both(Matcher<? super T> matcher) {
+ return CoreMatchers.both(matcher);
}
/**
@@ -78,16 +98,29 @@
* <pre>
* assertThat(string, either(containsString("a")).or(containsString("b")));
* </pre>
+ *
+ * @deprecated Please use {@link CoreMatchers#either(Matcher)} instead.
*/
- public static <T> CombinableMatcher<T> either(Matcher<T> matcher) {
- return new CombinableMatcher<T>(matcher);
- }
-
+ @Deprecated
+ public static <T> CombinableEitherMatcher<T> either(Matcher<? super T> matcher) {
+ return CoreMatchers.either(matcher);
+ }
+
+ /**
+ * @param throwableMatcher
+ * @return A matcher that delegates to throwableMatcher and in addition
+ * appends the stacktrace of the actual Throwable in case of a mismatch.
+ */
+ public static <T extends Throwable> Matcher<T> isThrowable(Matcher<T> throwableMatcher) {
+ return StacktracePrintingMatcher.isThrowable(throwableMatcher);
+ }
+
/**
- * @param causeMatcher
- * @return a matcher matching the cause of the exception with the given {@code causeMatcher}
+ * @param exceptionMatcher
+ * @return A matcher that delegates to exceptionMatcher and in addition
+ * appends the stacktrace of the actual Exception in case of a mismatch.
*/
- public static Matcher<Throwable> causedBy(final Matcher<? super Throwable> causeMatcher) {
- return new CauseMatcher(causeMatcher);
- }
+ public static <T extends Exception> Matcher<T> isException(Matcher<T> exceptionMatcher) {
+ return StacktracePrintingMatcher.isException(exceptionMatcher);
+ }
}
View
65 src/main/java/org/junit/rules/ExpectedException.java
@@ -1,14 +1,15 @@
package org.junit.rules;
+import static junit.framework.Assert.fail;
+import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.CoreMatchers.instanceOf;
import static org.junit.Assert.assertThat;
-import static org.junit.matchers.JUnitMatchers.both;
-import static org.junit.matchers.JUnitMatchers.containsString;
-import org.hamcrest.Description;
+import static org.junit.internal.matchers.ThrowableCauseMatcher.hasCause;
+import static org.junit.internal.matchers.ThrowableMessageMatcher.hasMessage;
+import junit.framework.Assert;
import org.hamcrest.Matcher;
import org.hamcrest.StringDescription;
import org.junit.internal.AssumptionViolatedException;
-import org.junit.internal.matchers.TypeSafeMatcher;
import org.junit.runners.model.Statement;
/**
@@ -89,7 +90,7 @@ public static ExpectedException none() {
return new ExpectedException();
}
- private Matcher<Object> fMatcher= null;
+ private final ExpectedExceptionMatcherBuilder fMatcherBuilder= new ExpectedExceptionMatcherBuilder();
private boolean handleAssumptionViolatedExceptions= false;
@@ -117,14 +118,8 @@ public Statement apply(Statement base,
* Adds {@code matcher} to the list of requirements for any thrown
* exception.
*/
- // Should be able to remove this suppression in some brave new hamcrest
- // world.
- @SuppressWarnings("unchecked")
public void expect(Matcher<?> matcher) {
- if (fMatcher == null)
- fMatcher= (Matcher<Object>) matcher;
- else
- fMatcher= both(fMatcher).and(matcher);
+ fMatcherBuilder.add(matcher);
}
/**
@@ -170,22 +165,23 @@ public ExpectedExceptionStatement(Statement base) {
public void evaluate() throws Throwable {
try {
fNext.evaluate();
+ if (fMatcherBuilder.expectsThrowable())
+ failDueToMissingException();
} catch (AssumptionViolatedException e) {
optionallyHandleException(e, handleAssumptionViolatedExceptions);
- return;
} catch (AssertionError e) {
optionallyHandleException(e, handleAssertionErrors);
- return;
} catch (Throwable e) {
handleException(e);
- return;
}
- if (fMatcher != null)
- throw new AssertionError("Expected test to throw "
- + StringDescription.toString(fMatcher));
}
}
+ private void failDueToMissingException() throws AssertionError {
+ String expectation= StringDescription.toString(fMatcherBuilder.build());
+ fail("Expected test to throw " + expectation);
+ }
+
private void optionallyHandleException(Throwable e, boolean handleException)
throws Throwable {
if (handleException)
@@ -195,36 +191,9 @@ private void optionallyHandleException(Throwable e, boolean handleException)
}
private void handleException(Throwable e) throws Throwable {
- if (fMatcher == null)
+ if (fMatcherBuilder.expectsThrowable())
+ assertThat(e, fMatcherBuilder.build());
+ else
throw e;
- assertThat(e, fMatcher);
- }
-
- private Matcher<Throwable> hasMessage(final Matcher<String> matcher) {
- return new TypeSafeMatcher<Throwable>() {
- public void describeTo(Description description) {
- description.appendText("exception with message ");
- description.appendDescriptionOf(matcher);
- }
-
- @Override
- public boolean matchesSafely(Throwable item) {
- return matcher.matches(item.getMessage());
- }
- };
- }
-
- private Matcher<Throwable> hasCause(final Matcher<? extends Throwable> causeMatcher) {
- return new TypeSafeMatcher<Throwable>() {
- public void describeTo(Description description) {
- description.appendText("exception with cause ");
- description.appendDescriptionOf(causeMatcher);
- }
-
- @Override
- public boolean matchesSafely(Throwable item) {
- return causeMatcher.matches(item.getCause());
- }
- };
}
}
View
46 src/main/java/org/junit/rules/ExpectedExceptionMatcherBuilder.java
@@ -0,0 +1,46 @@
+package org.junit.rules;
+
+import static org.hamcrest.CoreMatchers.allOf;
+import static org.junit.matchers.JUnitMatchers.isThrowable;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import org.hamcrest.Matcher;
+
+/**
+ * Builds special matcher used by {@link ExpectedException}.
+ */
+class ExpectedExceptionMatcherBuilder {
+
+ private final List<Matcher<?>> fMatchers= new ArrayList<Matcher<?>>();
+
+ void add(Matcher<?> matcher) {
+ fMatchers.add(matcher);
+ }
+
+ boolean expectsThrowable() {
+ return !fMatchers.isEmpty();
+ }
+
+ Matcher<Throwable> build() {
+ return isThrowable(allOfTheMatchers());
+ }
+
+ private Matcher<Throwable> allOfTheMatchers() {
+ if (fMatchers.size() == 1) {
+ return cast(fMatchers.get(0));
+ }
+ return allOf(castedMatchers());
+ }
+
+ @SuppressWarnings({ "unchecked", "rawtypes" })
+ private List<Matcher<? super Throwable>> castedMatchers() {
+ return new ArrayList<Matcher<? super Throwable>>((List) fMatchers);
+ }
+
+ @SuppressWarnings("unchecked")
+ private Matcher<Throwable> cast(Matcher<?> singleMatcher) {
+ return (Matcher<Throwable>) singleMatcher;
+ }
+}
View
37 src/test/java/org/junit/internal/matchers/StacktracePrintingMatcherTest.java
@@ -0,0 +1,37 @@
+package org.junit.internal.matchers;
+
+import static org.hamcrest.CoreMatchers.any;
+import static org.hamcrest.CoreMatchers.containsString;
+import static org.hamcrest.CoreMatchers.equalTo;
+import static org.hamcrest.CoreMatchers.notNullValue;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertThat;
+import static org.junit.Assert.assertTrue;
+import static org.junit.internal.matchers.StacktracePrintingMatcher.isException;
+import static org.junit.internal.matchers.StacktracePrintingMatcher.isThrowable;
+import org.junit.Test;
+
+public class StacktracePrintingMatcherTest {
+
+ @Test
+ public void succeedsWhenInnerMatcherSucceeds() throws Exception {
+ assertTrue(isThrowable(any(Throwable.class)).matches(new Exception()));
+ }
+
+ @Test
+ public void failsWhenInnerMatcherFails() throws Exception {
+ assertFalse(isException(notNullValue(Exception.class)).matches(null));
+ }
+
+ @Test
+ public void assertThatIncludesStacktrace() {
+ Exception actual= new IllegalArgumentException("my message");
+ Exception expected= new NullPointerException();
+
+ try {
+ assertThat(actual, isThrowable(equalTo(expected)));
+ } catch (AssertionError e) {
+ assertThat(e.getMessage(), containsString("Stacktrace was: java.lang.IllegalArgumentException: my message"));
+ }
+ }
+}
View
12 src/test/java/org/junit/tests/AllTests.java
@@ -3,14 +3,12 @@
import junit.framework.JUnit4TestAdapter;
import junit.framework.Test;
import org.junit.internal.MethodSorterTest;
+import org.junit.internal.matchers.StacktracePrintingMatcherTest;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
import org.junit.runners.Suite.SuiteClasses;
import org.junit.tests.assertion.AssertionTest;
-import org.junit.tests.assertion.BothTest;
-import org.junit.tests.assertion.EachTest;
import org.junit.tests.assertion.MultipleFailureExceptionTest;
-import org.junit.tests.assertion.CauseMatcherTest;
import org.junit.tests.deprecated.JUnit4ClassRunnerTest;
import org.junit.tests.description.AnnotatedDescriptionTest;
import org.junit.tests.description.SuiteDescriptionTest;
@@ -31,8 +29,8 @@
import org.junit.tests.experimental.rules.ExternalResourceRuleTest;
import org.junit.tests.experimental.rules.MethodRulesTest;
import org.junit.tests.experimental.rules.NameRulesTest;
-import org.junit.tests.experimental.rules.RuleFieldValidatorTest;
import org.junit.tests.experimental.rules.RuleChainTest;
+import org.junit.tests.experimental.rules.RuleFieldValidatorTest;
import org.junit.tests.experimental.rules.TempFolderRuleTest;
import org.junit.tests.experimental.rules.TemporaryFolderUsageTest;
import org.junit.tests.experimental.rules.TestRuleTest;
@@ -126,9 +124,7 @@
IgnoreClassTest.class,
OldTestClassAdaptingListenerTest.class,
AnnotatedDescriptionTest.class,
- BothTest.class,
AssumptionViolatedExceptionTest.class,
- EachTest.class,
ExperimentalTests.class,
InheritedTestTest.class,
TestClassTest.class,
@@ -162,9 +158,9 @@
RuleFieldValidatorTest.class,
RuleChainTest.class,
BlockJUnit4ClassRunnerTest.class,
- CauseMatcherTest.class,
MethodSorterTest.class,
- TestedOnSupplierTest.class
+ TestedOnSupplierTest.class,
+ StacktracePrintingMatcherTest.class
})
public class AllTests {
public static Test suite() {
View
22 src/test/java/org/junit/tests/assertion/AssertionTest.java
@@ -1,13 +1,14 @@
package org.junit.tests.assertion;
import static org.hamcrest.CoreMatchers.equalTo;
+import static org.hamcrest.CoreMatchers.instanceOf;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertNotSame;
-import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
@@ -462,7 +463,7 @@ public void arraysDeclaredAsObjectAreComparedAsObjects() {
String expected = "expected";
String actual = "actual";
- String expectedMessage = "identifier\nExpected: \"expected\"\n got: \"actual\"\n";
+ String expectedMessage = "identifier\nExpected: \"expected\"\n but: was \"actual\"";
try {
assertThat("identifier", actual, equalTo(expected));
@@ -472,10 +473,10 @@ public void arraysDeclaredAsObjectAreComparedAsObjects() {
}
@Test public void assertThatIncludesAdvancedMismatch() {
- String expectedMessage = "identifier\nExpected: is an instance of java.lang.Integer\n got: \"actual\"\n";
+ String expectedMessage = "identifier\nExpected: is an instance of java.lang.Integer\n but: \"actual\" is a java.lang.String";
try {
- assertThat("identifier", "actual", is(Integer.class));
+ assertThat("identifier", "actual", is(instanceOf(Integer.class)));
} catch (AssertionError e) {
assertEquals(expectedMessage, e.getMessage());
}
@@ -485,7 +486,7 @@ public void arraysDeclaredAsObjectAreComparedAsObjects() {
String expected = "expected";
String actual = "actual";
- String expectedMessage = "\nExpected: \"expected\"\n got: \"actual\"\n";
+ String expectedMessage = "\nExpected: \"expected\"\n but: was \"actual\"";
try {
assertThat(actual, equalTo(expected));
@@ -494,17 +495,6 @@ public void arraysDeclaredAsObjectAreComparedAsObjects() {
}
}
- @Test public void assertThatIncludesActualException() {
- Exception actual= new IllegalArgumentException();
- Exception expected= new NullPointerException();
-
- try {
- assertThat(actual, equalTo(expected));
- } catch (AssertionError e) {
- assertEquals(actual, e.getCause());
- }
- }
-
@Test public void nullAndStringNullPrintCorrectError() {
try {
assertEquals(null, "null");
View
71 src/test/java/org/junit/tests/assertion/BothTest.java
@@ -1,71 +0,0 @@
-package org.junit.tests.assertion;
-
-import static org.hamcrest.CoreMatchers.is;
-import static org.hamcrest.CoreMatchers.not;
-import static org.junit.Assert.assertThat;
-import static org.junit.Assume.assumeTrue;
-import static org.junit.matchers.JUnitMatchers.both;
-import static org.junit.matchers.JUnitMatchers.containsString;
-import static org.junit.matchers.JUnitMatchers.either;
-import org.hamcrest.Matcher;
-import org.junit.Test;
-import org.junit.experimental.theories.DataPoint;
-import org.junit.experimental.theories.Theories;
-import org.junit.experimental.theories.Theory;
-import org.junit.runner.RunWith;
-
-@RunWith(Theories.class)
-public class BothTest {
- @DataPoint
- public static Matcher<Integer> IS_3= is(3);
-
- @DataPoint
- public static Matcher<Integer> IS_4= is(4);
-
- @DataPoint
- public static int THREE= 3;
-
- @Test
- public void bothPasses() {
- assertThat(3, both(is(Integer.class)).and(is(3)));
- }
-
- @Theory
- public void bothFails(int value, Matcher<Integer> first,
- Matcher<Integer> second) {
- assumeTrue(!(first.matches(value) && second.matches(value)));
- assertThat(value, not(both(first).and(second)));
- }
-
- @Theory
- public <T> void descriptionIsSensible(Matcher<T> first, Matcher<T> second) {
- Matcher<?> both= both(first).and(second);
- assertThat(both.toString(), containsString(first.toString()));
- assertThat(both.toString(), containsString(second.toString()));
- }
-
- @Test
- public void eitherPasses() {
- assertThat(3, either(is(3)).or(is(4)));
- }
-
- @Theory
- public <T> void threeAndsWork(Matcher<Integer> first,
- Matcher<Integer> second, Matcher<Integer> third, int value) {
- assumeTrue(first.matches(value) && second.matches(value)
- && third.matches(value));
- assertThat(value, both(first).and(second).and(third));
- }
-
- @Theory
- public <T> void threeOrsWork(Matcher<Integer> first,
- Matcher<Integer> second, Matcher<Integer> third, int value) {
- assumeTrue(first.matches(value) || second.matches(value)
- || third.matches(value));
- assertThat(value, either(first).or(second).or(third));
- }
-
- @Test public void subclassesAreOkInSecondPositionOnly() {
- assertThat(3, both(is(Integer.class)).and(is(3)));
- }
-}
View
18 src/test/java/org/junit/tests/assertion/CauseMatcherTest.java
@@ -1,18 +0,0 @@
-package org.junit.tests.assertion;
-
-import static org.hamcrest.CoreMatchers.instanceOf;
-import static org.junit.Assert.assertThat;
-import static org.junit.matchers.JUnitMatchers.causedBy;
-import org.junit.Test;
-
-public class CauseMatcherTest {
- @Test
- public void causedByCorrect() {
- assertThat(new IllegalStateException(new IllegalArgumentException()), causedBy(instanceOf(IllegalArgumentException.class)));
- }
-
- @Test(expected=AssertionError.class)
- public void causedByFailing() {
- assertThat(new IllegalStateException(new IllegalArgumentException()), causedBy(instanceOf(IllegalStateException.class)));
- }
-}
View
13 src/test/java/org/junit/tests/assertion/EachTest.java
@@ -1,13 +0,0 @@
-package org.junit.tests.assertion;
-
-import static org.hamcrest.CoreMatchers.is;
-import static org.junit.Assert.assertThat;
-import org.junit.Test;
-import org.junit.internal.matchers.Each;
-
-public class EachTest {
- @Test
- public void eachDescription() {
- assertThat(Each.each(is("a")).toString(), is("each is \"a\""));
- }
-}
View
2  src/test/java/org/junit/tests/experimental/AssumptionTest.java
@@ -1,5 +1,6 @@
package org.junit.tests.experimental;
+import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.fail;
@@ -9,7 +10,6 @@
import static org.junit.Assume.assumeTrue;
import static org.junit.experimental.results.PrintableResult.testResult;
import static org.junit.experimental.results.ResultMatchers.isSuccessful;
-import static org.junit.internal.matchers.StringContains.containsString;
import org.junit.Assume;
import org.junit.Before;
import org.junit.BeforeClass;
View
2  src/test/java/org/junit/tests/experimental/AssumptionViolatedExceptionTest.java
@@ -1,10 +1,10 @@
package org.junit.tests.experimental;
+import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.notNullValue;
import static org.junit.Assert.assertThat;
import static org.junit.Assume.assumeThat;
-import static org.junit.internal.matchers.StringContains.containsString;
import org.hamcrest.Matcher;
import org.hamcrest.StringDescription;
import org.junit.Test;
View
2  src/test/java/org/junit/tests/experimental/max/MaxStarterTest.java
@@ -1,11 +1,11 @@
package org.junit.tests.experimental.max;
+import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
-import static org.junit.matchers.JUnitMatchers.containsString;
import java.io.File;
import java.util.ArrayList;
View
2  src/test/java/org/junit/tests/experimental/parallel/ParallelClassTest.java
@@ -4,9 +4,9 @@
import static org.junit.Assert.assertTrue;
import org.hamcrest.Description;
import org.hamcrest.Matcher;
+import org.hamcrest.TypeSafeMatcher;
import org.junit.Test;
import org.junit.experimental.ParallelComputer;
-import org.junit.internal.matchers.TypeSafeMatcher;
import org.junit.runner.JUnitCore;
import org.junit.runner.Result;
import org.junit.runner.RunWith;
View
2  src/test/java/org/junit/tests/experimental/parallel/ParallelMethodTest.java
@@ -4,9 +4,9 @@
import static org.junit.Assert.assertTrue;
import org.hamcrest.Description;
import org.hamcrest.Matcher;
+import org.hamcrest.TypeSafeMatcher;
import org.junit.Test;
import org.junit.experimental.ParallelComputer;
-import org.junit.internal.matchers.TypeSafeMatcher;
import org.junit.runner.JUnitCore;
import org.junit.runner.Result;
View
3  src/test/java/org/junit/tests/experimental/results/PrintableResultTest.java
@@ -2,8 +2,8 @@
import static java.util.Arrays.asList;
import static org.hamcrest.CoreMatchers.allOf;
+import static org.hamcrest.CoreMatchers.containsString;
import static org.junit.Assert.assertThat;
-import static org.junit.internal.matchers.StringContains.containsString;
import java.util.Arrays;
@@ -17,7 +17,6 @@
@RunWith(Theories.class)
public class PrintableResultTest {
- @SuppressWarnings("unchecked")
@Theory(nullsAccepted= false)
public void backTraceHasGoodToString(String descriptionName,
final String stackTraceClassName) {
View
2  src/test/java/org/junit/tests/experimental/results/ResultMatchersTest.java
@@ -1,8 +1,8 @@
package org.junit.tests.experimental.results;
+import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
-import static org.junit.internal.matchers.StringContains.containsString;
import org.junit.Test;
import org.junit.experimental.results.ResultMatchers;
import org.junit.experimental.theories.Theory;
View
6 src/test/java/org/junit/tests/experimental/rules/EventCollector.java
@@ -1,13 +1,13 @@
package org.junit.tests.experimental.rules;
+import static org.hamcrest.CoreMatchers.allOf;
import static org.hamcrest.core.IsEqual.equalTo;
-import static org.junit.matchers.JUnitMatchers.both;
import java.util.ArrayList;
import java.util.List;
import org.hamcrest.Matcher;
-import org.junit.internal.matchers.TypeSafeMatcher;
+import org.hamcrest.TypeSafeMatcher;
import org.junit.runner.Description;
import org.junit.runner.Result;
import org.junit.runner.notification.Failure;
@@ -15,7 +15,7 @@
class EventCollector extends RunListener {
static Matcher<EventCollector> everyTestRunSuccessful() {
- return both(hasNoFailure()).and(hasNoAssumptionFailure());
+ return allOf(hasNoFailure(), hasNoAssumptionFailure());
}
private static Matcher<EventCollector> hasNumberOfFailures(
View
43 src/test/java/org/junit/tests/experimental/rules/ExpectedExceptionTest.java
@@ -2,30 +2,27 @@
import static java.util.Arrays.asList;
import static org.hamcrest.CoreMatchers.any;
-import static org.hamcrest.CoreMatchers.instanceOf;
+import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.CoreMatchers.is;
+import static org.hamcrest.CoreMatchers.nullValue;
+import static org.hamcrest.CoreMatchers.startsWith;
import static org.hamcrest.core.IsEqual.equalTo;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.fail;
import static org.junit.Assume.assumeTrue;
-import static org.junit.matchers.JUnitMatchers.causedBy;
import static org.junit.rules.ExpectedException.none;
import static org.junit.tests.experimental.rules.EventCollector.everyTestRunSuccessful;
-import static org.junit.tests.experimental.rules.EventCollector.failureIs;
import static org.junit.tests.experimental.rules.EventCollector.hasSingleAssumptionFailure;
import static org.junit.tests.experimental.rules.EventCollector.hasSingleFailure;
import static org.junit.tests.experimental.rules.EventCollector.hasSingleFailureWithMessage;
-import java.io.IOException;
import java.util.Collection;
import org.hamcrest.CoreMatchers;
-import org.hamcrest.Description;
import org.hamcrest.Matcher;
import org.junit.Rule;
import org.junit.Test;
import org.junit.internal.AssumptionViolatedException;
-import org.junit.internal.matchers.TypeSafeMatcher;
import org.junit.rules.ExpectedException;
import org.junit.runner.JUnitCore;
import org.junit.runner.RunWith;
@@ -49,8 +46,8 @@
hasSingleFailureWithMessage(startsWith("\nExpected: an instance of java.lang.NullPointerException")) },
{
HasWrongMessage.class,
- hasSingleFailureWithMessage("\nExpected: exception with message a string containing \"expectedMessage\"\n"
- + " got: <java.lang.IllegalArgumentException: actualMessage>\n") },
+ hasSingleFailureWithMessage(startsWith("\nExpected: exception with message a string containing \"expectedMessage\"\n"
+ + " but: message was \"actualMessage\"")) },
{
ThrowNoExceptionButExpectExceptionWithType.class,
hasSingleFailureWithMessage("Expected test to throw an instance of java.lang.NullPointerException") },
@@ -70,7 +67,7 @@
hasSingleFailure() },
{
ThrowAssumptionViolatedExceptionButExpectOtherType.class,
- failureIs(causedBy(instanceOf(AssumptionViolatedException.class))) },
+ hasSingleFailureWithMessage(containsString("Stacktrace was: org.junit.internal.AssumptionViolatedException")) },
{ ViolateAssumptionAndExpectException.class,
hasSingleAssumptionFailure() },
{ ThrowExpectedAssertionError.class, everyTestRunSuccessful() },
@@ -81,12 +78,17 @@
hasSingleFailureWithMessage(ARBITRARY_MESSAGE) },
{
ExpectsMultipleMatchers.class,
- hasSingleFailureWithMessage(startsWith("\nExpected: (exception with message a string containing \"Ack!\" and an instance of java.lang.IllegalArgumentException)")) },
+ hasSingleFailureWithMessage(startsWith("\nExpected: (an instance of java.lang.IllegalArgumentException and exception with message a string containing \"Ack!\")")) },
{ ThrowExceptionWithMatchingCause.class, everyTestRunSuccessful() },
{ ThrowExpectedNullCause.class, everyTestRunSuccessful() },
{
ThrowUnexpectedCause.class,
- hasSingleFailureWithMessage(startsWith("\nExpected: (exception with cause")) }
+ hasSingleFailureWithMessage(CoreMatchers.<String>allOf(
+ startsWith("\nExpected: ("),
+ containsString("exception with cause is <java.lang.NullPointerException: expected cause>"),
+ containsString("cause was <java.lang.NullPointerException: an unexpected cause>"),
+ containsString("Stacktrace was: java.lang.IllegalArgumentException: Ack!"),
+ containsString("Caused by: java.lang.NullPointerException: an unexpected cause"))) }
});
}
@@ -234,7 +236,7 @@ public void throwsMore() {
@Test
public void throwsMore() {
- thrown.expect(any(IOException.class));
+ thrown.expect(any(Exception.class));
throw new NullPointerException("Ack!");
}
}
@@ -346,7 +348,7 @@ public void throwExceptionWithMatchingCause() {
public void throwExpectedNullCause() {
thrown.expect(IllegalArgumentException.class);
thrown.expectMessage("Ack!");
- thrown.expectCause(CoreMatchers.<Throwable>nullValue());
+ thrown.expectCause(nullValue(Throwable.class));
throw new IllegalArgumentException("Ack!");
}
@@ -366,19 +368,4 @@ public void throwWithCause() {
throw new IllegalArgumentException("Ack!", new NullPointerException("an unexpected cause"));
}
}
-
-
- private static Matcher<String> startsWith(final String prefix) {
- return new TypeSafeMatcher<String>() {
- public void describeTo(Description description) {
- description.appendText("starts with ");
- description.appendText(prefix);
- }
-
- @Override
- public boolean matchesSafely(String item) {
- return item.startsWith(prefix);
- }
- };
- }
}
View
4 src/test/java/org/junit/tests/experimental/rules/MethodRulesTest.java
@@ -1,5 +1,6 @@
package org.junit.tests.experimental.rules;
+import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;
@@ -8,14 +9,13 @@
import static org.junit.experimental.results.PrintableResult.testResult;
import static org.junit.experimental.results.ResultMatchers.hasSingleFailureContaining;
import static org.junit.experimental.results.ResultMatchers.isSuccessful;
-import static org.junit.matchers.JUnitMatchers.containsString;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
-import org.junit.rules.TestRule;
import org.junit.rules.MethodRule;
import org.junit.rules.TestName;
+import org.junit.rules.TestRule;
import org.junit.rules.TestWatchman;
import org.junit.runner.Description;
import org.junit.runner.JUnitCore;
View
2  src/test/java/org/junit/tests/experimental/rules/TempFolderRuleTest.java
@@ -1,5 +1,6 @@
package org.junit.tests.experimental.rules;
+import static org.hamcrest.CoreMatchers.hasItem;
import static org.hamcrest.core.IsNot.not;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertThat;
@@ -7,7 +8,6 @@
import static org.junit.experimental.results.PrintableResult.testResult;
import static org.junit.experimental.results.ResultMatchers.failureCountIs;
import static org.junit.experimental.results.ResultMatchers.isSuccessful;
-import static org.junit.internal.matchers.IsCollectionContaining.hasItem;
import java.io.File;
import java.io.IOException;
View
2  src/test/java/org/junit/tests/experimental/rules/TestRuleTest.java
@@ -1,5 +1,6 @@
package org.junit.tests.experimental.rules;
+import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;
@@ -8,7 +9,6 @@
import static org.junit.experimental.results.PrintableResult.testResult;
import static org.junit.experimental.results.ResultMatchers.hasSingleFailureContaining;
import static org.junit.experimental.results.ResultMatchers.isSuccessful;
-import static org.junit.matchers.JUnitMatchers.containsString;
import java.util.LinkedList;
import java.util.List;
View
2  src/test/java/org/junit/tests/experimental/rules/TimeoutRuleTest.java
@@ -1,8 +1,8 @@
package org.junit.tests.experimental.rules;
+import static org.hamcrest.CoreMatchers.containsString;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;
-import static org.junit.matchers.JUnitMatchers.containsString;
import org.junit.Ignore;
import org.junit.Rule;
import org.junit.Test;
View
8 src/test/java/org/junit/tests/experimental/rules/VerifierRuleTest.java
@@ -59,12 +59,12 @@
@Test public void usedErrorCollectorCheckThatShouldFail() {
PrintableResult testResult= testResult(UsesErrorCollectorCheckThat.class);
- assertThat(testResult, hasFailureContaining("got: <3>"));
- assertThat(testResult, hasFailureContaining("got: <5>"));
+ assertThat(testResult, hasFailureContaining("was <3>"));
+ assertThat(testResult, hasFailureContaining("was <5>"));
assertThat(testResult, hasFailureContaining("reason 1"));
- assertThat(testResult, hasFailureContaining("got: <7>"));
+ assertThat(testResult, hasFailureContaining("was <7>"));
assertThat(testResult, hasFailureContaining("reason 2"));
- assertThat(testResult, hasFailureContaining("got: <9>"));
+ assertThat(testResult, hasFailureContaining("was <9>"));
}
public static class UsesErrorCollectorCheckSucceeds {
View
9 src/test/java/org/junit/tests/experimental/theories/ParameterSignatureTest.java
@@ -1,16 +1,15 @@
package org.junit.tests.experimental.theories;
-import static org.hamcrest.CoreMatchers.is;
+import static org.hamcrest.CoreMatchers.isA;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;
import static org.junit.Assume.assumeTrue;
-import static org.junit.matchers.JUnitMatchers.hasItem;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
-import java.util.ArrayList;
import java.util.List;
+import org.hamcrest.CoreMatchers;
import org.junit.Test;
import org.junit.experimental.theories.DataPoint;
import org.junit.experimental.theories.ParameterSignature;
@@ -51,7 +50,7 @@ public void getAnnotations() throws SecurityException,
Method method= ParameterSignatureTest.class.getMethod("foo", int.class);
List<Annotation> annotations= ParameterSignature.signatures(method)
.get(0).getAnnotations();
- assertThat(new ArrayList<Object>(annotations),
- hasItem(is(TestedOn.class)));
+ assertThat(annotations,
+ CoreMatchers.<TestedOn> hasItem(isA(TestedOn.class)));
}
}
View
2  src/test/java/org/junit/tests/experimental/theories/ParameterizedAssertionErrorTest.java
@@ -1,10 +1,10 @@
package org.junit.tests.experimental.theories;
+import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.not;
import static org.junit.Assert.assertThat;
import static org.junit.Assume.assumeThat;
-import static org.junit.internal.matchers.StringContains.containsString;
import org.junit.Test;
import org.junit.experimental.theories.DataPoint;
import org.junit.experimental.theories.Theories;
View
5 ...st/java/org/junit/tests/experimental/theories/runner/UnsuccessfulWithDataPointFields.java
@@ -6,8 +6,9 @@
import static org.junit.experimental.results.ResultMatchers.failureCountIs;
import static org.junit.experimental.results.ResultMatchers.hasFailureContaining;
import static org.junit.experimental.results.ResultMatchers.hasSingleFailureContaining;
-import static org.junit.matchers.JUnitMatchers.both;
+import org.hamcrest.CoreMatchers;
import org.junit.Test;
+import org.junit.experimental.results.PrintableResult;
import org.junit.experimental.theories.DataPoint;
import org.junit.experimental.theories.Theories;
import org.junit.experimental.theories.Theory;
@@ -99,7 +100,7 @@ public void numbers(int x) {
public void dataPointsMustBeStatic() {
assertThat(
testResult(DataPointsMustBeStatic.class),
- both(failureCountIs(2))
+ CoreMatchers.<PrintableResult> both(failureCountIs(2))
.and(
hasFailureContaining("DataPoint field THREE must be static"))
.and(
View
2  src/test/java/org/junit/tests/experimental/theories/runner/WhenNoParametersMatch.java
@@ -1,11 +1,11 @@
package org.junit.tests.experimental.theories.runner;
+import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.not;
import static org.junit.Assert.assertThat;
import static org.junit.Assume.assumeThat;
import static org.junit.experimental.results.PrintableResult.testResult;
-import static org.junit.internal.matchers.StringContains.containsString;
import org.hamcrest.Matcher;
import org.junit.experimental.theories.DataPoint;
import org.junit.experimental.theories.DataPoints;
View
7 src/test/java/org/junit/tests/experimental/theories/runner/WithDataPointMethod.java
@@ -1,13 +1,13 @@
package org.junit.tests.experimental.theories.runner;
+import static org.hamcrest.CoreMatchers.containsString;
+import static org.hamcrest.CoreMatchers.everyItem;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.not;
import static org.hamcrest.CoreMatchers.nullValue;
import static org.junit.Assert.assertThat;
import static org.junit.experimental.results.PrintableResult.testResult;
import static org.junit.experimental.results.ResultMatchers.isSuccessful;
-import static org.junit.internal.matchers.Each.each;
-import static org.junit.internal.matchers.StringContains.containsString;
import java.lang.reflect.Method;
import java.util.ArrayList;
@@ -146,7 +146,6 @@ public void ignoreDataPointMethodsWithoutAnnotation() throws Throwable {
}
private Matcher<Iterable<Failure>> empty() {
- Matcher<Failure> nullValue= nullValue();
- return each(nullValue);
+ return everyItem(nullValue(Failure.class));
}
}
View
2  src/test/java/org/junit/tests/experimental/theories/runner/WithOnlyTestAnnotations.java
@@ -1,12 +1,12 @@
package org.junit.tests.experimental.theories.runner;
+import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;
import static org.junit.experimental.results.PrintableResult.testResult;
import static org.junit.experimental.results.ResultMatchers.failureCountIs;
import static org.junit.experimental.results.ResultMatchers.isSuccessful;
-import static org.junit.matchers.JUnitMatchers.containsString;
import org.junit.Test;
import org.junit.experimental.theories.DataPoint;
import org.junit.experimental.theories.Theories;
View
2  src/test/java/org/junit/tests/junit3compatibility/AllTestsTest.java
@@ -1,9 +1,9 @@
package org.junit.tests.junit3compatibility;
+import static org.hamcrest.CoreMatchers.containsString;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
-import static org.junit.matchers.JUnitMatchers.containsString;
import junit.framework.JUnit4TestAdapter;
import junit.framework.TestCase;
import junit.framework.TestSuite;
View
4 src/test/java/org/junit/tests/running/classes/ParameterizedTestTest.java
@@ -1,9 +1,9 @@
package org.junit.tests.running.classes;
+import static org.hamcrest.CoreMatchers.containsString;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;
import static org.junit.experimental.results.PrintableResult.testResult;
-import static org.junit.matchers.JUnitMatchers.containsString;
import java.util.Arrays;
import java.util.Collection;
@@ -254,4 +254,4 @@ public void aTest() {
public void exceptionWhenPrivateConstructor() throws Throwable {
new Parameterized(PrivateConstructor.class);
}
-}
+}
View
2  src/test/java/org/junit/tests/running/classes/ParentRunnerTest.java
@@ -8,8 +8,8 @@
import java.util.List;
import org.hamcrest.Matcher;
+import org.hamcrest.TypeSafeMatcher;
import org.junit.Test;
-import org.junit.internal.matchers.TypeSafeMatcher;
import org.junit.runner.Description;
import org.junit.runner.JUnitCore;
import org.junit.runner.Request;
View
6 src/test/java/org/junit/tests/running/methods/AnnotationTest.java
@@ -1,8 +1,8 @@
package org.junit.tests.running.methods;
+import static org.hamcrest.CoreMatchers.allOf;
+import static org.hamcrest.CoreMatchers.containsString;
import static org.junit.Assert.assertThat;
-import static org.junit.matchers.JUnitMatchers.both;
-import static org.junit.matchers.JUnitMatchers.containsString;
import java.util.Collection;
import java.util.HashSet;
@@ -143,7 +143,7 @@ public void testTestAndTeardownFailure() throws Exception {
Result runner= core.run(TestAndTeardownFailureTest.class);
assertEquals(1, runner.getRunCount());
assertEquals(2, runner.getFailureCount());
- assertThat(runner.getFailures().toString(), both(containsString("hereAfter")).and(containsString("inTest")));
+ assertThat(runner.getFailures().toString(), allOf(containsString("hereAfter"), containsString("inTest")));
}
static public class TeardownAfterFailureTest {
View
2  src/test/java/org/junit/tests/running/methods/TimeoutTest.java
@@ -1,11 +1,11 @@
package org.junit.tests.running.methods;
+import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
-import static org.junit.matchers.JUnitMatchers.containsString;
import java.io.PrintWriter;
import java.io.StringWriter;
Please sign in to comment.
Something went wrong with that request. Please try again.