Assume vs assert in JUnit tests

Simply check out the javadoc for Assume:

A set of methods useful for stating assumptions about the conditions in which a test is meaningful. A failed assumption does not mean the code is broken, but that the test provides no useful information.

In other words: when an assert fires, you know that your testcase failed. Your production code isn't doing what you expect it to do.

Assume means ... you don't know exactly what happened.


The most easiest difference between Assert and Assume is :

Assume will only run when the assumption is true. Will be skipped if it false.

assumeTrue(boolean assumption, String message)

Assert will run normally if true. In case of false assert, it gives predefined error message.

assertTrue(boolean condition, String message)

You would use assume if you have circumstances under which some tests should not run at all. "Not run" means that it cannot fail, because, well, it did not run.

You would use assert to fail a test if something goes wrong.

So, in a hypothetical scenario where:

  • you have different builds for different customers, and
  • you have some resource which is only applicable to a particular client, and
  • there is something testable about that resource, then

you would write a test which:

  • assumes that the resource is present, (so the test will not run on customers that do not have that resource,) and then
  • asserts that everything about the resource is okay (so on the customer that does actually have the resource, the test makes sure that the resource is as it should be.)

The Assert class is the workhorse of JUnit and is the class JUnit testers are most familiar with. Most JUnit assert signatures are similar in nature. They consist of an optional message, an expected instance or variable and the actual instance or variable to be compared. Or, in the case of a boolean test like True, False, or Null, there is simply the actual instance to be tested.

The signature with a message simply has an initial parameter with a message string that will be displayed in the event the assert fails:

assert<something>(“Failure Message String”, <condition to be tested>);

Assumptions: You’ve probably heard that it’s best not to work on assumptions so here is a testing tool JUnit gives you to ensure your tests don’t.

Both Asserts and Assumes stop when a test fails and move on to the next test. The difference is that a failed Assert registers the failure as a failed test while an Assume just moves to the next test. This permits a tester to ensure that conditions, some of which may be external and out of control of the tester, are present as required before a test is run.

There are four varieties of Assumes: one to check a boolean condition, one to check that an exception has not occurred, one to check for null objects, and one that can take a Hamcrest matcher. As seen in the Assert section above, the ability to take a Hamcrest matcher is a gateway to testing flexibility.

You can read more here https://objectcomputing.com/resources/publications/sett/march-2014-junit-not-just-another-pretty-assert/

In short Assume used to disable tests, for example the following disables a test on Linux: Assume.assumeFalse(System.getProperty("os.name").contains("Linux"));
Assert is used to test the functionality.