Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Assume vs assert in JUnit tests

I have read that assume will not run the test if assumption failed, but I am not sure regarding the logic of when to place assert vs assume.

For example: any resource loading check should be done with assume?

When should I use assume over assert?

(Note: i am looking for correct design of when to use one over the other)

like image 875
Mike Avatar asked Jun 19 '17 10:06

Mike


People also ask

What is the difference between assert and assume?

To assume is to believe something without having definite proof, in its primary sense. To assert is to state that something is so.

What is assumptions in JUnit?

Assumptions is a collection of utility methods that support conditional test execution based on assumptions. In direct contrast to failed assertions, failed assumptions do not result in a test failure; rather, a failed assumption results in a test being aborted.

What is the use of assert in JUnit?

The assertSame() method tests if two object references point to the same object. The assertNotSame() method tests if two object references do not point to the same object. void assertArrayEquals(expectedArray, resultArray); The assertArrayEquals() method will test whether two arrays are equal to each other.


1 Answers

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.)
like image 79
Mike Nakis Avatar answered Oct 11 '22 04:10

Mike Nakis