From a33478f57e970d396aa405a4b8198677169be212 Mon Sep 17 00:00:00 2001 From: java-coding-prodigy <85149944+java-coding-prodigy@users.noreply.github.com> Date: Fri, 10 May 2024 19:14:12 +0530 Subject: [PATCH] Deprecate `catchThrowableOfType(ThrowingCallable, Class)` in favor of `catchThrowableOfType(Class, ThrowingCallable)` (#2823) Co-authored-by: vlsi@users.noreply.github.com --- .../java/org/assertj/core/api/Assertions.java | 52 +++++++++++++++- .../core/api/AssertionsForClassTypes.java | 59 ++++++++++++++++--- .../org/assertj/core/api/BDDAssertions.java | 54 +++++++++++++++-- .../org/assertj/core/api/Java6Assertions.java | 44 +++++++++++++- .../org/assertj/core/api/ThrowableAssert.java | 13 +++- .../org/assertj/core/api/WithAssertions.java | 52 ++++++++++++++-- .../Assertions_catchThrowableOfType_Test.java | 18 +++--- ...tAssertions_catchThrowableOfType_Test.java | 6 +- .../kotlin/Assertions_assertThat_Test.kt | 7 ++- .../Assertions_catchThrowableOfType_Test.kt | 33 +++++++++++ ...sertions_describedAs_text_supplier_Test.kt | 22 +++---- 11 files changed, 309 insertions(+), 51 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-core/src/main/java/org/assertj/core/api/Assertions.java b/assertj-core/src/main/java/org/assertj/core/api/Assertions.java index e5f5c817a99..2e9c6559615 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 @@ -1368,7 +1368,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(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
@@ -1383,7 +1384,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);
@@ -1426,10 +1427,55 @@ 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(shouldRaiseThrowable, type);
+    return 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(() -> { 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.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/AssertionsForClassTypes.java b/assertj-core/src/main/java/org/assertj/core/api/AssertionsForClassTypes.java index e8c4c48e605..eb7c8eb446e 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 @@ -798,7 +798,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)
@@ -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 */ @@ -831,7 +831,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 : @@ -898,7 +898,7 @@ 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); @@ -940,12 +940,55 @@ 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(shouldRaiseThrowable, type); + return 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 {@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.26.0 + */ + 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. // ------------------------------------------------------------------------------------------------- @@ -1007,7 +1050,7 @@ public static void fail(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. @@ -1875,7 +1918,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)}
  • @@ -1921,7 +1964,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 17a79f9b856..dc39bb7cc03 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 @@ -1851,7 +1851,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); 
      @@ -1870,7 +1870,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
      @@ -1885,7 +1885,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
          */
      @@ -1929,12 +1929,56 @@ 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(shouldRaiseThrowable, type);
      +    return 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(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. + * @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.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/Java6Assertions.java b/assertj-core/src/main/java/org/assertj/core/api/Java6Assertions.java index 34c5d54c3b1..1cfee0f7cf1 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 @@ -1301,11 +1301,53 @@ 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(shouldRaiseThrowable, type); + return 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 {@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.26.0 + */ + 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 9d6b9057f34..40e02f432f6 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,14 +67,21 @@ public static Throwable catchThrowable(ThrowingCallable shouldRaiseThrowable) { return null; } - @SuppressWarnings("unchecked") + @Deprecated public static THROWABLE catchThrowableOfType(ThrowingCallable shouldRaiseThrowable, Class type) { + 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()) - .isInstanceOf(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 e3a95213b55..c18cf61f0ce 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 @@ -2904,7 +2904,7 @@ 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); @@ -2946,10 +2946,54 @@ 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. */ - default THROWABLE catchThrowableOfType(final ThrowingCallable shouldRaiseThrowable, - final Class type) { - return Assertions.catchThrowableOfType(shouldRaiseThrowable, type); + @Deprecated + default THROWABLE catchThrowableOfType(ThrowingCallable shouldRaiseThrowable, + Class type) { + return 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 {@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.26.0 + */ + default THROWABLE catchThrowableOfType(Class type, + ThrowingCallable shouldRaiseThrowable) { + return ThrowableAssert.catchThrowableOfType(type, shouldRaiseThrowable); } /** 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 b2d4eb15350..fdc10913a84 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(); } @@ -48,11 +46,11 @@ 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); + 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); } 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 1d4cb4f4748..35b751b4129 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); } 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 40ed820c095..0142a5bc952 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 00000000000..7e166b6bb49 --- /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-2024 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 36d2a7d72f8..6b65fbdf27b 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.tests.kotlin.testkit.AssertionsUtil.expectAssertionError import org.assertj.core.tests.kotlin.testkit.ConcreteAssert import org.junit.jupiter.api.Test @@ -28,28 +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) @@ -58,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 @@ -83,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") }