1.3. Unit Testing
LongInteger: unit tests.
Test: LongInteger()
LongInteger()
Let us create a testing class called LongIntegerTest
and make a unit test for the constructors:
L6
: the@Test
annotation indicates that this method is used for unit testing.L7
: methods used for unit testing must bepublic
.L8
: tests the default constructorassertEquals()
: compares two parameters where the left parameter is the expected value and the right parameter is the actual value.
L12-15
: tests the constructor with a string parameter.L18-19
: tests the copy constructor.
When should we use
import static
instead ofimport
?
When you run this test, you see a prompt similar to the followings:
Test: multiply()
multiply()
Let us define another method for testing the multiply()
method:
Test: compareTo()
compareTo()
Let us define a method for testing the compareTo()
method:
assertTrue()
: passes if the parameter returns true.
Unit testing provides an effective way of ensuring the correctness of your program. Making a unit test for every method is standard practice, although this standard is often dismissed due to time pressure.
Last updated