Usage of Java assertions in JTreg tests?
Alan Bateman
Alan.Bateman at oracle.com
Tue Nov 15 17:41:17 UTC 2022
On 15/11/2022 17:19, Volker Simonis wrote:
> Hi,
>
> I recently stumbled upon
> test/jdk/java/lang/invoke/JavaDocExamplesTest.java which uses a lot of
> Java assertions like this:
>
> for (int n = 0; n <= 10; n++) {
> Object[] badArityArgs = (n == 2 ? new Object[0] : new Object[n]);
> try { assert((boolean) eq2.invokeExact(badArityArgs) && false); }
> catch (IllegalArgumentException ex) { } // OK
> }
>
> The intent of this test is to verify that invokeExact() always throws
> an exception because otherwise the assertion will fire. Unfortunately,
> this test only does something at all if JVM is running with "-ea"
> which is not the default. In the standard configuration, all the code
> inside the "assert(..)" will just be skipped and the test always
> passes.
>
> This example (and a lot more under test/jdk/java/lang/invoke/) is
> there since JDK 7. The tests also use testNG assertions a lot (e.g.
> "assertEqual()", "assertTrue()", etc.) so I wonder if the plain java
> asserts shown in the example above are just typos and should be
> replaced by application level testNG assertions? What are these tests
> good for otherwise if they won't be executed most of the time?
>
I don't know the history of this test to know if it really meant to rely
on running with -ea or not. Given that it has assertXXX methods then it
may have been partly converted to TestNG at some point. However, I don't
think they are as useless as it might seem. RunTests.gmk (the make file
that is used when use you `make run-test ...`) adds -ea -esa to the
command line when running non-hotspot tests so the assertions are
enabled, which is useful when running tests in areas that do make use of
assert.
-Alan
More information about the jdk-dev
mailing list