TDD - JUnit

Test-driven development (TDD), also called test-driven design, is a method of implementing software programming that interlaces unit testing, programming and refactoring on source code.

searchsoftwarequality.techtarget.com

Qualité

Découpage

Prévision

Intégration continue

Maintenabilité

Connaissances

Intégration

Cohérence de groupe

[...]
dependencies {
    testCompile group: 'junit', name: 'junit', version: '4.12'
}
[...]
$> gradle test

gradle.build

Tests

package com.Workshop;

import org.junit.Test;

import static org.junit.Assert.*;

public class MyClassTest {
    @Test
    public void myTest() throws Exception {
        [...]
    }
}
package com.Workshop;

import org.junit.Test;

import static org.junit.Assert.*;

public class IntTest {
    @Test
    public void addOne() throws Exception {
        MyInt value = MyInt(1);

        assertEquals(value.multiply(2), 2); 
    }
}

http://junit.org/junit4/javadoc/4.12/org/junit/Assert.html

assertEquals(String message, float expected, float actual, float delta) 

assertNotEquals(Object unexpected, Object actual) 

assertNull(Object object)

assertTrue(boolean condition)

assertArrayEquals(int[] expecteds, int[] actuals)

En pratique ? 

Mise en application

Développez en TDD une classe "MyInt" que l'on pourra utiliser comme suit :

MyInt value = 10;

value.add(10);
value.multiply(2);
value.sub(3);
value.div(2);
value.mod(5);
value.print();

Rappel :

Java workshop

By foret_a

Java workshop

  • 411