sábado, 23 de noviembre de 2019

JUnit5 Jupiter (7) Test for Interface Contracts

JUnit5 Jupiter (7) Test for Interface Contracts, Object.equals, Comparable.compareTo


public interface Testable<T> {
    T createValue();}

public interface EqualsContract<T> extends Testable<T> {
    T createNotEqualValue();
    @Test 
    default void valueEqualsItself() {
        T value = createValue();    }

    @Test     
    default void valueDoesNotEqualsNull() {
      T value = createValue(); 
      assertFalse( value.equals(null));    }

    @Test 
    default void valueDoesNotEqualDifferentValue() {
        T value = createValue(); 
        T differentValue = createNotEqualValue(); 
        assertNotEquals( value, differentValue); 
        assertNotEquals( differentValue, value);    }
}

public interface ComparableContract<T extends Comparable<T>> extends Testable<T> {
    T createSmallerValue();
    @Test     
    default void returnsZeroWhenComparableWithItself() {
        T value = createValue(); 
        assertEquals( 0, value.compareTo( value)); 
    }

    @Test 
    default void returnsPositiveNumberWhenComparedToSmallerValue() {
        T value = createValue(); 
        T smallerValue = createSmallerValue(); 
        assertTrue( value.compareTo(smallerValue) > 0); 
    }

    @Test     
    default void returnsNegativeNumberWhenCompareToLargerValue() {
        T value = createValue(); 
        T smallerValue = createSmallerValue(); 
        assertTrue( smallerValue.compareTo( value) < 0);     
    }
}

public class StringTest implements ComparableContract<String>, EqualsContract<String> {
    @Override     
    public String createValue() {
        return "M_empiezaconM"; 
    }

    @Override     
    public String createSmallerValue() {
        return "B_masChicoQue_M";     
    }

    @Override     
    public String createNotEqualValue() {
        return "X_diferenteACreateValue";     
    }
}

Run Test


eot

No hay comentarios:

Publicar un comentario