Taking a broader view of what test-driven development is

Image for post
Image for post
Photo by rawpixel on Unsplash
public class Fraction {    private final long numer, denom;    // STUB TO FAIL THE FIRST TEST
public Fraction plus(Fraction addend) {
return new Fraction(0, 1);
}
// STUB TO FAIL THE FIRST TEST
public Fraction minus(Fraction subtrahend) {
return new Fraction(0, 1);
}
// STUB TO FAIL THE FIRST TEST
public Fraction times(Fraction multiplicand) {
return new Fraction(0, 1);
}
// STUB TO FAIL THE FIRST TEST
public Fraction divides(Fraction divisor) {
return new Fraction(0, 1);
}
public Fraction(long numerator, long denominator) {
this.numer = numerator;
this.denom = denominator;
}
}
    @Test
public void testAdd() {
Fraction addendA = new Fraction(1, 3);
Fraction addendB = new Fraction(1, 7);
Fraction expected = new Fraction(10, 21);
Fraction actual = addendA.plus(addendB);
assertEquals(expected, actual);
}
expected:<fractions.Fraction@cac736f>
but was:<fractions.Fraction@5e265ba4>
    @Test
public void testEquals() {
Fraction numberA = new Fraction(5, 8);
Fraction numberB = new Fraction(10, 16);
assertEquals(numberA, numberB);
Fraction numberC = new Fraction(4, 7);
assertNotEquals(numberA, numberC);
SQLNonTransientException obj = new
SQLNonTransientException();
assertNotEquals(numberB, obj);
}
// TODO: Flesh out testHashCode()
@Ignore
@Test
public void testHashCode() {
fail("Pending test");
}
@Test
public void testToString() {
Fraction fract = new Fraction(56, 58);
String expected = "28/29";
String actual = fract.toString().replace(" ", "");
assertEquals(expected, actual);
fract = new Fraction(3, -2);
expected = "-3/2";
actual = fract.toString().replace(" ", "");
assertEquals(expected, actual);
}
    @Test
public void testEquals() {
Fraction numberA = new Fraction(5, 8);
Fraction numberB = new Fraction(10, 16);
assertEquals(numberA, numberB);
}
@Test
public void testNotEqualsOtherFraction() {
Fraction numberA = new Fraction(5, 8);
Fraction numberC = new Fraction(4, 7);
assertNotEquals(numberA, numberC);
}

@Test
public void testNotEqualsOtherObject() {
Fraction numberB = new Fraction(10, 16);
SQLNonTransientException obj = new
SQLNonTransientException();
assertNotEquals(numberB, obj);
}
    @Override
public boolean equals(Object obj) {
return true;
}
Values should be different. Actual: fractions.Fraction@3830f1c0
    @Override
public boolean equals(Object obj) {
Fraction other = (Fraction) obj;
return (this.numer == other.numer &&
this.denom == other.denom);
}
expected:<fractions.Fraction@2fa6> but was:<fractions.Fraction@2df8>
    @Override
public String toString() {
return Long.toString(this.numer) + " / " +
Long.toString(this.denom);
}
expected:<5 / 8> but was:<10 / 16>
Testcase: testEquals(fractions.FractionTest): Caused an ERROR
java.sql.SQLNonTransientException cannot be cast to fractions.Fraction
java.lang.ClassCastException: java.sql.SQLNonTransientException cannot be cast to fractions.Fraction
at fractions.Fraction.equals(Fraction.java:37)
at fractions.FractionTest.testEquals(FractionTest.java:97)
    @Override
public boolean equals(Object obj) {
if (!(obj instanceof Fraction)) {
return false;
}

Fraction other = (Fraction) obj;
return (this.numer == other.numer &&
this.denom == other.denom);
}

is a composer and photographer from Detroit, Michigan. He has been working on a Java program to display certain mathematical diagrams.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store