Voir aussi
import org.junit.*; import static org.junit.Assert.*; public class JUnitGestionParametres {
Méthodes fournies par JUnit (org.junit.Assert) :
assertEquals
( [ String message , ] expected, actual )
assertEquals
( [ String message , ] expected, actual, delta )assertNull
( [ String message, ] Object object )assertSame
( [ String message, ] expected, actual )assertTrue
( [ String message, ] boolean condition )assertFalse
( [ String message, ] boolean condition )assertArrayEquals
( Object[], Object[] )fail
( [ String message ] )@Test
@Test public void testGetParametre2() throws InvalidStringException, AlreadyExistsException, NonExistentException { gp.addParametre(NOM_PARAMETRE_LEGAL1, VALEUR_PARAMETRE_LEGAL1); gp.setParametre(NOM_PARAMETRE_LEGAL1, VALEUR_PARAMETRE_LEGAL2); assertEquals(VALEUR_PARAMETRE_LEGAL2, gp.getParametre(NOM_PARAMETRE_LEGAL1)); }
@Test(expected = InvalidStringException.class) public void testInvalidStringException2() throws InvalidStringException, AlreadyExistsException { gp.addParametre(STRING_VIDE, VALEUR_PARAMETRE_LEGAL1); }
Les tests sont exécutés en sélectionnant Run
comme tests JUnit dans le menu Run
pour une ressource correspondant à des tests :
À la ligne de commande :
java -classpath /jupiter/home/iglewski/junit3.8.1/junit.jar:/jupiter/home/iglewski/ens/inf1583/CVS/lib/exam.jar \ junit.textui.TestRunner tests.JUnitDifferenceDeuxMots
Avec un interface graphique Swing :
java -classpath /jupiter/home/iglewski/junit3.8.1/junit.jar:/jupiter/home/iglewski/ens/inf1583/CVS/lib/exam.jar \ junit.swingui.TestRunner tests.JUnitDifferenceDeuxMots
Pour créer une suite de tests, on peut utiliser le schéma ci-dessous. La
classe annotée avec @RunWith
exécutera les tests qui se
trouvent dans les
classes indiquées par @SuiteClasses
.
import org.junit.runner.RunWith; import org.junit.runners.Suite; @RunWith(Suite.class) @Suite.SuiteClasses({ JUnitGestionParametres.class, JUnitQuestionReponseU.class }) public class TousLesTests { }
Le fichier TousLesTests.java contient une suite de tests.
@Ignore
avant ou après @Test.
@Test(timeout=1) public void testGetParametre2() throws InvalidStringException, AlreadyExistsException, NonExistentException { gp.addParametre(NOM_PARAMETRE_LEGAL1, VALEUR_PARAMETRE_LEGAL1); gp.setParametre(NOM_PARAMETRE_LEGAL1, VALEUR_PARAMETRE_LEGAL2); assertEquals(VALEUR_PARAMETRE_LEGAL2, gp.getParametre(NOM_PARAMETRE_LEGAL1)); }
import Fibonacci; import java.util.Collection; import java.util.Arrays; import org.junit.Test; import static org.junit.Assert.assertEquals; import org.junit.runner.RunWith; import org.junit.runners.Parameterized; import org.junit.runners.Parameterized.Parameters; @RunWith(Parameterized.class) public class FibonacciTest { @Parameters public static Collection data() { return Arrays.asList(new Object[][] { { 0, 0 }, { 1, 1 }, { 2, 1 }, { 3, 2 }, { 4, 3 }, { 5, 5 }, { 6, 8 } }); } private int fInput; private int fExpected; public FibonacciTest(int input, int expected) { fInput = input; fExpected = expected; } @Test public void test() { assertEquals(fExpected, Fibonacci.calculer(fInput)); } } /////////////// public class Fibonacci { public static int calculer(int i){ int k1 = 0, k2 = 1; for (int m=0; m<i; m++){ int temp = k1; k1 = k2; k2 = temp + k1; } return k1; } }
Parmi les deux tests ci-dessous, le premier est plus significatif.
assertTrue("opérateur de puissance produit un mauvais résultat", puissance(2,3) == 8); assertTrue("opérateur de puissance produit un mauvais résultat", puissance(2,2) == 4);
Il y a plus de chances dans le deuxième cas que le test réussise par "hasard".