From a25784fd2eafb38df5ab8f3fbe6a6d9204e820ae Mon Sep 17 00:00:00 2001 From: java-coding-prodigy Date: Thu, 27 Oct 2022 12:02:50 +0530 Subject: [PATCH 01/12] Added new method --- .../src/main/java/org/assertj/core/api/Assertions.java | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/assertj-core/src/main/java/org/assertj/core/api/Assertions.java b/assertj-core/src/main/java/org/assertj/core/api/Assertions.java index 7e96b486a8..199dc924dc 100644 --- a/assertj-core/src/main/java/org/assertj/core/api/Assertions.java +++ b/assertj-core/src/main/java/org/assertj/core/api/Assertions.java @@ -1415,11 +1415,16 @@ public static Throwable catchThrowable(ThrowingCallable shouldRaiseThrowable) { * @see #catchThrowable(ThrowingCallable) * @since 3.9.0 */ + @Deprecated(since = "3.23.11") public static THROWABLE catchThrowableOfType(ThrowingCallable shouldRaiseThrowable, Class type) { return AssertionsForClassTypes.catchThrowableOfType(shouldRaiseThrowable, type); } + public static THROWABLE catchThrowableOfType(Class type, + ThrowingCallable shouldRaiseThrowable) { + return AssertionsForClassTypes.catchThrowableOfType(shouldRaiseThrowable, type); + } /** * Allows catching an instance of {@link Exception}. *

From fc65a9319d1af263c8ea81e813a89758681e6f1f Mon Sep 17 00:00:00 2001 From: java-coding-prodigy Date: Thu, 27 Oct 2022 12:08:42 +0530 Subject: [PATCH 02/12] replaced usages --- .../main/java/org/assertj/core/api/Assertions.java | 6 +++--- .../org/assertj/core/api/AssertionsForClassTypes.java | 6 ++++++ .../java/org/assertj/core/api/Java6Assertions.java | 4 ++++ .../java/org/assertj/core/api/ThrowableAssert.java | 11 +++++++++++ .../java/org/assertj/core/api/WithAssertions.java | 5 +++++ 5 files changed, 29 insertions(+), 3 deletions(-) diff --git a/assertj-core/src/main/java/org/assertj/core/api/Assertions.java b/assertj-core/src/main/java/org/assertj/core/api/Assertions.java index 199dc924dc..24a8d53514 100644 --- a/assertj-core/src/main/java/org/assertj/core/api/Assertions.java +++ b/assertj-core/src/main/java/org/assertj/core/api/Assertions.java @@ -1356,7 +1356,7 @@ public static ObjectAssert assertWith(T actual, Consumer... requiremen *

* This caught {@link Throwable} can then be asserted. *

- * If you need to assert on the real type of Throwable caught (e.g. IOException), use {@link #catchThrowableOfType(ThrowingCallable, Class)}. + * If you need to assert on the real type of Throwable caught (e.g. IOException), use {@link #catchThrowableOfType(Class, ThrowingCallable)}. *

* Example: *

{@literal @}Test
@@ -1371,7 +1371,7 @@ public static  ObjectAssert assertWith(T actual, Consumer... requiremen
    *
    * @param shouldRaiseThrowable The lambda with the code that should raise the exception.
    * @return The captured exception or null if none was raised by the callable.
-   * @see #catchThrowableOfType(ThrowingCallable, Class)
+   * @see #catchThrowableOfType(Class, ThrowingCallable)
    */
   public static Throwable catchThrowable(ThrowingCallable shouldRaiseThrowable) {
     return AssertionsForClassTypes.catchThrowable(shouldRaiseThrowable);
@@ -1423,7 +1423,7 @@ public static  THROWABLE catchThrowableOfType(Throw
 
   public static  THROWABLE catchThrowableOfType(Class type,
                                                                              ThrowingCallable shouldRaiseThrowable) {
-    return AssertionsForClassTypes.catchThrowableOfType(shouldRaiseThrowable, type);
+    return AssertionsForClassTypes.catchThrowableOfType(type, shouldRaiseThrowable);
   }
   /**
    * Allows catching an instance of {@link Exception}.
diff --git a/assertj-core/src/main/java/org/assertj/core/api/AssertionsForClassTypes.java b/assertj-core/src/main/java/org/assertj/core/api/AssertionsForClassTypes.java
index b89560a84b..7b5c85fa9f 100644
--- a/assertj-core/src/main/java/org/assertj/core/api/AssertionsForClassTypes.java
+++ b/assertj-core/src/main/java/org/assertj/core/api/AssertionsForClassTypes.java
@@ -940,11 +940,17 @@ public static Throwable catchThrowable(ThrowingCallable shouldRaiseThrowable) {
    * @see #catchThrowable(ThrowableAssert.ThrowingCallable)
    * @since 3.9.0
    */
+  @Deprecated(since = "3.23.11")
   public static  THROWABLE catchThrowableOfType(ThrowingCallable shouldRaiseThrowable,
                                                                              Class type) {
     return ThrowableAssert.catchThrowableOfType(shouldRaiseThrowable, type);
   }
 
+
+  public static  THROWABLE catchThrowableOfType(Class type,
+                                                                             ThrowingCallable shouldRaiseThrowable) {
+    return ThrowableAssert.catchThrowableOfType(type, shouldRaiseThrowable);
+  }
   // -------------------------------------------------------------------------------------------------
   // fail methods : not assertions but here to have a single entry point to all AssertJ features.
   // -------------------------------------------------------------------------------------------------
diff --git a/assertj-core/src/main/java/org/assertj/core/api/Java6Assertions.java b/assertj-core/src/main/java/org/assertj/core/api/Java6Assertions.java
index 43fe430700..300388e18b 100644
--- a/assertj-core/src/main/java/org/assertj/core/api/Java6Assertions.java
+++ b/assertj-core/src/main/java/org/assertj/core/api/Java6Assertions.java
@@ -1299,10 +1299,14 @@ public static Throwable catchThrowable(ThrowingCallable shouldRaiseThrowable) {
    * @see #catchThrowable(ThrowableAssert.ThrowingCallable)
    * @since 3.9.0
    */
+  @Deprecated(since = "3.23.11")
   public static  THROWABLE catchThrowableOfType(ThrowingCallable shouldRaiseThrowable, Class type) {
     return ThrowableAssert.catchThrowableOfType(shouldRaiseThrowable, type);
   }
 
+  public static  THROWABLE catchThrowableOfType(Class type, ThrowingCallable shouldRaiseThrowable) {
+    return ThrowableAssert.catchThrowableOfType(type, shouldRaiseThrowable);
+  }
   // -------------------------------------------------------------------------------------------------
   // fail methods : not assertions but here to have a single entry point to all AssertJ features.
   // -------------------------------------------------------------------------------------------------
diff --git a/assertj-core/src/main/java/org/assertj/core/api/ThrowableAssert.java b/assertj-core/src/main/java/org/assertj/core/api/ThrowableAssert.java
index 086d90732e..a489f2bd22 100644
--- a/assertj-core/src/main/java/org/assertj/core/api/ThrowableAssert.java
+++ b/assertj-core/src/main/java/org/assertj/core/api/ThrowableAssert.java
@@ -68,6 +68,7 @@ public static Throwable catchThrowable(ThrowingCallable shouldRaiseThrowable) {
   }
 
   @SuppressWarnings("unchecked")
+  @Deprecated(since = "3.23.11")
   public static  THROWABLE catchThrowableOfType(ThrowingCallable shouldRaiseThrowable,
                                                                              Class type) {
     Throwable throwable = catchThrowable(shouldRaiseThrowable);
@@ -77,4 +78,14 @@ public static  THROWABLE catchThrowableOfType(Throw
                                   .isInstanceOf(type);
     return (THROWABLE) throwable;
   }
+  @SuppressWarnings("unchecked")
+  public static  THROWABLE catchThrowableOfType(Class type,
+                                                                             ThrowingCallable shouldRaiseThrowable) {
+    Throwable throwable = catchThrowable(shouldRaiseThrowable);
+    if (throwable == null) return null;
+    // check exception type
+    new ThrowableAssert(throwable).overridingErrorMessage(shouldBeInstance(throwable, type).create())
+      .isInstanceOf(type);
+    return (THROWABLE) throwable;
+  }
 }
diff --git a/assertj-core/src/main/java/org/assertj/core/api/WithAssertions.java b/assertj-core/src/main/java/org/assertj/core/api/WithAssertions.java
index 9afdbf8660..372594e162 100644
--- a/assertj-core/src/main/java/org/assertj/core/api/WithAssertions.java
+++ b/assertj-core/src/main/java/org/assertj/core/api/WithAssertions.java
@@ -2877,11 +2877,16 @@ default Throwable catchThrowable(final ThrowingCallable shouldRaiseThrowable) {
    * @see #catchThrowable(ThrowingCallable)
    * @since 3.9.0
    */
+  @Deprecated(since = "3.23.11")
   default  THROWABLE catchThrowableOfType(final ThrowingCallable shouldRaiseThrowable,
                                                                        final Class type) {
     return Assertions.catchThrowableOfType(shouldRaiseThrowable, type);
   }
 
+  default  THROWABLE catchThrowableOfType(final Class type,
+                                                                       final ThrowingCallable shouldRaiseThrowable) {
+    return Assertions.catchThrowableOfType(type, shouldRaiseThrowable);
+  }
   /**
    * Allows catching an instance of {@link Exception}.
    * 

From cfd47ea9e13e0dfeca1dabe13a0ffc7422746797 Mon Sep 17 00:00:00 2001 From: java-coding-prodigy Date: Fri, 28 Oct 2022 19:51:30 +0530 Subject: [PATCH 03/12] Changed and added documentation --- .../java/org/assertj/core/api/Assertions.java | 41 +++++++++++++++++- .../core/api/AssertionsForClassTypes.java | 43 +++++++++++++++++-- .../org/assertj/core/api/Java6Assertions.java | 38 ++++++++++++++++ .../org/assertj/core/api/WithAssertions.java | 40 ++++++++++++++++- 4 files changed, 157 insertions(+), 5 deletions(-) diff --git a/assertj-core/src/main/java/org/assertj/core/api/Assertions.java b/assertj-core/src/main/java/org/assertj/core/api/Assertions.java index 24a8d53514..8589e3d519 100644 --- a/assertj-core/src/main/java/org/assertj/core/api/Assertions.java +++ b/assertj-core/src/main/java/org/assertj/core/api/Assertions.java @@ -1371,13 +1371,14 @@ public static ObjectAssert assertWith(T actual, Consumer... requiremen * * @param shouldRaiseThrowable The lambda with the code that should raise the exception. * @return The captured exception or null if none was raised by the callable. - * @see #catchThrowableOfType(Class, ThrowingCallable) + * @see #catchThrowableOfType(ThrowingCallable, Class) */ public static Throwable catchThrowable(ThrowingCallable shouldRaiseThrowable) { return AssertionsForClassTypes.catchThrowable(shouldRaiseThrowable); } /** + * @deprecated use {@link #catchThrowableOfType(Class, ThrowingCallable)} instead. * Allows catching a {@link Throwable} of a specific type. *

* A call is made to {@code catchThrowable(ThrowingCallable)}, if no exception is thrown it returns null @@ -1421,6 +1422,44 @@ public static THROWABLE catchThrowableOfType(Throw return AssertionsForClassTypes.catchThrowableOfType(shouldRaiseThrowable, type); } + /** + * Allows catching a {@link Throwable} of a specific type. + *

+ * A call is made to {@code catchThrowable(ThrowingCallable)}, if no exception is thrown it returns null + * otherwise it checks that the caught {@link Throwable} has the specified type and casts it making it convenient to perform subtype-specific assertions on it. + *

+ * Example: + *

 class TextException extends Exception {
+   *   int line;
+   *   int column;
+   *
+   *   public TextException(String msg, int line, int column) {
+   *     super(msg);
+   *     this.line = line;
+   *     this.column = column;
+   *   }
+   * }
+   *
+   * TextException textException = catchThrowableOfType(() -> { throw new TextException("boom!", 1, 5); },
+   *                                                    TextException.class);
+   * // assertions succeed
+   * assertThat(textException).hasMessage("boom!");
+   * assertThat(textException.line).isEqualTo(1);
+   * assertThat(textException.column).isEqualTo(5);
+   *
+   * // succeeds as catchThrowableOfType returns null when the code does not thrown any exceptions
+   * assertThat(catchThrowableOfType( Exception.class, () -> {})).isNull();
+   *
+   * // fails as TextException is not a RuntimeException
+   * catchThrowableOfType(() -> { throw new TextException("boom!", 1, 5); }, RuntimeException.class);
+ * + * @param the {@link Throwable} type. + * @param shouldRaiseThrowable The lambda with the code that should raise the exception. + * @param type The type of exception that the code is expected to raise. + * @return The captured exception or null if none was raised by the callable. + * @see #catchThrowable(ThrowingCallable) + * @since 3.23.11 + */ public static THROWABLE catchThrowableOfType(Class type, ThrowingCallable shouldRaiseThrowable) { return AssertionsForClassTypes.catchThrowableOfType(type, shouldRaiseThrowable); diff --git a/assertj-core/src/main/java/org/assertj/core/api/AssertionsForClassTypes.java b/assertj-core/src/main/java/org/assertj/core/api/AssertionsForClassTypes.java index 7b5c85fa9f..a3902e0f62 100644 --- a/assertj-core/src/main/java/org/assertj/core/api/AssertionsForClassTypes.java +++ b/assertj-core/src/main/java/org/assertj/core/api/AssertionsForClassTypes.java @@ -821,7 +821,7 @@ public static ThrowableTypeAssert assertThatExceptionOf *
assertThatNoException().isThrownBy(() -> { System.out.println("OK"); });
* * This method is more or less the same of {@code assertThatCode(...).doesNotThrowAnyException();} but in a more natural way. - + * @return the created {@link NotThrownAssert}. * @since 3.17.0 */ @@ -897,13 +897,14 @@ public static NotThrownAssert assertThatNoException() { * * @param shouldRaiseThrowable The lambda with the code that should raise the exception. * @return The captured exception or null if none was raised by the callable. - * @see AssertionsForClassTypes#catchThrowableOfType(ThrowableAssert.ThrowingCallable, Class) + * @see AssertionsForClassTypes#catchThrowableOfType(Class, ThrowableAssert.ThrowingCallable) */ public static Throwable catchThrowable(ThrowingCallable shouldRaiseThrowable) { return ThrowableAssert.catchThrowable(shouldRaiseThrowable); } /** + * @deprecated use {@link #catchThrowableOfType(Class, ThrowingCallable)} instead. * Allows catching a {@link Throwable} of a specific type. *

* A call is made to {@code catchThrowable(ThrowingCallable)}, if no exception is thrown {@code catchThrowableOfType} returns null, @@ -946,7 +947,43 @@ public static THROWABLE catchThrowableOfType(Throw return ThrowableAssert.catchThrowableOfType(shouldRaiseThrowable, type); } - + /** + * Allows catching a {@link Throwable} of a specific type. + *

+ * A call is made to {@code catchThrowable(ThrowingCallable)}, if no exception is thrown {@code catchThrowableOfType} returns null, + * otherwise it checks that the caught {@link Throwable} has the specified type then casts it to it before returning it, + * making it convenient to perform subtype-specific assertions on the result. + *

+ * Example: + *

 class CustomParseException extends Exception {
+   *   int line;
+   *   int column;
+   *
+   *   public CustomParseException(String msg, int l, int c) {
+   *     super(msg);
+   *     line = l;
+   *     column = c;
+   *   }
+   * }
+   *
+   * CustomParseException e = catchThrowableOfType(() -> { throw new CustomParseException("boom!", 1, 5); },
+   *                                               CustomParseException.class);
+   * // assertions pass
+   * assertThat(e).hasMessageContaining("boom");
+   * assertThat(e.line).isEqualTo(1);
+   * assertThat(e.column).isEqualTo(5);
+   *
+   * // fails as CustomParseException is not a RuntimeException
+   * catchThrowableOfType(RuntimeException.class,
+   *                     () -> { throw new CustomParseException("boom!", 1, 5); });
+ * + * @param the {@link Throwable} type. + * @param shouldRaiseThrowable The lambda with the code that should raise the exception. + * @param type The type of exception that the code is expected to raise. + * @return The captured exception or null if none was raised by the callable. + * @see #catchThrowable(ThrowableAssert.ThrowingCallable) + * @since 3.23.11 + */ public static THROWABLE catchThrowableOfType(Class type, ThrowingCallable shouldRaiseThrowable) { return ThrowableAssert.catchThrowableOfType(type, shouldRaiseThrowable); diff --git a/assertj-core/src/main/java/org/assertj/core/api/Java6Assertions.java b/assertj-core/src/main/java/org/assertj/core/api/Java6Assertions.java index 300388e18b..a01ac229ca 100644 --- a/assertj-core/src/main/java/org/assertj/core/api/Java6Assertions.java +++ b/assertj-core/src/main/java/org/assertj/core/api/Java6Assertions.java @@ -1263,6 +1263,7 @@ public static Throwable catchThrowable(ThrowingCallable shouldRaiseThrowable) { } /** + * @deprecated use {@link #catchThrowableOfType(Class, ThrowingCallable)} instead. * Allows catching a {@link Throwable} of a specific type. *

* A call is made to {@code catchThrowable(ThrowingCallable)}, if no exception is thrown {@code catchThrowableOfType} returns null, @@ -1304,6 +1305,43 @@ public static THROWABLE catchThrowableOfType(Throw return ThrowableAssert.catchThrowableOfType(shouldRaiseThrowable, type); } + /** + * Allows catching a {@link Throwable} of a specific type. + *

+ * A call is made to {@code catchThrowable(ThrowingCallable)}, if no exception is thrown {@code catchThrowableOfType} returns null, + * otherwise it checks that the caught {@link Throwable} has the specified type then casts it to it before returning it, + * making it convenient to perform subtype-specific assertions on the result. + *

+ * Example: + *

 class CustomParseException extends Exception {
+   *   int line;
+   *   int column;
+   *
+   *   public CustomParseException(String msg, int l, int c) {
+   *     super(msg);
+   *     line = l;
+   *     column = c;
+   *   }
+   * }
+   *
+   * CustomParseException e = catchThrowableOfType(CustomParseException.class,
+   *                                               () -> { throw new CustomParseException("boom!", 1, 5); });
+   * // assertions pass
+   * assertThat(e).hasMessageContaining("boom");
+   * assertThat(e.line).isEqualTo(1);
+   * assertThat(e.column).isEqualTo(5);
+   *
+   * // fails as CustomParseException is not a RuntimeException
+   * catchThrowableOfType(RuntimeException.class,
+   *                      () -> { throw new CustomParseException("boom!", 1, 5); });
+ * + * @param the {@link Throwable} type. + * @param shouldRaiseThrowable The lambda with the code that should raise the exception. + * @param type The type of exception that the code is expected to raise. + * @return The captured exception or null if none was raised by the callable. + * @see #catchThrowable(ThrowableAssert.ThrowingCallable) + * @since 3.23.11 + */ public static THROWABLE catchThrowableOfType(Class type, ThrowingCallable shouldRaiseThrowable) { return ThrowableAssert.catchThrowableOfType(type, shouldRaiseThrowable); } diff --git a/assertj-core/src/main/java/org/assertj/core/api/WithAssertions.java b/assertj-core/src/main/java/org/assertj/core/api/WithAssertions.java index 372594e162..9008a44064 100644 --- a/assertj-core/src/main/java/org/assertj/core/api/WithAssertions.java +++ b/assertj-core/src/main/java/org/assertj/core/api/WithAssertions.java @@ -2834,13 +2834,14 @@ default ObjectAssert assertWith(T actual, Consumer... requirements) { * * @param shouldRaiseThrowable The lambda with the code that should raise the exception. * @return The captured exception or null if none was raised by the callable. - * @see #catchThrowableOfType(ThrowingCallable, Class) + * @see #catchThrowableOfType(Class, ThrowingCallable) */ default Throwable catchThrowable(final ThrowingCallable shouldRaiseThrowable) { return Assertions.catchThrowable(shouldRaiseThrowable); } /** + * @deprecated use {@link #catchThrowableOfType(Class, ThrowingCallable)} instead. * Allows catching a {@link Throwable} of a specific type. *

* A call is made to {@code catchThrowable(ThrowingCallable)}, if no exception is thrown {@code catchThrowableOfType} returns null, @@ -2883,6 +2884,43 @@ default THROWABLE catchThrowableOfType(final Throw return Assertions.catchThrowableOfType(shouldRaiseThrowable, type); } + /** + * Allows catching a {@link Throwable} of a specific type. + *

+ * A call is made to {@code catchThrowable(ThrowingCallable)}, if no exception is thrown {@code catchThrowableOfType} returns null, + * otherwise it checks that the caught {@link Throwable} has the specified type then casts it to it before returning it, + * making it convenient to perform subtype-specific assertions on the result. + *

+ * Example: + *

 class CustomParseException extends Exception {
+   *   int line;
+   *   int column;
+   *
+   *   public CustomParseException(String msg, int l, int c) {
+   *     super(msg);
+   *     line = l;
+   *     column = c;
+   *   }
+   * }
+   *
+   * CustomParseException e = catchThrowableOfType(() -> { throw new CustomParseException("boom!", 1, 5); },
+   *                                               CustomParseException.class);
+   * // assertions pass
+   * assertThat(e).hasMessageContaining("boom");
+   * assertThat(e.line).isEqualTo(1);
+   * assertThat(e.column).isEqualTo(5);
+   *
+   * // fails as CustomParseException is not a RuntimeException
+   * catchThrowableOfType(RuntimeException.class,
+   *                      () -> { throw new CustomParseException("boom!", 1, 5); });
+ * + * @param the {@link Throwable} type. + * @param shouldRaiseThrowable The lambda with the code that should raise the exception. + * @param type The type of exception that the code is expected to raise. + * @return The captured exception or null if none was raised by the callable. + * @see #catchThrowable(ThrowingCallable) + * @since 3.23.11 + */ default THROWABLE catchThrowableOfType(final Class type, final ThrowingCallable shouldRaiseThrowable) { return Assertions.catchThrowableOfType(type, shouldRaiseThrowable); From d00ec98b8e6d18f3ffcface3f63b8666323962f6 Mon Sep 17 00:00:00 2001 From: java-coding-prodigy Date: Sun, 6 Nov 2022 11:17:43 +0530 Subject: [PATCH 04/12] removed @since in deprecated and changed method calls in deprecated methods --- .../src/main/java/org/assertj/core/api/Assertions.java | 4 ++-- .../java/org/assertj/core/api/AssertionsForClassTypes.java | 4 ++-- .../src/main/java/org/assertj/core/api/Java6Assertions.java | 4 ++-- .../src/main/java/org/assertj/core/api/ThrowableAssert.java | 2 +- .../src/main/java/org/assertj/core/api/WithAssertions.java | 4 ++-- 5 files changed, 9 insertions(+), 9 deletions(-) diff --git a/assertj-core/src/main/java/org/assertj/core/api/Assertions.java b/assertj-core/src/main/java/org/assertj/core/api/Assertions.java index 8589e3d519..7296e66d94 100644 --- a/assertj-core/src/main/java/org/assertj/core/api/Assertions.java +++ b/assertj-core/src/main/java/org/assertj/core/api/Assertions.java @@ -1416,10 +1416,10 @@ public static Throwable catchThrowable(ThrowingCallable shouldRaiseThrowable) { * @see #catchThrowable(ThrowingCallable) * @since 3.9.0 */ - @Deprecated(since = "3.23.11") + @Deprecated public static THROWABLE catchThrowableOfType(ThrowingCallable shouldRaiseThrowable, Class type) { - return AssertionsForClassTypes.catchThrowableOfType(shouldRaiseThrowable, type); + return AssertionsForClassTypes.catchThrowableOfType(type, shouldRaiseThrowable); } /** diff --git a/assertj-core/src/main/java/org/assertj/core/api/AssertionsForClassTypes.java b/assertj-core/src/main/java/org/assertj/core/api/AssertionsForClassTypes.java index a3902e0f62..152585d8f0 100644 --- a/assertj-core/src/main/java/org/assertj/core/api/AssertionsForClassTypes.java +++ b/assertj-core/src/main/java/org/assertj/core/api/AssertionsForClassTypes.java @@ -941,10 +941,10 @@ public static Throwable catchThrowable(ThrowingCallable shouldRaiseThrowable) { * @see #catchThrowable(ThrowableAssert.ThrowingCallable) * @since 3.9.0 */ - @Deprecated(since = "3.23.11") + @Deprecated public static THROWABLE catchThrowableOfType(ThrowingCallable shouldRaiseThrowable, Class type) { - return ThrowableAssert.catchThrowableOfType(shouldRaiseThrowable, type); + return ThrowableAssert.catchThrowableOfType(type, shouldRaiseThrowable); } /** diff --git a/assertj-core/src/main/java/org/assertj/core/api/Java6Assertions.java b/assertj-core/src/main/java/org/assertj/core/api/Java6Assertions.java index a01ac229ca..c0df0905c3 100644 --- a/assertj-core/src/main/java/org/assertj/core/api/Java6Assertions.java +++ b/assertj-core/src/main/java/org/assertj/core/api/Java6Assertions.java @@ -1300,9 +1300,9 @@ public static Throwable catchThrowable(ThrowingCallable shouldRaiseThrowable) { * @see #catchThrowable(ThrowableAssert.ThrowingCallable) * @since 3.9.0 */ - @Deprecated(since = "3.23.11") + @Deprecated public static THROWABLE catchThrowableOfType(ThrowingCallable shouldRaiseThrowable, Class type) { - return ThrowableAssert.catchThrowableOfType(shouldRaiseThrowable, type); + return ThrowableAssert.catchThrowableOfType(type, shouldRaiseThrowable); } /** diff --git a/assertj-core/src/main/java/org/assertj/core/api/ThrowableAssert.java b/assertj-core/src/main/java/org/assertj/core/api/ThrowableAssert.java index a489f2bd22..8a7f36d1b9 100644 --- a/assertj-core/src/main/java/org/assertj/core/api/ThrowableAssert.java +++ b/assertj-core/src/main/java/org/assertj/core/api/ThrowableAssert.java @@ -68,7 +68,7 @@ public static Throwable catchThrowable(ThrowingCallable shouldRaiseThrowable) { } @SuppressWarnings("unchecked") - @Deprecated(since = "3.23.11") + @Deprecated public static THROWABLE catchThrowableOfType(ThrowingCallable shouldRaiseThrowable, Class type) { Throwable throwable = catchThrowable(shouldRaiseThrowable); diff --git a/assertj-core/src/main/java/org/assertj/core/api/WithAssertions.java b/assertj-core/src/main/java/org/assertj/core/api/WithAssertions.java index 9008a44064..9064267f91 100644 --- a/assertj-core/src/main/java/org/assertj/core/api/WithAssertions.java +++ b/assertj-core/src/main/java/org/assertj/core/api/WithAssertions.java @@ -2878,10 +2878,10 @@ default Throwable catchThrowable(final ThrowingCallable shouldRaiseThrowable) { * @see #catchThrowable(ThrowingCallable) * @since 3.9.0 */ - @Deprecated(since = "3.23.11") + @Deprecated default THROWABLE catchThrowableOfType(final ThrowingCallable shouldRaiseThrowable, final Class type) { - return Assertions.catchThrowableOfType(shouldRaiseThrowable, type); + return Assertions.catchThrowableOfType(type, shouldRaiseThrowable); } /** From a4d7146d303aa6d1e02858e864e556c60ca82ea7 Mon Sep 17 00:00:00 2001 From: java-coding-prodigy Date: Sun, 6 Nov 2022 11:20:38 +0530 Subject: [PATCH 05/12] Changed test methods --- .../core/api/Assertions_catchThrowableOfType_Test.java | 2 +- .../api/EntryPointAssertions_catchThrowableOfType_Test.java | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/assertj-core/src/test/java/org/assertj/core/api/Assertions_catchThrowableOfType_Test.java b/assertj-core/src/test/java/org/assertj/core/api/Assertions_catchThrowableOfType_Test.java index c487b39425..adf3b913e8 100644 --- a/assertj-core/src/test/java/org/assertj/core/api/Assertions_catchThrowableOfType_Test.java +++ b/assertj-core/src/test/java/org/assertj/core/api/Assertions_catchThrowableOfType_Test.java @@ -48,7 +48,7 @@ void catchThrowable_returns_null_when_no_exception_thrown() { @Test void catchThrowableOfType_should_fail_with_good_message_if_wrong_type() { // GIVEN - ThrowingCallable code = () -> catchThrowableOfType(raisingException("boom!!"), RuntimeException.class); + ThrowingCallable code = () -> catchThrowableOfType(RuntimeException.class, raisingException("boom!!")); // WHEN AssertionError assertionError = expectAssertionError(code); // THEN diff --git a/assertj-core/src/test/java/org/assertj/core/api/EntryPointAssertions_catchThrowableOfType_Test.java b/assertj-core/src/test/java/org/assertj/core/api/EntryPointAssertions_catchThrowableOfType_Test.java index 3c5a07b94f..5d2c1d4031 100644 --- a/assertj-core/src/test/java/org/assertj/core/api/EntryPointAssertions_catchThrowableOfType_Test.java +++ b/assertj-core/src/test/java/org/assertj/core/api/EntryPointAssertions_catchThrowableOfType_Test.java @@ -27,18 +27,18 @@ class EntryPointAssertions_catchThrowableOfType_Test extends EntryPointAssertion @ParameterizedTest @MethodSource("catchThrowableOfTypes") - void should_catch_throwable_of_type(BiFunction, RuntimeException> catchThrowableOfType) { + void should_catch_throwable_of_type(BiFunction, ThrowingCallable, RuntimeException> catchThrowableOfType) { // GIVEN ThrowingCallable throwingCallable = () -> { throw RUNTIME_EXCEPTION; }; // WHEN - RuntimeException throwable = catchThrowableOfType.apply(throwingCallable, RuntimeException.class); + RuntimeException throwable = catchThrowableOfType.apply(RuntimeException.class, throwingCallable); // THEN then(throwable).isSameAs(RUNTIME_EXCEPTION); } - private static Stream, RuntimeException>> catchThrowableOfTypes() { + private static Stream, ThrowingCallable, RuntimeException>> catchThrowableOfTypes() { return Stream.of(Assertions::catchThrowableOfType, BDDAssertions::catchThrowableOfType, withAssertions::catchThrowableOfType); } From 6631aa3b624573138fd63b7a595ab6bf881849e8 Mon Sep 17 00:00:00 2001 From: java-coding-prodigy Date: Sun, 13 Nov 2022 12:38:26 +0530 Subject: [PATCH 06/12] Added the changes to BDDAssertions.java as well --- .../org/assertj/core/api/BDDAssertions.java | 53 +++++++++++++++++-- 1 file changed, 50 insertions(+), 3 deletions(-) diff --git a/assertj-core/src/main/java/org/assertj/core/api/BDDAssertions.java b/assertj-core/src/main/java/org/assertj/core/api/BDDAssertions.java index 51a1db9e07..3f8761f317 100644 --- a/assertj-core/src/main/java/org/assertj/core/api/BDDAssertions.java +++ b/assertj-core/src/main/java/org/assertj/core/api/BDDAssertions.java @@ -1843,7 +1843,7 @@ public static SpliteratorAssert then(Spliterator act *

* This caught {@link Throwable} can then be asserted. *

- * If you need to assert on the real type of Throwable caught (e.g. IOException), use {@link #catchThrowableOfType(ThrowingCallable, Class)}. + * If you need to assert on the real type of Throwable caught (e.g. IOException), use {@link #catchThrowableOfType(Class, ThrowingCallable)}. *

* Example: *

{@literal @}Test
@@ -1858,7 +1858,7 @@ public static  SpliteratorAssert then(Spliterator act
    *
    * @param shouldRaiseThrowable The lambda with the code that should raise the exception.
    * @return The captured exception or null if none was raised by the callable.
-   * @see #catchThrowableOfType(ThrowingCallable, Class)
+   * @see #catchThrowableOfType(Class, ThrowingCallable)
    *
    * @since 3.20.0
    */
@@ -1867,6 +1867,7 @@ public static Throwable catchThrowable(ThrowingCallable shouldRaiseThrowable) {
   }
 
   /**
+   * @deprecated use {@link #catchThrowableOfType(Class, ThrowingCallable)} instead.
    * Allows catching a {@link Throwable} of a specific type.
    * 

* A call is made to {@code catchThrowable(ThrowingCallable)}, if no exception is thrown it returns null @@ -1905,9 +1906,55 @@ public static Throwable catchThrowable(ThrowingCallable shouldRaiseThrowable) { * * @since 3.20.0 */ + + @Deprecated public static THROWABLE catchThrowableOfType(ThrowingCallable shouldRaiseThrowable, Class type) { - return AssertionsForClassTypes.catchThrowableOfType(shouldRaiseThrowable, type); + return AssertionsForClassTypes.catchThrowableOfType(type, shouldRaiseThrowable); + } + + /** + * Allows catching a {@link Throwable} of a specific type. + *

+ * A call is made to {@code catchThrowable(ThrowingCallable)}, if no exception is thrown it returns null + * otherwise it checks that the caught {@link Throwable} has the specified type and casts it making it convenient to perform subtype-specific assertions on it. + *

+ * Example: + *

 class TextException extends Exception {
+   *   int line;
+   *   int column;
+   *
+   *   public TextException(String msg, int line, int column) {
+   *     super(msg);
+   *     this.line = line;
+   *     this.column = column;
+   *   }
+   * }
+   *
+   * TextException textException = catchThrowableOfType(TextException.class() ,
+   *                                                    -> { throw new TextException("boom!", 1, 5); });
+   * // assertions succeed
+   * assertThat(textException).hasMessage("boom!");
+   * assertThat(textException.line).isEqualTo(1);
+   * assertThat(textException.column).isEqualTo(5);
+   *
+   * // succeeds as catchThrowableOfType returns null when the code does not thrown any exceptions
+   * assertThat(catchThrowableOfType(Exception.class, () -> {})).isNull();
+   *
+   * // fails as TextException is not a RuntimeException
+   * catchThrowableOfType(() -> { throw new TextException("boom!", 1, 5); }, RuntimeException.class);
+ * + * @param the {@link Throwable} type. + * @param shouldRaiseThrowable The lambda with the code that should raise the exception. + * @param type The type of exception that the code is expected to raise. + * @return The captured exception or null if none was raised by the callable. + * @see #catchThrowable(ThrowingCallable) + * + * @since 3.23.11 + */ + public static THROWABLE catchThrowableOfType(Class type, + ThrowingCallable shouldRaiseThrowable) { + return AssertionsForClassTypes.catchThrowableOfType(type, shouldRaiseThrowable); } /** From dcbe4edcfe477f82dc152ebb4f7942eec4a49be0 Mon Sep 17 00:00:00 2001 From: java-coding-prodigy Date: Sun, 13 Nov 2022 12:45:09 +0530 Subject: [PATCH 07/12] Documentation Fix --- .../src/main/java/org/assertj/core/api/Assertions.java | 3 ++- .../java/org/assertj/core/api/AssertionsForClassTypes.java | 1 + .../src/main/java/org/assertj/core/api/BDDAssertions.java | 1 + .../src/main/java/org/assertj/core/api/Java6Assertions.java | 1 + .../src/main/java/org/assertj/core/api/WithAssertions.java | 1 + 5 files changed, 6 insertions(+), 1 deletion(-) diff --git a/assertj-core/src/main/java/org/assertj/core/api/Assertions.java b/assertj-core/src/main/java/org/assertj/core/api/Assertions.java index 7296e66d94..af5c679a4c 100644 --- a/assertj-core/src/main/java/org/assertj/core/api/Assertions.java +++ b/assertj-core/src/main/java/org/assertj/core/api/Assertions.java @@ -1371,7 +1371,7 @@ public static ObjectAssert assertWith(T actual, Consumer... requiremen * * @param shouldRaiseThrowable The lambda with the code that should raise the exception. * @return The captured exception or null if none was raised by the callable. - * @see #catchThrowableOfType(ThrowingCallable, Class) + * @see #catchThrowableOfType(Class, ThrowingCallable) */ public static Throwable catchThrowable(ThrowingCallable shouldRaiseThrowable) { return AssertionsForClassTypes.catchThrowable(shouldRaiseThrowable); @@ -1379,6 +1379,7 @@ public static Throwable catchThrowable(ThrowingCallable shouldRaiseThrowable) { /** * @deprecated use {@link #catchThrowableOfType(Class, ThrowingCallable)} instead. + *

* Allows catching a {@link Throwable} of a specific type. *

* A call is made to {@code catchThrowable(ThrowingCallable)}, if no exception is thrown it returns null diff --git a/assertj-core/src/main/java/org/assertj/core/api/AssertionsForClassTypes.java b/assertj-core/src/main/java/org/assertj/core/api/AssertionsForClassTypes.java index 152585d8f0..466915400f 100644 --- a/assertj-core/src/main/java/org/assertj/core/api/AssertionsForClassTypes.java +++ b/assertj-core/src/main/java/org/assertj/core/api/AssertionsForClassTypes.java @@ -905,6 +905,7 @@ public static Throwable catchThrowable(ThrowingCallable shouldRaiseThrowable) { /** * @deprecated use {@link #catchThrowableOfType(Class, ThrowingCallable)} instead. + *

* Allows catching a {@link Throwable} of a specific type. *

* A call is made to {@code catchThrowable(ThrowingCallable)}, if no exception is thrown {@code catchThrowableOfType} returns null, diff --git a/assertj-core/src/main/java/org/assertj/core/api/BDDAssertions.java b/assertj-core/src/main/java/org/assertj/core/api/BDDAssertions.java index 3f8761f317..331fe196c3 100644 --- a/assertj-core/src/main/java/org/assertj/core/api/BDDAssertions.java +++ b/assertj-core/src/main/java/org/assertj/core/api/BDDAssertions.java @@ -1868,6 +1868,7 @@ public static Throwable catchThrowable(ThrowingCallable shouldRaiseThrowable) { /** * @deprecated use {@link #catchThrowableOfType(Class, ThrowingCallable)} instead. + *

* Allows catching a {@link Throwable} of a specific type. *

* A call is made to {@code catchThrowable(ThrowingCallable)}, if no exception is thrown it returns null diff --git a/assertj-core/src/main/java/org/assertj/core/api/Java6Assertions.java b/assertj-core/src/main/java/org/assertj/core/api/Java6Assertions.java index c0df0905c3..9bd975a0d2 100644 --- a/assertj-core/src/main/java/org/assertj/core/api/Java6Assertions.java +++ b/assertj-core/src/main/java/org/assertj/core/api/Java6Assertions.java @@ -1264,6 +1264,7 @@ public static Throwable catchThrowable(ThrowingCallable shouldRaiseThrowable) { /** * @deprecated use {@link #catchThrowableOfType(Class, ThrowingCallable)} instead. + *

* Allows catching a {@link Throwable} of a specific type. *

* A call is made to {@code catchThrowable(ThrowingCallable)}, if no exception is thrown {@code catchThrowableOfType} returns null, diff --git a/assertj-core/src/main/java/org/assertj/core/api/WithAssertions.java b/assertj-core/src/main/java/org/assertj/core/api/WithAssertions.java index 9064267f91..5f334dbdc6 100644 --- a/assertj-core/src/main/java/org/assertj/core/api/WithAssertions.java +++ b/assertj-core/src/main/java/org/assertj/core/api/WithAssertions.java @@ -2842,6 +2842,7 @@ default Throwable catchThrowable(final ThrowingCallable shouldRaiseThrowable) { /** * @deprecated use {@link #catchThrowableOfType(Class, ThrowingCallable)} instead. + *

* Allows catching a {@link Throwable} of a specific type. *

* A call is made to {@code catchThrowable(ThrowingCallable)}, if no exception is thrown {@code catchThrowableOfType} returns null, From a3695c045ab0e87c4d5a1728933c948adf689e52 Mon Sep 17 00:00:00 2001 From: java-coding-prodigy Date: Wed, 8 Feb 2023 22:01:52 +0530 Subject: [PATCH 08/12] Fixed arguement order --- .../java/org/assertj/core/api/AssertionsForClassTypes.java | 4 ++-- .../src/main/java/org/assertj/core/api/BDDAssertions.java | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/assertj-core/src/main/java/org/assertj/core/api/AssertionsForClassTypes.java b/assertj-core/src/main/java/org/assertj/core/api/AssertionsForClassTypes.java index 1270f07bc6..e80dc1274b 100644 --- a/assertj-core/src/main/java/org/assertj/core/api/AssertionsForClassTypes.java +++ b/assertj-core/src/main/java/org/assertj/core/api/AssertionsForClassTypes.java @@ -956,8 +956,8 @@ public static THROWABLE catchThrowableOfType(Throw * } * } * - * CustomParseException e = catchThrowableOfType(() -> { throw new CustomParseException("boom!", 1, 5); }, - * CustomParseException.class); + * CustomParseException e = catchThrowableOfType(CustomParseException.class, + * () -> { throw new CustomParseException("boom!", 1, 5); }); * // assertions pass * assertThat(e).hasMessageContaining("boom"); * assertThat(e.line).isEqualTo(1); diff --git a/assertj-core/src/main/java/org/assertj/core/api/BDDAssertions.java b/assertj-core/src/main/java/org/assertj/core/api/BDDAssertions.java index 3c2b55563f..e8a31173a4 100644 --- a/assertj-core/src/main/java/org/assertj/core/api/BDDAssertions.java +++ b/assertj-core/src/main/java/org/assertj/core/api/BDDAssertions.java @@ -1932,7 +1932,7 @@ public static THROWABLE catchThrowableOfType(Throw * assertThat(catchThrowableOfType(Exception.class, () -> {})).isNull(); * * // fails as TextException is not a RuntimeException - * catchThrowableOfType(() -> { throw new TextException("boom!", 1, 5); }, RuntimeException.class);

+ * catchThrowableOfType(RuntimeException.class, () -> { throw new TextException("boom!", 1, 5); });
* * @param the {@link Throwable} type. * @param shouldRaiseThrowable The lambda with the code that should raise the exception. From d476cf88a56f68ee5cc6c71f57d9f426364b1919 Mon Sep 17 00:00:00 2001 From: Stefano Cordio Date: Fri, 10 May 2024 12:27:52 +0200 Subject: [PATCH 09/12] Apply code review --- .../java/org/assertj/core/api/Assertions.java | 12 +++++------ .../core/api/AssertionsForClassTypes.java | 17 +++++++-------- .../org/assertj/core/api/BDDAssertions.java | 14 +++++-------- .../org/assertj/core/api/Java6Assertions.java | 7 +++---- .../org/assertj/core/api/ThrowableAssert.java | 12 ++++------- .../org/assertj/core/api/WithAssertions.java | 21 ++++++++++--------- .../Assertions_catchThrowableOfType_Test.java | 16 +++++++------- 7 files changed, 44 insertions(+), 55 deletions(-) diff --git a/assertj-core/src/main/java/org/assertj/core/api/Assertions.java b/assertj-core/src/main/java/org/assertj/core/api/Assertions.java index 6d10b4095d..d9af155fed 100644 --- a/assertj-core/src/main/java/org/assertj/core/api/Assertions.java +++ b/assertj-core/src/main/java/org/assertj/core/api/Assertions.java @@ -1345,7 +1345,8 @@ public static ObjectAssert assertWith(T actual, Consumer... requiremen *

* This caught {@link Throwable} can then be asserted. *

- * If you need to assert on the real type of Throwable caught (e.g. IOException), use {@link #catchThrowableOfType(Class, ThrowingCallable)}. + * If you need to assert on the real type of Throwable caught (e.g. IOException), use + * {@link #catchThrowableOfType(Class, ThrowingCallable)}. *

* Example: *

{@literal @}Test
@@ -1367,8 +1368,6 @@ public static Throwable catchThrowable(ThrowingCallable shouldRaiseThrowable) {
   }
 
   /**
-   * @deprecated use {@link #catchThrowableOfType(Class, ThrowingCallable)} instead.
-   * 

* Allows catching a {@link Throwable} of a specific type. *

* A call is made to {@code catchThrowable(ThrowingCallable)}, if no exception is thrown it returns null @@ -1405,11 +1404,12 @@ public static Throwable catchThrowable(ThrowingCallable shouldRaiseThrowable) { * @return The captured exception or null if none was raised by the callable. * @see #catchThrowable(ThrowingCallable) * @since 3.9.0 + * @deprecated use {@link #catchThrowableOfType(Class, ThrowingCallable)} instead. */ @Deprecated public static THROWABLE catchThrowableOfType(ThrowingCallable shouldRaiseThrowable, Class type) { - return AssertionsForClassTypes.catchThrowableOfType(type, shouldRaiseThrowable); + return catchThrowableOfType(type, shouldRaiseThrowable); } /** @@ -1448,11 +1448,11 @@ public static THROWABLE catchThrowableOfType(Throw * @param type The type of exception that the code is expected to raise. * @return The captured exception or null if none was raised by the callable. * @see #catchThrowable(ThrowingCallable) - * @since 3.23.11 + * @since 3.26.0 */ public static THROWABLE catchThrowableOfType(Class type, ThrowingCallable shouldRaiseThrowable) { - return AssertionsForClassTypes.catchThrowableOfType(type, shouldRaiseThrowable); + return ThrowableAssert.catchThrowableOfType(type, shouldRaiseThrowable); } /** * Allows catching an instance of {@link Exception}. diff --git a/assertj-core/src/main/java/org/assertj/core/api/AssertionsForClassTypes.java b/assertj-core/src/main/java/org/assertj/core/api/AssertionsForClassTypes.java index e80dc1274b..aec34e818d 100644 --- a/assertj-core/src/main/java/org/assertj/core/api/AssertionsForClassTypes.java +++ b/assertj-core/src/main/java/org/assertj/core/api/AssertionsForClassTypes.java @@ -786,7 +786,7 @@ public static AbstractThrowableAssert assertThat(T a /** * Entry point to check that an exception of type T is thrown by a given {@code throwingCallable} - * which allows to chain assertions on the thrown exception. + * which allows chaining assertions on the thrown exception. *

* Example: *

 assertThatExceptionOfType(IOException.class)
@@ -819,7 +819,7 @@ public static NotThrownAssert assertThatNoException() {
   }
 
   /**
-   * Allows to capture and then assert on a {@link Throwable} more easily when used with Java 8 lambdas.
+   * Allows capturing and then assert on a {@link Throwable} more easily when used with Java 8 lambdas.
    *
    * 

* Example : @@ -893,8 +893,6 @@ public static Throwable catchThrowable(ThrowingCallable shouldRaiseThrowable) { } /** - * @deprecated use {@link #catchThrowableOfType(Class, ThrowingCallable)} instead. - *

* Allows catching a {@link Throwable} of a specific type. *

* A call is made to {@code catchThrowable(ThrowingCallable)}, if no exception is thrown {@code catchThrowableOfType} returns null, @@ -930,11 +928,12 @@ public static Throwable catchThrowable(ThrowingCallable shouldRaiseThrowable) { * @return The captured exception or null if none was raised by the callable. * @see #catchThrowable(ThrowableAssert.ThrowingCallable) * @since 3.9.0 + * @deprecated use {@link #catchThrowableOfType(Class, ThrowingCallable)} instead. */ @Deprecated public static THROWABLE catchThrowableOfType(ThrowingCallable shouldRaiseThrowable, Class type) { - return ThrowableAssert.catchThrowableOfType(type, shouldRaiseThrowable); + return catchThrowableOfType(type, shouldRaiseThrowable); } /** @@ -972,7 +971,7 @@ public static THROWABLE catchThrowableOfType(Throw * @param type The type of exception that the code is expected to raise. * @return The captured exception or null if none was raised by the callable. * @see #catchThrowable(ThrowableAssert.ThrowingCallable) - * @since 3.23.11 + * @since 3.26.0 */ public static THROWABLE catchThrowableOfType(Class type, ThrowingCallable shouldRaiseThrowable) { @@ -1018,7 +1017,7 @@ public static void fail(String failureMessage, Throwable realCause) { /** * Only delegate to {@link Fail#failBecauseExceptionWasNotThrown(Class)} so that Assertions offers a full feature * entry point to all AssertJ features (but you can use Fail if you prefer). - * + *

* {@link Assertions#shouldHaveThrown(Class)} can be used as a replacement. * * @param throwableClass the Throwable class that was expected to be thrown. @@ -1886,7 +1885,7 @@ public static void setLenientDateParsing(boolean value) { * User date formats are used before default ones in the order they have been registered (first registered, first * used). *

- * AssertJ is gonna use any date formats registered with one of these methods : + * AssertJ is going to use any date formats registered with one of these methods : *

    *
  • {@link org.assertj.core.api.AbstractDateAssert#withDateFormat(String)}
  • *
  • {@link org.assertj.core.api.AbstractDateAssert#withDateFormat(java.text.DateFormat)}
  • @@ -1932,7 +1931,7 @@ public static void registerCustomDateFormat(DateFormat userCustomDateFormat) { * User date formats are used before default ones in the order they have been registered (first registered, first * used). *

    - * AssertJ is gonna use any date formats registered with one of these methods : + * AssertJ is going to use any date formats registered with one of these methods : *

      *
    • {@link org.assertj.core.api.AbstractDateAssert#withDateFormat(String)}
    • *
    • {@link org.assertj.core.api.AbstractDateAssert#withDateFormat(java.text.DateFormat)}
    • diff --git a/assertj-core/src/main/java/org/assertj/core/api/BDDAssertions.java b/assertj-core/src/main/java/org/assertj/core/api/BDDAssertions.java index e8a31173a4..9f7bf93f89 100644 --- a/assertj-core/src/main/java/org/assertj/core/api/BDDAssertions.java +++ b/assertj-core/src/main/java/org/assertj/core/api/BDDAssertions.java @@ -1813,7 +1813,7 @@ public static ListAssert then(IntStream actual) { /** * Creates a new instance of {@link SpliteratorAssert} from the given {@link Spliterator}. - * + *

      * Example: *

       Spliterator<Integer> spliterator = Stream.of(1, 2, 3).spliterator();
          * then(spliterator).hasCharacteristics(Spliterator.SIZED); 
      @@ -1856,8 +1856,6 @@ public static Throwable catchThrowable(ThrowingCallable shouldRaiseThrowable) { } /** - * @deprecated use {@link #catchThrowableOfType(Class, ThrowingCallable)} instead. - *

      * Allows catching a {@link Throwable} of a specific type. *

      * A call is made to {@code catchThrowable(ThrowingCallable)}, if no exception is thrown it returns null @@ -1893,14 +1891,13 @@ public static Throwable catchThrowable(ThrowingCallable shouldRaiseThrowable) { * @param type The type of exception that the code is expected to raise. * @return The captured exception or null if none was raised by the callable. * @see #catchThrowable(ThrowingCallable) - * * @since 3.20.0 + * @deprecated use {@link #catchThrowableOfType(Class, ThrowingCallable)} instead. */ - @Deprecated public static THROWABLE catchThrowableOfType(ThrowingCallable shouldRaiseThrowable, Class type) { - return AssertionsForClassTypes.catchThrowableOfType(type, shouldRaiseThrowable); + return catchThrowableOfType(type, shouldRaiseThrowable); } /** @@ -1939,12 +1936,11 @@ public static THROWABLE catchThrowableOfType(Throw * @param type The type of exception that the code is expected to raise. * @return The captured exception or null if none was raised by the callable. * @see #catchThrowable(ThrowingCallable) - * - * @since 3.23.11 + * @since 3.26.0 */ public static THROWABLE catchThrowableOfType(Class type, ThrowingCallable shouldRaiseThrowable) { - return AssertionsForClassTypes.catchThrowableOfType(type, shouldRaiseThrowable); + return ThrowableAssert.catchThrowableOfType(type, shouldRaiseThrowable); } /** diff --git a/assertj-core/src/main/java/org/assertj/core/api/Java6Assertions.java b/assertj-core/src/main/java/org/assertj/core/api/Java6Assertions.java index 38e07dccec..eb28400a26 100644 --- a/assertj-core/src/main/java/org/assertj/core/api/Java6Assertions.java +++ b/assertj-core/src/main/java/org/assertj/core/api/Java6Assertions.java @@ -1252,8 +1252,6 @@ public static Throwable catchThrowable(ThrowingCallable shouldRaiseThrowable) { } /** - * @deprecated use {@link #catchThrowableOfType(Class, ThrowingCallable)} instead. - *

      * Allows catching a {@link Throwable} of a specific type. *

      * A call is made to {@code catchThrowable(ThrowingCallable)}, if no exception is thrown {@code catchThrowableOfType} returns null, @@ -1289,10 +1287,11 @@ public static Throwable catchThrowable(ThrowingCallable shouldRaiseThrowable) { * @return The captured exception or null if none was raised by the callable. * @see #catchThrowable(ThrowableAssert.ThrowingCallable) * @since 3.9.0 + * @deprecated use {@link #catchThrowableOfType(Class, ThrowingCallable)} instead. */ @Deprecated public static THROWABLE catchThrowableOfType(ThrowingCallable shouldRaiseThrowable, Class type) { - return ThrowableAssert.catchThrowableOfType(type, shouldRaiseThrowable); + return catchThrowableOfType(type, shouldRaiseThrowable); } /** @@ -1330,7 +1329,7 @@ public static THROWABLE catchThrowableOfType(Throw * @param type The type of exception that the code is expected to raise. * @return The captured exception or null if none was raised by the callable. * @see #catchThrowable(ThrowableAssert.ThrowingCallable) - * @since 3.23.11 + * @since 3.26.0 */ public static THROWABLE catchThrowableOfType(Class type, ThrowingCallable shouldRaiseThrowable) { return ThrowableAssert.catchThrowableOfType(type, shouldRaiseThrowable); diff --git a/assertj-core/src/main/java/org/assertj/core/api/ThrowableAssert.java b/assertj-core/src/main/java/org/assertj/core/api/ThrowableAssert.java index 8a97be837e..8c71ce8b17 100644 --- a/assertj-core/src/main/java/org/assertj/core/api/ThrowableAssert.java +++ b/assertj-core/src/main/java/org/assertj/core/api/ThrowableAssert.java @@ -67,25 +67,21 @@ public static Throwable catchThrowable(ThrowingCallable shouldRaiseThrowable) { return null; } - @SuppressWarnings("unchecked") @Deprecated public static THROWABLE catchThrowableOfType(ThrowingCallable shouldRaiseThrowable, Class type) { - Throwable throwable = catchThrowable(shouldRaiseThrowable); - if (throwable == null) return null; - // check exception type - new ThrowableAssert(throwable).overridingErrorMessage(shouldBeInstance(throwable, type).create()) - .isInstanceOf(type); - return (THROWABLE) throwable; + return catchThrowableOfType(type, shouldRaiseThrowable); } + @SuppressWarnings("unchecked") public static THROWABLE catchThrowableOfType(Class type, ThrowingCallable shouldRaiseThrowable) { Throwable throwable = catchThrowable(shouldRaiseThrowable); if (throwable == null) return null; // check exception type - new ThrowableAssert(throwable).overridingErrorMessage(shouldBeInstance(throwable, type).create()) + new ThrowableAssert<>(throwable).overridingErrorMessage(shouldBeInstance(throwable, type).create()) .isInstanceOf(type); return (THROWABLE) throwable; } + } diff --git a/assertj-core/src/main/java/org/assertj/core/api/WithAssertions.java b/assertj-core/src/main/java/org/assertj/core/api/WithAssertions.java index d6aee22622..63ac881ca4 100644 --- a/assertj-core/src/main/java/org/assertj/core/api/WithAssertions.java +++ b/assertj-core/src/main/java/org/assertj/core/api/WithAssertions.java @@ -2830,8 +2830,6 @@ default Throwable catchThrowable(final ThrowingCallable shouldRaiseThrowable) { } /** - * @deprecated use {@link #catchThrowableOfType(Class, ThrowingCallable)} instead. - *

      * Allows catching a {@link Throwable} of a specific type. *

      * A call is made to {@code catchThrowable(ThrowingCallable)}, if no exception is thrown {@code catchThrowableOfType} returns null, @@ -2867,11 +2865,12 @@ default Throwable catchThrowable(final ThrowingCallable shouldRaiseThrowable) { * @return The captured exception or null if none was raised by the callable. * @see #catchThrowable(ThrowingCallable) * @since 3.9.0 + * @deprecated use {@link #catchThrowableOfType(Class, ThrowingCallable)} instead. */ @Deprecated - default THROWABLE catchThrowableOfType(final ThrowingCallable shouldRaiseThrowable, - final Class type) { - return Assertions.catchThrowableOfType(type, shouldRaiseThrowable); + default THROWABLE catchThrowableOfType(ThrowingCallable shouldRaiseThrowable, + Class type) { + return catchThrowableOfType(type, shouldRaiseThrowable); } /** @@ -2909,12 +2908,13 @@ default THROWABLE catchThrowableOfType(final Throw * @param type The type of exception that the code is expected to raise. * @return The captured exception or null if none was raised by the callable. * @see #catchThrowable(ThrowingCallable) - * @since 3.23.11 + * @since 3.26.0 */ - default THROWABLE catchThrowableOfType(final Class type, - final ThrowingCallable shouldRaiseThrowable) { - return Assertions.catchThrowableOfType(type, shouldRaiseThrowable); + default THROWABLE catchThrowableOfType(Class type, + ThrowingCallable shouldRaiseThrowable) { + return ThrowableAssert.catchThrowableOfType(type, shouldRaiseThrowable); } + /** * Allows catching an instance of {@link Exception}. *

      @@ -3074,7 +3074,8 @@ default IOException catchIOException(ThrowingCallable shouldRaiseIOException) { * @since 3.22.0 */ default ReflectiveOperationException catchReflectiveOperationException(ThrowingCallable shouldRaiseReflectiveOperationException) { - return AssertionsForClassTypes.catchThrowableOfType(shouldRaiseReflectiveOperationException, ReflectiveOperationException.class); + return AssertionsForClassTypes.catchThrowableOfType(shouldRaiseReflectiveOperationException, + ReflectiveOperationException.class); } /** diff --git a/assertj-core/src/test/java/org/assertj/core/api/Assertions_catchThrowableOfType_Test.java b/assertj-core/src/test/java/org/assertj/core/api/Assertions_catchThrowableOfType_Test.java index 165347dd2a..d91c6591d5 100644 --- a/assertj-core/src/test/java/org/assertj/core/api/Assertions_catchThrowableOfType_Test.java +++ b/assertj-core/src/test/java/org/assertj/core/api/Assertions_catchThrowableOfType_Test.java @@ -12,8 +12,6 @@ */ package org.assertj.core.api; -import static org.assertj.core.api.Assertions.assertThat; -import static org.assertj.core.api.Assertions.catchThrowable; import static org.assertj.core.api.Assertions.catchThrowableOfType; import static org.assertj.core.api.Assertions_catchThrowable_Test.codeThrowing; import static org.assertj.core.api.BDDAssertions.then; @@ -32,7 +30,7 @@ void can_capture_exception_and_then_assert_following_AAA_or_BDD_style() { // GIVEN Exception exception = new Exception("boom!!"); // WHEN - Throwable boom = catchThrowable(codeThrowing(exception)); + Throwable boom = catchThrowableOfType(Exception.class, codeThrowing(exception)); // THEN then(boom).isSameAs(exception); } @@ -40,7 +38,7 @@ void can_capture_exception_and_then_assert_following_AAA_or_BDD_style() { @Test void catchThrowable_returns_null_when_no_exception_thrown() { // WHEN - Throwable boom = catchThrowable(() -> {}); + Throwable boom = catchThrowableOfType(RuntimeException.class, () -> {}); // THEN then(boom).isNull(); } @@ -50,9 +48,9 @@ void catchThrowableOfType_should_fail_with_good_message_if_wrong_type() { // GIVEN ThrowingCallable code = () -> catchThrowableOfType(RuntimeException.class, raisingException("boom!!")); // WHEN - AssertionError assertionError = expectAssertionError(code); + AssertionError error = expectAssertionError(code); // THEN - assertThat(assertionError).hasMessageContainingAll(RuntimeException.class.getName(), Exception.class.getName()); + then(error).hasMessageContainingAll(RuntimeException.class.getName(), Exception.class.getName()); } @Test @@ -60,7 +58,7 @@ void catchThrowableOfType_should_succeed_and_return_actual_instance_with_correct // GIVEN final Exception expected = new RuntimeException("boom!!"); // WHEN - Exception actual = catchThrowableOfType(codeThrowing(expected), Exception.class); + Exception actual = catchThrowableOfType(Exception.class, codeThrowing(expected)); // THEN then(actual).isSameAs(expected); } @@ -68,7 +66,7 @@ void catchThrowableOfType_should_succeed_and_return_actual_instance_with_correct @Test void catchThrowableOfType_should_succeed_and_return_null_if_no_exception_thrown() { // WHEN - IOException actual = catchThrowableOfType(() -> {}, IOException.class); + IOException actual = catchThrowableOfType(IOException.class, () -> {}); // THEN then(actual).isNull(); } @@ -78,7 +76,7 @@ void should_catch_mocked_throwable() { // GIVEN Throwable throwable = mock(Throwable.class); // WHEN - Throwable actual = catchThrowableOfType(codeThrowing(throwable), Throwable.class); + Throwable actual = catchThrowableOfType(Throwable.class, codeThrowing(throwable)); // THEN then(actual).isSameAs(throwable); } From f261e8e25741cc17de9cbfbaaba3d604a53a00d4 Mon Sep 17 00:00:00 2001 From: Stefano Cordio Date: Fri, 10 May 2024 12:28:29 +0200 Subject: [PATCH 10/12] Add Kotlin test --- .../kotlin/Assertions_assertThat_Test.kt | 7 ++-- .../Assertions_catchThrowableOfType_Test.kt | 33 +++++++++++++++++++ ...sertions_describedAs_text_supplier_Test.kt | 23 +++++++------ 3 files changed, 48 insertions(+), 15 deletions(-) create mode 100644 assertj-tests/assertj-integration-tests/assertj-core-kotlin/src/test/kotlin/org/assertj/core/tests/kotlin/Assertions_catchThrowableOfType_Test.kt diff --git a/assertj-tests/assertj-integration-tests/assertj-core-kotlin/src/test/kotlin/org/assertj/core/tests/kotlin/Assertions_assertThat_Test.kt b/assertj-tests/assertj-integration-tests/assertj-core-kotlin/src/test/kotlin/org/assertj/core/tests/kotlin/Assertions_assertThat_Test.kt index 6851ff7584..41d2f728fc 100644 --- a/assertj-tests/assertj-integration-tests/assertj-core-kotlin/src/test/kotlin/org/assertj/core/tests/kotlin/Assertions_assertThat_Test.kt +++ b/assertj-tests/assertj-integration-tests/assertj-core-kotlin/src/test/kotlin/org/assertj/core/tests/kotlin/Assertions_assertThat_Test.kt @@ -18,13 +18,13 @@ import org.junit.jupiter.api.Test internal class Assertions_assertThat_Test { @Test - internal fun intarray() { + fun intarray() { val x: IntArray = intArrayOf(1, 2, 3) assertThat(x).contains(1, 2, 3) } @Test - internal fun `immutable list`() { + fun `immutable list`() { val list = listOf("Viserys", "Rhaenyra", "Daemon") assertThat(list).contains("Viserys", "Rhaenyra", "Daemon") assertThat(list).hasSize(3).anySatisfy { @@ -34,8 +34,9 @@ internal class Assertions_assertThat_Test { } @Test - internal fun `mutable list`() { + fun `mutable list`() { val list = mutableListOf("Viserys", "Rhaenyra", "Daemon") assertThat(list).contains("Viserys", "Rhaenyra", "Daemon") } + } diff --git a/assertj-tests/assertj-integration-tests/assertj-core-kotlin/src/test/kotlin/org/assertj/core/tests/kotlin/Assertions_catchThrowableOfType_Test.kt b/assertj-tests/assertj-integration-tests/assertj-core-kotlin/src/test/kotlin/org/assertj/core/tests/kotlin/Assertions_catchThrowableOfType_Test.kt new file mode 100644 index 0000000000..cb50d41945 --- /dev/null +++ b/assertj-tests/assertj-integration-tests/assertj-core-kotlin/src/test/kotlin/org/assertj/core/tests/kotlin/Assertions_catchThrowableOfType_Test.kt @@ -0,0 +1,33 @@ +/* + * 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. + * + * Copyright 2012-2023 the original author or authors. + */ +package org.assertj.core.tests.kotlin + +import org.assertj.core.api.Assertions.catchThrowableOfType +import org.assertj.core.api.BDDAssertions.then +import org.junit.jupiter.api.Test + +internal class Assertions_catchThrowableOfType_Test { + + @Test + fun `should work with lambda expressions`() { + // GIVEN + val exception = Exception("boom!!") + // WHEN + val thrown = catchThrowableOfType(Exception::class.java) { + throw exception + } + // THEN + then(thrown).isSameAs(exception) + } + +} diff --git a/assertj-tests/assertj-integration-tests/assertj-core-kotlin/src/test/kotlin/org/assertj/core/tests/kotlin/Assertions_describedAs_text_supplier_Test.kt b/assertj-tests/assertj-integration-tests/assertj-core-kotlin/src/test/kotlin/org/assertj/core/tests/kotlin/Assertions_describedAs_text_supplier_Test.kt index 13cfd308e7..3c829a703a 100644 --- a/assertj-tests/assertj-integration-tests/assertj-core-kotlin/src/test/kotlin/org/assertj/core/tests/kotlin/Assertions_describedAs_text_supplier_Test.kt +++ b/assertj-tests/assertj-integration-tests/assertj-core-kotlin/src/test/kotlin/org/assertj/core/tests/kotlin/Assertions_describedAs_text_supplier_Test.kt @@ -14,7 +14,7 @@ package org.assertj.core.tests.kotlin import org.assertj.core.api.Assertions import org.assertj.core.api.Assertions.assertThat -import org.assertj.core.api.BDDAssertions +import org.assertj.core.api.BDDAssertions.then import org.assertj.core.api.ConcreteAssert import org.assertj.core.util.AssertionsUtil import org.junit.jupiter.api.Test @@ -28,29 +28,28 @@ import java.util.function.Supplier internal class Assertions_describedAs_text_supplier_Test { @Test - fun descriptionText_should_evaluate_lazy_description() { + fun `descriptionText should evaluate lazy description`() { // GIVEN val assertions = ConcreteAssert("foo") // WHEN assertions.describedAs { "description" } // THEN - BDDAssertions.then(assertions.descriptionText()).isEqualTo("description") + then(assertions.descriptionText()).isEqualTo("description") } - @Test - fun should_not_evaluate_description_when_assertion_succeeds() { + fun `should not evaluate description when assertion succeeds`() { // GIVEN val evaluated = AtomicBoolean(false) val descriptionSupplier = spiedSupplier(evaluated) // WHEN assertThat(true).describedAs(descriptionSupplier).isTrue // THEN - BDDAssertions.then(evaluated).isFalse + then(evaluated).isFalse } @Test - fun should_evaluate_description_when_assertion_fails() { + fun `should evaluate description when assertion fails`() { // GIVEN val evaluated = AtomicBoolean(false) val descriptionSupplier = spiedSupplier(evaluated) @@ -59,21 +58,21 @@ internal class Assertions_describedAs_text_supplier_Test { assertThat(true).describedAs(descriptionSupplier).isFalse() } // THEN - BDDAssertions.then(evaluated).isTrue + then(evaluated).isTrue } @Test - fun should_return_this() { + fun `should return this`() { // GIVEN val assertions = ConcreteAssert("foo") // WHEN val returnedAssertions = assertions.describedAs { "description" } // THEN - BDDAssertions.then(returnedAssertions).isSameAs(assertions) + then(returnedAssertions).isSameAs(assertions) } @Test - fun should_throw_evaluate_lazy_description() { + fun `should throw evaluate lazy description`() { // GIVEN val assertions = ConcreteAssert("foo") val descriptionSupplier: Supplier? = null @@ -84,7 +83,7 @@ internal class Assertions_describedAs_text_supplier_Test { ).descriptionText() } // THEN - BDDAssertions.then(throwable).isInstanceOf(IllegalStateException::class.java) + then(throwable).isInstanceOf(IllegalStateException::class.java) .hasMessage("the descriptionSupplier should not be null") } From ceac01a4557ab0dd3bdc57a048360dce751fa171 Mon Sep 17 00:00:00 2001 From: Stefano Cordio Date: Fri, 10 May 2024 12:35:36 +0200 Subject: [PATCH 11/12] Spotless --- assertj-core/src/main/java/org/assertj/core/api/Assertions.java | 1 + .../main/java/org/assertj/core/api/AssertionsForClassTypes.java | 2 +- .../src/main/java/org/assertj/core/api/ThrowableAssert.java | 2 +- 3 files changed, 3 insertions(+), 2 deletions(-) diff --git a/assertj-core/src/main/java/org/assertj/core/api/Assertions.java b/assertj-core/src/main/java/org/assertj/core/api/Assertions.java index ff790fefaa..2e9c655961 100644 --- a/assertj-core/src/main/java/org/assertj/core/api/Assertions.java +++ b/assertj-core/src/main/java/org/assertj/core/api/Assertions.java @@ -1477,6 +1477,7 @@ public static THROWABLE catchThrowableOfType(Class ThrowingCallable shouldRaiseThrowable) { return ThrowableAssert.catchThrowableOfType(type, shouldRaiseThrowable); } + /** * Allows catching an instance of {@link Exception}. *

      diff --git a/assertj-core/src/main/java/org/assertj/core/api/AssertionsForClassTypes.java b/assertj-core/src/main/java/org/assertj/core/api/AssertionsForClassTypes.java index bdf2d8f8d5..eb7c8eb446 100644 --- a/assertj-core/src/main/java/org/assertj/core/api/AssertionsForClassTypes.java +++ b/assertj-core/src/main/java/org/assertj/core/api/AssertionsForClassTypes.java @@ -822,7 +822,7 @@ public static ThrowableTypeAssert assertThatExceptionOf *

      assertThatNoException().isThrownBy(() -> { System.out.println("OK"); });
      * * This method is more or less the same of {@code assertThatCode(...).doesNotThrowAnyException();} but in a more natural way. - + * * @return the created {@link NotThrownAssert}. * @since 3.17.0 */ diff --git a/assertj-core/src/main/java/org/assertj/core/api/ThrowableAssert.java b/assertj-core/src/main/java/org/assertj/core/api/ThrowableAssert.java index fc5677dbd0..40e02f432f 100644 --- a/assertj-core/src/main/java/org/assertj/core/api/ThrowableAssert.java +++ b/assertj-core/src/main/java/org/assertj/core/api/ThrowableAssert.java @@ -80,7 +80,7 @@ public static THROWABLE catchThrowableOfType(Class if (throwable == null) return null; // check exception type new ThrowableAssert<>(throwable).overridingErrorMessage(shouldBeInstance(throwable, type).create()) - .isInstanceOf(type); + .isInstanceOf(type); return (THROWABLE) throwable; } From e639b66d74feb905034ff9cbaae6a4d793d3556d Mon Sep 17 00:00:00 2001 From: Stefano Cordio Date: Fri, 10 May 2024 12:43:21 +0200 Subject: [PATCH 12/12] Fix license header --- .../core/tests/kotlin/Assertions_catchThrowableOfType_Test.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/assertj-tests/assertj-integration-tests/assertj-core-kotlin/src/test/kotlin/org/assertj/core/tests/kotlin/Assertions_catchThrowableOfType_Test.kt b/assertj-tests/assertj-integration-tests/assertj-core-kotlin/src/test/kotlin/org/assertj/core/tests/kotlin/Assertions_catchThrowableOfType_Test.kt index cb50d41945..7e166b6bb4 100644 --- a/assertj-tests/assertj-integration-tests/assertj-core-kotlin/src/test/kotlin/org/assertj/core/tests/kotlin/Assertions_catchThrowableOfType_Test.kt +++ b/assertj-tests/assertj-integration-tests/assertj-core-kotlin/src/test/kotlin/org/assertj/core/tests/kotlin/Assertions_catchThrowableOfType_Test.kt @@ -8,7 +8,7 @@ * 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. * - * Copyright 2012-2023 the original author or authors. + * Copyright 2012-2024 the original author or authors. */ package org.assertj.core.tests.kotlin