1

We have some java code that we would ideally like to run in three ways:

  1. Throw an exception when an assert fails
  2. Print a stack trace, but otherwise continue when an assert fails
  3. Ignore an assert

This is to be run in three different environments (development, testing, production, respectively). We can switch between the first and last by using the -ea JVM option, but is it possible to do the second?

Thanks.

EDIT: we already have assert statements everywhere. we prefer to change this option at run-time without changing our code.

2
  • Why would you want #2 in a testing environment? Asserts represent statements that should always pass, and if they don't, that's a bug. (Frankly, I prefer #1 in development, testing, and production.) Commented Aug 29, 2012 at 3:15
  • We don't want an assert in production in case we missed something. We don't want an assert to crash our testing environment, like a beta website. We want to know about the errors without having testing come to a halt. Commented Aug 29, 2012 at 5:02

3 Answers 3

4

It almost seems like you want to use unit testing for this instead of Java's built-in assert. Look into what JUnit could do for you and your team.

You can set up tests to run, and they'll notify you of failure. If you want a stack trace, you can create your own by raising/catching an exception, then use e.printStackTrace().

Sign up to request clarification or add additional context in comments.

5 Comments

testcases do not test the same thing that asserts do. testing is for functionality. assert is for "this should never ever happen and if it does, i can't promise i'm going to do what i should." you should be unable to create a testcase to trip the assert, and if you can, then you should handle that test and not be using assert.
You can do both - you can test for functionality (i.e. was this method called? was this data persisted to the DAO?) and assert things that must be true about the state of the object when you're done (given these conditions, do these fields contain this data?). A program should never (in my mind) implicitly throw its hands up and freak out. If you ever run into a case like that, then there must be something true about the object or environment that can be asserted.
i don't get it. the code has already passed all the designed tests in development environment, the testing environment is for extra testing before it goes into production. in testing and production, we don't want it to throw its hands up and freak out, but we do want it to in development.
Could you justify why? If it could occur in development, it can occur anywhere. Regardless of if it's a code breaking bug or some weird test case, your code should never throw its hands up.
It could occur in development, but it shouldn't, and we can't create a testcase to make it occur. Still, regardless of how thorough tests are, additional tests find something, and we want to know of those without throwing our hands up, that's why we have a testing environment.
0

You can't, not using Assert. Assert is specifically designed to fail and not be ignored.

2 Comments

I think you are confusing the java keyword assert with the JUnit Assert class
this may be true for assert as well, but i'm hoping someone knows some JVM flag i don't
0

You can achieve #2 very easily, since assert throws an AssertionError.

try {
  assert false;
} catch (final AssertionError error) {
  error.printStackTrace();
}
System.out.println("survived!");

4 Comments

how does this let us switch between #1, #2, and #3? this only lets us do #2 and #3, and not #1.
@Jayen use a flag to determine whether to use one wrapped in try-catch vs one not.
like a global variable that gets set by an argument parser? this seems very hacky. not to mention changing every assert we already have in our code.
@Jayen well that's what it'll take. PS I was thinking something more like -Dxxx.softAssert=true

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.