junit - imaglig-membres.imag.fr/donsez/cours/junit.pdf · 2014. 1. 9. · 05/01/2009 didier donsez,...

21
JUnit Tests unitaires en Java Didier DONSEZ Université Joseph Fourier (Grenoble 1) PolyTech’Grenoble – LIG/ADELE [email protected] [email protected] http://membres-liglab.imag.fr/donsez/cours

Upload: others

Post on 25-Aug-2020

8 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: junit - imaglig-membres.imag.fr/donsez/cours/junit.pdf · 2014. 1. 9. · 05/01/2009 Didier Donsez, JUnit, 2007-2009 2 Sommaire Motivations JUnit API Misc Bibliographie

JUnit

Tests unitaires en Java

Didier DONSEZUniversité Joseph Fourier (Grenoble 1)

PolyTech’Grenoble – LIG/[email protected]

[email protected]

http://membres-liglab.imag.fr/donsez/cours

Page 2: junit - imaglig-membres.imag.fr/donsez/cours/junit.pdf · 2014. 1. 9. · 05/01/2009 Didier Donsez, JUnit, 2007-2009 2 Sommaire Motivations JUnit API Misc Bibliographie

05/01/2009Didier Donsez, JUnit, 2007-2009

2

Sommaire� Motivations

� JUnit� API

� Misc� Bibliographie

Page 3: junit - imaglig-membres.imag.fr/donsez/cours/junit.pdf · 2014. 1. 9. · 05/01/2009 Didier Donsez, JUnit, 2007-2009 2 Sommaire Motivations JUnit API Misc Bibliographie

05/01/2009Didier Donsez, JUnit, 2007-2009

3

Motivations� Tests unitaires

� http://fr.wikipedia.org/wiki/Test_%28informatique%29� http://fr.wikipedia.org/wiki/Test_unitaire

� eXtreme Programming (XP)� Développer les tests unitaires en même temps/avant le

programme à tester� A partir des spécifications

Page 4: junit - imaglig-membres.imag.fr/donsez/cours/junit.pdf · 2014. 1. 9. · 05/01/2009 Didier Donsez, JUnit, 2007-2009 2 Sommaire Motivations JUnit API Misc Bibliographie

05/01/2009Didier Donsez, JUnit, 2007-2009

5

JUnit� Basé sur le patron de conception [Gamma95]

� Séparation du code de test du code métier� Le code de test est défini, développé et livré séparément

� Site Web� http://www.junit.org

Page 5: junit - imaglig-membres.imag.fr/donsez/cours/junit.pdf · 2014. 1. 9. · 05/01/2009 Didier Donsez, JUnit, 2007-2009 2 Sommaire Motivations JUnit API Misc Bibliographie

05/01/2009Didier Donsez, JUnit, 2007-2009

6

API junit.framework.*

� Interface� Test

� Classes� TestCase étends Assert et implémente Test� TestSuite (suite de TestCase)� Assert (méthodes assertXXX() et failZZZ())

� Méthodes� MyTest.testYYY()

� YYY = specifique à l’application testée� Assert.assertXXX(…)

� XXX = True, False, Equal, …� TestCase.setUp(), TestCase.setUpBeforeClass()

� initialise des objets tels que des connexions JDBC, … avec les tests� TestCase.tearDown(), TestCase.tearDownAfterClass()

� relâche les objets pris au setUp() après les tests� TestSuite.addTest(Test)

� Ajoute un test à une TestSuite

Page 6: junit - imaglig-membres.imag.fr/donsez/cours/junit.pdf · 2014. 1. 9. · 05/01/2009 Didier Donsez, JUnit, 2007-2009 2 Sommaire Motivations JUnit API Misc Bibliographie

05/01/2009Didier Donsez, JUnit, 2007-2009

7

Exemple : Money

import junit.framework.*;public class MoneyTest extends TestCase {

private Money f12CHF;private Money f14CHF;public MoneyTest(String testName){

super(testName);}protected void setUp() {

f12CHF= new Money(12,"CHF");f14CHF= new Money(14,"CHF");

}public void testSimpleAdd () {

Money espected = new Money(26,"CHF");Money result= f12CHF.add(f14CHF);assertTrue(expected.equals(result));

}…

}

Étend TestCase

Nom du test

Invoquée avant chaque test

Une des méthodes de test

Assertion à vérifier

Page 7: junit - imaglig-membres.imag.fr/donsez/cours/junit.pdf · 2014. 1. 9. · 05/01/2009 Didier Donsez, JUnit, 2007-2009 2 Sommaire Motivations JUnit API Misc Bibliographie

05/01/2009Didier Donsez, JUnit, 2007-2009

8

API� TestSuite

� Organiser/Composer/Regrouper plusieurs TestsTestSuite suite = new TestSuite("MoneyTestSuite");

