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:
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
public class LongIntegerTest {
@Test
public void testConstructors() {
// default constructor
assertEquals("0", new LongInteger().toString());
// constructor with a string parameter
assertEquals("12", new LongInteger("12").toString());
assertEquals("34", new LongInteger("+34").toString());
assertEquals("-56", new LongInteger("-56").toString());
assertEquals("-0", new LongInteger("-0").toString());
// copy constructor
assertEquals("12", new LongInteger(new LongInteger("12")).toString());
assertEquals("-34", new LongInteger(new LongInteger("-34")).toString());
}
}L6: the@Testannotation 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 staticinstead 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:
L11-12:acan hold the integer152415787517146788750190521(), which is much larger than the maximum value oflongthat is .
Test: compareTo()
compareTo()Let us define a method for testing the compareTo() method:
assertTrue(): passes if the parameter returns true.
Last updated
Was this helpful?