Hands on Testing Java: Lab #3

Experiment #4: Relational Expressions

Do this...
Create a test-case class named Experiment04Test.

Relational Operators

In addition to the arithmetic operators, Java provides a variety of other operators. Some allow two values to be compared, and they produce a boolean value (i.e. true or false).

Operator Meaning
== equality operator returns true if and only if its operands are equal.
!= inequality operator returns true if and only if its operands are different.
< less than operator returns true if and only if its left operand is less-than its right operand.
> greater than operator returns true if and only if its left operand is greater-than its right operand.
<= less than or equal to operator returns true if and only if its left operand is not greater than its right operand.
>= greater than or equal to operator returns true if and only if its left operand is not less than its right operand.

Boolean Data Type

In Java, a true/false value is represented by the Boolean type, denoted boolean. Most Boolean operators describe the relationship between two values, so they are often called relational operators.

The boolean type has two literals: true and false.

Trying Them Out

Do this...
Create a test method in Experiment04Test named testRelationalOperators(). Add these assertions to the new test method, substituting in an appropriate comment or value for each ???:

assertEquals("5 is equal to ???", true, 5 == ???);
assertEquals("5 is not equal to ???", false, 5 == ???);
assertEquals("5 is not-equal to ???", true, 5 != ???);
assertEquals("5 is not not-equals to ???", false, 5 != ???);
assertEquals("5 is less than ???", true, 5 < ???);
assertEquals("5 is not less than ???", false, 5 < ???);
assertEquals("5 is ??? 6", ???, 5 <= 6);
assertEquals("5 is ??? 3", ???, 5 <= 3);
assertEquals("5 is greater than ???", true, 5 > ???);
assertEquals("5 is not greater than ???", false, 5 > ???);
assertEquals("5 is ??? 6", ???, 5 >= 6);
assertEquals("5 is ??? 3", ???, 5 >= 3);

Compile and run your code for a green bar.

These particular assertions compare integers, but the comparison operator can be used with any numeric type, integer or floating-point. There is one case to be concerned about: floating-point equality. Due the way that floating-point numbers are computed, they aren't perfectly accurate, so instead of testing x==3.14159, you test a small range around the value. This is why assertEquals() requires three values:

assertEquals(3.14159, x, 1e-8);

The 1e-8 is a really small number which indicates how closely (in this case) x should be to 3.14159. For most of the things that we'll do, using the value 1e-8 should be good enough, but depending on the problem, this value might have to be change to make the test easier or harder.

The JUnit Way

Actually, we've done things wrong, above. We really shouldn't test with boolean literals since JUnit provides a better way of stating the test. Instead of writing these statements:

assertEquals("5 is equal to ???", true, 5 == ???);
assertEquals("5 is not equal to ???", false, 5 == ???);

We should write these:

assertTrue("5 is equal to ???", 5 == ???);
assertFalse("5 is not equal to ???", 5 == ???);

However, for this experiment it was more important to try out the true and false literals. In the rest of the experiments we'll use these alternatives, but there's no need to change the tests for this experiment.

Terminology

boolean, Boolean type, equality operator, greater than operator, greater than or equal to operator, inequality operator, less than operator, less than or equal to operator, relational operator