suite.addTest(MoneyTest.class);

suite.addTest(AnotherMoneyTest.class);

� ActiveTestSuite� Chaque test est exécuté par une thread indépendante

� RepeatedTest� Performance ou erreur intermitente

suite.addTest(new RepeatedTest(new MoneyTest("testSimpleAdd"),100));

suite.addTest(new RepeatedTest(new MoneyTest("testComplexAdd"),100));

Page 8: junit - imaglig-membres.imag.fr/donsez/cours/junit.pdf · 2014. 1. 9. · 05/01/2009 Didier Donsez, JUnit, 2007-2009 2 Sommaire Motivations JUnit API Misc Bibliographie

05/01/2009Didier Donsez, JUnit, 2007-2009

11

TestRunner� Utilitaires (textuel et graphique) pour lancer une suite de tests

� java junit.textui.TestRunner money.MoneyTest

� java junit.swingui.TestRunner money.MoneyTest

Page 9: junit - imaglig-membres.imag.fr/donsez/cours/junit.pdf · 2014. 1. 9. · 05/01/2009 Didier Donsez, JUnit, 2007-2009 2 Sommaire Motivations JUnit API Misc Bibliographie

05/01/2009Didier Donsez, JUnit, 2007-2009

12

JUnit 4� Motivation : Exploiter Java 5.0

� Generics, @OP (Meta-programmation)� API org.junit.*

� Étendre TestCase n’est plus nécessaire� @Test � for a test methods

� @Test(expected=IndexOutOfBoundsException.class)� @Test(timeout= 1000)

� @Suite� @Suite(tests={MoneyTest.class, SimpleTest.class})� @Suite(pattern="*Test"}� @Suite(package="org.junit.test.*")

� @Ignore � to ignore a test� @Ignore(reason= "...")

� @Before, @After � setup/teardown� @BeforeClass, @AfterClass � one time setup/teardown� class Assert � méthodes static assertXXX()� @Theory � globally true assertion

Page 10: junit - imaglig-membres.imag.fr/donsez/cours/junit.pdf · 2014. 1. 9. · 05/01/2009 Didier Donsez, JUnit, 2007-2009 2 Sommaire Motivations JUnit API Misc Bibliographie

05/01/2009Didier Donsez, JUnit, 2007-2009

13

Exemple : Money

import org.junit.*;public class MoneyTest4 {

private Money f12CHF;private Money f14CHF;

@Beforeprotected void setUp() {

f12CHF= new Money(12,"CHF");f14CHF= new Money(14,"CHF");

}

@Testpublic void testSimpleAdd () {

Money espected = new Money(26,"CHF");Money result= f12CHF.add(f14CHF);

Assert.assertTrue(expected.equals(result));}

…}

Page 11: junit - imaglig-membres.imag.fr/donsez/cours/junit.pdf · 2014. 1. 9. · 05/01/2009 Didier Donsez, JUnit, 2007-2009 2 Sommaire Motivations JUnit API Misc Bibliographie

05/01/2009Didier Donsez, JUnit, 2007-2009

14

Tests unitaires JavaEE� Test unitaires de Servlets, JSP, Taglibs, Filters, EJB, � Jakarta Cactus http://jakarta.apache.org/cactus/

� Architecture

� A lire� http://www.theserverside.com/tt/articles/content/JakartaPitfalls/JakartaPitfallsChapter1.pdf� http://jakarta.apache.org/cactus/writing/howto_testcase.html

Page 12: junit - imaglig-membres.imag.fr/donsez/cours/junit.pdf · 2014. 1. 9. · 05/01/2009 Didier Donsez, JUnit, 2007-2009 2 Sommaire Motivations JUnit API Misc Bibliographie

05/01/2009Didier Donsez, JUnit, 2007-2009

15

Jakarta Cactus

from doc Jakarta Cactus

Page 13: junit - imaglig-membres.imag.fr/donsez/cours/junit.pdf · 2014. 1. 9. · 05/01/2009 Didier Donsez, JUnit, 2007-2009 2 Sommaire Motivations JUnit API Misc Bibliographie

05/01/2009Didier Donsez, JUnit, 2007-2009

16

Tests unitaires avec Spring� Motivation

� difficulté de tester la logique applicative des EJB en dehors d’un container EJB (2 ou 3).

� POJO (Plain Old Java Object)

� TODO

Page 14: junit - imaglig-membres.imag.fr/donsez/cours/junit.pdf · 2014. 1. 9. · 05/01/2009 Didier Donsez, JUnit, 2007-2009 2 Sommaire Motivations JUnit API Misc Bibliographie

05/01/2009Didier Donsez, JUnit, 2007-2009

17

Tests unitaires de bases de données� Tests unitaires sur des bases relationnelles

