On Tue, 2 Mar 2021 18:07:24 GMT, Stuart Marks <sma...@openjdk.org> wrote:

>> test/jdk/jdk/internal/util/ArraysSupport/NewLength.java line 100:
>> 
>>> 98:             int r = ArraysSupport.newLength(old, min, pref);
>>> 99:             fail("expected OutOfMemoryError, got normal return value of 
>>> " + r);
>>> 100:         } catch (OutOfMemoryError success) { }
>> 
>> Consider using `expectThrows` or `assertThrows` from `org.testng.Assert`.
>
> Good point. However, I actually tried to use assertThrows, but there doesn't 
> seem to be a way to get the unexpected return value into the error message. I 
> think having this value in the test output is important for diagnosing 
> failures.

That tradeoff you made, makes sense.

Indeed, neither `assertThrows` nor `expectThrows` in `org.testng.Assert` logs 
an unexpectedly returned value. This is because the code being tested is 
expressed as the `Assert.ThrowingRunnable` interface which has a single `void` 
method. So the code being tested cannot return a value to the testing framework.

Now, if `org.testng.Assert` were to provide the respective methods accepting, 
say, `Assert.ThrowingCallable` that had a value-returning method, TestNG would 
run into the same issues that JUnit ran into when they attempted to do that: 
https://github.com/junit-team/junit5/issues/1576

-------------

PR: https://git.openjdk.java.net/jdk/pull/1617

Reply via email to