1

Possible Duplicates:
Assert keyword in java
assert vs. JUnit Assertions

I use JUnit for unit testing, but a lot of times its hard to get at certain things with JUnit. I have recently started looking at Java assert.

Is it a good idea to use assert and how would you recommend using it?

Community
  • 1
  • 1
Ben B.
  • 3,706
  • 5
  • 26
  • 27

3 Answers3

1

The most important thing to note is that using assertions and testing your code are two different (if perhaps related) tasks.

I think the assertions docs pretty much explain the kind of situations to use assertions.

brain
  • 5,496
  • 1
  • 26
  • 29
  • The docs link is now broken. I found two links that may be helpful: [Programming with Assertions](https://docs.oracle.com/javase/7/docs/technotes/guides/language/assert.html) and [Using Assertions in Java](http://www.oracle.com/us/technologies/java/assertions-139853.html). – Brendon Whateley Nov 16 '18 at 15:51
1

Assertions are popular in languages without exception handling, for making checks to ensure the program is operating correctly, so as to make problems easier to diagnose.

However there can be some ambiguity as to what the most appropriate cause of action is if an assertion fires. If the program exits immediately, there may have been a wasted opportunity to handle the failure more gracefully (e.g. reporting the failure to the user and attempting to recover data). If the program does not exit immediately, it could be in an unknown state because of the error, which is also undesirable.

As a result it is considered better practice to throw exceptions in Java when a given condition fails, which allows a best-of-both-worlds solution to these problems. For errors that can occur during normal operation of the program, checked exceptions most appropriate. Calling code is obliged to handle these errors. For errors caused by failure internal program logic, throwing a RuntimeException would allow the program to handle the failure appropriately further up the call stack.

Asserts do have one benefit which is they can be employed in development, but can be 'compiled out' for a consumer or release build, where speed of execution is more of a priority than early detection and handling of errors. However apps that require that characteristic are not commonly built in Java (more likely C++), which is again why assert is rare in Java.

(Also, I believe that assets at runtime are not always enabled by default by some IDEs anyway, so it is easy to have an assertion fail and not know about it.)

Jim Blackler
  • 22,946
  • 12
  • 85
  • 101
  • 1
    I completely disagree. Assertions are very useful. The reason they're less important in Java than C++ is simply because Java is a much safer language, so most opportunities for assertion failures, like array index out-of-bounds, are already checked for you. The crucial distinction between assertions over exceptions is this: Exceptions are for unusual cases that you expect to occasionally encounter in production, and which you can recover from. Assertions are for situations that you would never see unless there is a bug in your code. The only way to recover is to go back and fix your code. – MiguelMunoz Jan 16 '14 at 22:59
  • 1
    So, assertions are only for the development phase. That's why you need to turn them on to use them. . (They are off by default.) In production, you would leave them off. The whole reason for using assert is to catch a potential bug. That's why they throw Errors instead of Exceptions. They should NOT be a part of your normal exception handling. Also, since you leave them off for production, you can afford to write assertions that do a lot of work (and slow you down) to ensure your code is working as expected. You don't usually need to do this, but the option is there if you need it. – MiguelMunoz Jan 16 '14 at 23:00
0

Yes it can be useful to use assert to verify the correctness of your program run-time. I would typically use it to catch errors according to the fail-fast principle to actively detect bugs.

A typical use case would be that an operation succeeded. Consider the following sample:

int nChanges = database.update(..);
assert(nChanges == 1);

I wouldn't consider using it for parameter validation or any such operation which should be validated in regular code according to the contract, since it doesn't throw the appropriate exceptions (and run-time exceptions can be mean). Similarly I wouldn't use it in testing since JUnit already offers assert mechanisms which in contrast to regular assert statements cannot be disabled.

Johan Sjöberg
  • 47,929
  • 21
  • 130
  • 148