Actually, mixing "live" logic and an assertion is bad practice.
In the C language, which is where the original "assert" concept comes from, the assertion mechanism wasn't part of the language spec, it was done using the C precompiler. You'd do something like this:
It was, in fact, intended to be able to load up a program with lots of testing code that would effectively not exist when assertions were turned off, so that when you went to production mode, you wouldn't be burdened with a ton of overhead for code whose sole purpose to ensure that supposedly good data and program states really were good data and program states.
The phrase "assert that the value passed in as a String, is in fact an integer (after converting it to an integer)." is morally dubious anyway. If you convert a String to an integer, of course the result is going to be an integer. It's in the very definition of the operation. In actuality, what you are saying is that you want to assert that the conversion has succeeded (since you used the word "after"). And for that, an assertion is a bad way of doing things, since, to repeat what I said initially, an assertion is something that effectively shouldn't exist when running in production.
So the closest thing to the stated goal would be Tim Cooke's second code example. Except that since in effect, it's swallowing an Exception when running in production code, it violates one of my most sacred programming principles, which is to never simply swallow an Exception.
When it comes to destroying a civilization, gas chambers cannot hold a candle to echo chambers.
Mark D Anthony wrote:I'm passing a string into an assertStatement, and want to assert that the value passed in as a String, is in fact an integer (after converting it to an integer).
Pay attention to what are you actually testing, whether the string conversion resulted in an integer, or whether the string represents an actual integer?
If the latter, you need to test not after the conversion takes place, but also before the conversion, so you could control it and not rely on somebody else (if that's possible).
Test 1 passed; Test 2 failed; Test 3 passed;