Usage of Java assertions in JTreg tests?

Volker Simonis volker.simonis at gmail.com
Tue Nov 15 17:19:32 UTC 2022


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?

Best regards,
Volker


More information about the jdk-dev mailing list