I
Ian Pilcher
I am attempting to use a static inner class for unit testing. As part
its class initialization I want to check that assertions are enabled for
both the inner class and its enclosing class.
Here is an example:
package temp;
public class TestMe
{
// Stuff to test goes here
private static boolean assertionsEnabled()
{
try
{
assert false;
return false;
}
catch (AssertionError e)
{
return true;
}
}
private static class __UnitTest__
{
// Ensure assertions are enabled for this class and TestMe
static
{
try
{
assert false;
throw new Error("assertions not enabled"); // 29
}
catch (AssertionError e)
{
assert TestMe.assertionsEnabled(); // 33
}
}
public static void main(String[] args)
{
// Tests go here
}
}
}
When I run with assertions disabled, things go as expected:
[pilcher@home temp]$ java temp.TestMe\$__UnitTest__
Exception in thread "main" java.lang.Error: assertions not enabled
at temp.TestMe$__UnitTest__.<clinit>(TestMe.java:29)
[pilcher@home temp]$
Turning all (non-system) assertions on also works as expected:
[pilcher@home temp]$ java -ea temp.TestMe\$__UnitTest__
[pilcher@home temp]$
Turning assertions on for the enclosing class seems to also enable them
for the inner class:
[pilcher@home temp]$ java -ea:temp.TestMe temp.TestMe\$__UnitTest__
[pilcher@home temp]$
Attempting to enable assertions for *only* the inner class does not
seem to work, however:
[pilcher@home temp]$ java -ea:temp.TestMe\$__UnitTest__
temp.TestMe\$__UnitTest__
Exception in thread "main" java.lang.Error: assertions not enabled
at temp.TestMe$__UnitTest__.<clinit>(TestMe.java:29)
[pilcher@home temp]$ java -ea:temp.TestMe.__UnitTest__
temp.TestMe\$__UnitTest__
Exception in thread "main" java.lang.Error: assertions not enabled
at temp.TestMe$__UnitTest__.<clinit>(TestMe.java:29)
[pilcher@home temp]$
Is this expected behavior? If so, is it guaranteed? (The latter would
be nice, because it would allow me to dispense with the
assertionsEnabled() method in the outer class.)
Thanks!
its class initialization I want to check that assertions are enabled for
both the inner class and its enclosing class.
Here is an example:
package temp;
public class TestMe
{
// Stuff to test goes here
private static boolean assertionsEnabled()
{
try
{
assert false;
return false;
}
catch (AssertionError e)
{
return true;
}
}
private static class __UnitTest__
{
// Ensure assertions are enabled for this class and TestMe
static
{
try
{
assert false;
throw new Error("assertions not enabled"); // 29
}
catch (AssertionError e)
{
assert TestMe.assertionsEnabled(); // 33
}
}
public static void main(String[] args)
{
// Tests go here
}
}
}
When I run with assertions disabled, things go as expected:
[pilcher@home temp]$ java temp.TestMe\$__UnitTest__
Exception in thread "main" java.lang.Error: assertions not enabled
at temp.TestMe$__UnitTest__.<clinit>(TestMe.java:29)
[pilcher@home temp]$
Turning all (non-system) assertions on also works as expected:
[pilcher@home temp]$ java -ea temp.TestMe\$__UnitTest__
[pilcher@home temp]$
Turning assertions on for the enclosing class seems to also enable them
for the inner class:
[pilcher@home temp]$ java -ea:temp.TestMe temp.TestMe\$__UnitTest__
[pilcher@home temp]$
Attempting to enable assertions for *only* the inner class does not
seem to work, however:
[pilcher@home temp]$ java -ea:temp.TestMe\$__UnitTest__
temp.TestMe\$__UnitTest__
Exception in thread "main" java.lang.Error: assertions not enabled
at temp.TestMe$__UnitTest__.<clinit>(TestMe.java:29)
[pilcher@home temp]$ java -ea:temp.TestMe.__UnitTest__
temp.TestMe\$__UnitTest__
Exception in thread "main" java.lang.Error: assertions not enabled
at temp.TestMe$__UnitTest__.<clinit>(TestMe.java:29)
[pilcher@home temp]$
Is this expected behavior? If so, is it guaranteed? (The latter would
be nice, because it would allow me to dispense with the
assertionsEnabled() method in the outer class.)
Thanks!