� DBUnit http://dbunit.sourceforge.net/� Basé sur JUnit et JDBC

� DbUnit has the ability to export and import your database data to and from XML

datasets. Since version 2.0, DbUnit can works with very large dataset when use

in streaming mode. DbUnit can also helps you to verify that your database data

match expected set of values.

Page 15: junit - imaglig-membres.imag.fr/donsez/cours/junit.pdf · 2014. 1. 9. · 05/01/2009 Didier Donsez, JUnit, 2007-2009 2 Sommaire Motivations JUnit API Misc Bibliographie

05/01/2009Didier Donsez, JUnit, 2007-2009

18

JUnit avec Ant� Tache Ant <junit>

� Lance les tests depuis un build.xml� Exemple

<junit printsummary="yes" haltonfailure="yes"><classpath><pathelement location="${build.tests}"/><pathelement path="${java.class.path}"/>

</classpath>

<formatter type="plain"/>

<test name="my.test.TestCase" haltonfailure="no" outfile="result"><formatter type="xml"/>

</test>

<batchtest fork="yes" todir="${reports.tests}"><fileset dir="${src.tests}"><include name="**/*Test*.java"/><exclude name="**/AllTests.java"/>

</fileset></batchtest>

</junit>

Page 16: junit - imaglig-membres.imag.fr/donsez/cours/junit.pdf · 2014. 1. 9. · 05/01/2009 Didier Donsez, JUnit, 2007-2009 2 Sommaire Motivations JUnit API Misc Bibliographie

05/01/2009Didier Donsez, JUnit, 2007-2009

19

JUnit avec Ant� Tache Ant <junitreport>

� Fusionne plusieurs rapports JUnit

� Exemple<junitreport todir="./reports"><fileset dir="./reports"><include name="TEST-*.xml"/>

</fileset><report format="frames" todir="./report/html"/>

</junitreport>

Page 17: junit - imaglig-membres.imag.fr/donsez/cours/junit.pdf · 2014. 1. 9. · 05/01/2009 Didier Donsez, JUnit, 2007-2009 2 Sommaire Motivations JUnit API Misc Bibliographie

05/01/2009Didier Donsez, JUnit, 2007-2009

20

JUnit avec Maven� Plugins Surefire de Maven

� maven-surefire-plugin� Lance les testcases JUnit lors de la phase de test du build

� http://maven.apache.org/plugins/maven-surefire-plugin/

� maven-surefire-report-plugin� Format les rapports de test lors de site:site

� mvn test -Dtest=MoneyTest

� Autres� Cactus Maven plugin� DBUnit Maven plugin (goals: operation,export,compare)

Page 18: junit - imaglig-membres.imag.fr/donsez/cours/junit.pdf · 2014. 1. 9. · 05/01/2009 Didier Donsez, JUnit, 2007-2009 2 Sommaire Motivations JUnit API Misc Bibliographie

05/01/2009Didier Donsez, JUnit, 2007-2009

21

JUnit depuis Eclipse (i)� Plugin intégré à Eclipse

� Création de TestCase dans tout projet Java

� …

Page 19: junit - imaglig-membres.imag.fr/donsez/cours/junit.pdf · 2014. 1. 9. · 05/01/2009 Didier Donsez, JUnit, 2007-2009 2 Sommaire Motivations JUnit API Misc Bibliographie

05/01/2009Didier Donsez, JUnit, 2007-2009

22

JUnit depuis Eclipse (ii)� et Lancement des tests

Page 20: junit - imaglig-membres.imag.fr/donsez/cours/junit.pdf · 2014. 1. 9. · 05/01/2009 Didier Donsez, JUnit, 2007-2009 2 Sommaire Motivations JUnit API Misc Bibliographie

05/01/2009Didier Donsez, JUnit, 2007-2009

23

NUnit� Comme « JUnit » mais pour .NET !

� Langages : C#, J#, VB.NET, MC++, …� Plateformes: .NET et Mono (dernières versions)� Namespace NUnit.Framework

� Site Web� http://www.nunit.org

Page 21: junit - imaglig-membres.imag.fr/donsez/cours/junit.pdf · 2014. 1. 9. · 05/01/2009 Didier Donsez, JUnit, 2007-2009 2 Sommaire Motivations JUnit API Misc Bibliographie

05/01/2009Didier Donsez, JUnit, 2007-2009

24

Bibliographie� Ted Husted, Vincent Massol, “JUnit in Action”,

Pub. Manning, 2003, 384 pages, ISBN: 1930110995

� J. B. Rainsberger, “JUnit Recipes: Practical Methods for Programmer Testing”, Pub. Manning, 2004, 752 pages, ISBN: 1932394230

� Eric Burke, Adrian Coyner, « Java ExtremeProgramming Cookbook », Pub O’Reilly