Spécifications, Tests et écriture de code

Page 1 sur 16Lecteur de document UniversityLib

Spécifications, Tests et écriture de code

Programming, Software Engineering · course

Sp cifications, Tests et criture de

code

Rappel sur le processus de conception dun programme

Pr condition

Comprendre les exigences fonctionnelles du programme

Indispensable pour d finir les pr conditions et postconditions des m thodes

Comprendre les exigences non fonctionnelles (ou crit res de qualit s)

Permet de juger la qualit du r sultat produit

Conception (Design)

De larchitecture logique

Du mod le de comportement

Des interfaces

Choix des structures de donn es

&

Ecriture du code

&

Rappel sur le processus de conception dun programme

Rappel sur le processus de conception dun programme

Rappels: Quels sont les avantages et inconv nients des principes de

conception d j vus

Masquage de linformation ?

Permet de changer simplement la repr sentation de lobjet

Priv vs public, Constructeurs, Getters/Setters

Les interfaces Java ?

Favorise linterop rabilit entre les impl mentations en d finissant un type commun

Liaison dynamique

Les classes abstraites ?

Permet le partage dattributs et de fixer des m thodes communes

Diminue linterop rabilit par rapport aux interfaces (h ritage simple)

Normalement une seule impl mentation pour une m thode abstraite

3

Pr condition

Comprendre les exigences fonctionnelles du programme

Indispensable pour d finir les pr conditions et postconditions des m thodes

Comprendre les exigences non fonctionnelles (ou crit res de qualit s)

Permet de juger la qualit du r sultat produit

Conception (Design)

De larchitecture logique

Du mod le de comportement

Des interfaces

Choix des structures de donn es

&

Ecriture du code

&

Quest ce quil manque ?

2

4

Rappel sur le processus de conception dun programme

Les sp cifications fonctionnelles

Pr condition

La r alisation dun logiciel est une chose complexe

Comprendre les exigences fonctionnelles du programme

Difficult darriver lobjectif de d part: attention aux d rives

Indispensable pour d finir les pr conditions et postconditions des m thodes

Comprendre les exigences non fonctionnelles (ou crit res de qualit s)

Permet de juger la qualit du r sultat produit

Conception (Design)

De larchitecture logique

Du mod le de comportement

Des interfaces

Choix des structures de donn es

&

Ecriture du code

Test du code

Build automatiques et int gration continue

Cette tape peut m me

intervenir avant la phase

d criture de code, on parle

alors de

Test Driven Development (TDD)

Les sp cifications fonctionnelles

public class Graph {

&

/**

  • @return the distance between noeud1 and noeud2
  • in the graph. If noeud1 and noeud2 are not
  • linked, returns -1 instead

*/

public int distance(Node noeud1, Node noeud2){

&

}

}

La sortie pr c dente est donc correcte si nSud1 et noeud2 ne sont pas reli s

Le but des sp cifications fonctionnelles est justement de d crire ce genre de

situations

On utilise la documentation (javadoc) pour les exprimer

5

7

Pour viter les d rives, le g nie logiciel recommande le passage par un cahier

des charges d finissant les besoins fonctionnels du logiciel (les sp cifications)

Vous devez donc avoir une id e du r sultat AVANT de commencer le

d veloppement du logiciel

Exemple

Instruction

monGraphe.distance(noeud1, noeud2)

R sultat

-1

Est-ce correct ?

Les sp cifications fonctionnelles

Elles doivent contenir plusieurs l ments

Le comportement normal

Les comportements anormaux attendus

Les crit res de qualit retenus

Ces sp cifications doivent tre

Compl tes: Tous les comportement doivent tre explicit s

Non ambig es: Il faut TOUT sp cifier sur le comportement du syst me

Consistantes: Elles ne doivent pas se contredire les unes les autres

Non-verbeuses: Elles ne doivent contenir que les informations n cessaires

lutilisateur

Testables: Elles doivent permettre de mettre en Suvre l valuation

Correctes: Dans le sens o elles r pondent bien au besoin utilisateur

6

8

Les sp cifications fonctionnelles

Les sp cifications fonctionnelles

Besoin

R sultat

Exemples de sp cifications tir es de la JDK

Fonction abs() de java.lang.Math

Les sp cifications fonctionnelles

Fonction sqrt() de java.lang.Math

9

11

Les sp cifications fonctionnelles

Cest parfois long&mais indispensable!

Exemple de la fonction pow() travers la javadoc g n r e

10

12

Les sp cifications fonctionnelles

Les sp cifications fonctionnelles

Lobjectif de ces sp cifications est donc de permettre deffectuer des tests

unitaires sans conna tre forc ment les d tails de limpl mentation

Le r le dun test unitaire est dassurer que chaque bout de code crit est correct

Un test vous permettra de trouver une erreur mais il ne la corrigera pas !

Il permet dam liorer la qualit g n rale du code, de clarifier la documentation et

les sp cifications

Lensemble des tests unitaires( ou suite de tests ) va permettre de v rifier que

le logiciel produit est conforme aux sp cifications de d part

13

14

La programmation conduite par les tests

La programmation conduite par les tests

Test Driven Development (TDD)

M thode de g nie logiciel recommandant de commencer par crire le test

unitaire AVANT d crire le code source

Le TDD est organis en 5 phases

Testing shows the presence,

not the absence of bugs

Edsger W. Dijkstra 1969

Ecriture du test partir des sp cifications fonctionnelles

V rification que le test choue (le code nexiste pas)

Ecriture du code

V rification que le test passe

Refactoriser (Am liorer) le code tout en v rifiant que le test passe toujours

Lint r t d crire le test unitaire en premier est de r fl chir aux diff rentes

situations auquel le code peut tre confront

On utilise lobjet avant m me quil nexiste

15

Source: Wikipedia

16

Pourquoi faire tout a ?

Que faut-il tester ?

Inconv nients

N cessite d crire beaucoup plus de code

A chaque classe, un test unitaire doit tre associ

Il faut crire un test pour chaque cas dune sp cification fonctionnelle

Les cas repr sentatifs

Les cas derreurs

Les environnements de d veloppements vous aide n anmoins pour r aliser cette tache

Vous navez pas de temps perdre !

Il faut v rifier les conditions limites

Faux! Il a t prouv que la recherche de bug est beaucoup plus rapide avec des test

unitaires

Avantages

Permet davoir une bonne confiance dans le code que vous produisez

Vous travaillerez en quipe !

Evite de passer des heures trouver un bug dans une centaine de classes

Un test est beaucoup plus simple lire quune classe et permet de voir

rapidement si une fonctionnalit a t oubli e

Exercice

Prenons la fonction suivante

/**

  • Met la valeur 0 sur les len premi res valeurs du tableau.

*

  • @param tab, le tableau de d part dune longueur dau moins len
  • @param len, le nombre de case mettre 0

public void resetPartiel(int[] tab, int len)

Quels sont les tests effectuer ?

Une solution possible:

Test avec un tableau vide

Test avec un tableau non allou (null)

Test avec un tableau de taille 1 puis 2

Test avec une valeur de len invalide (n gative, nulle, > taille du tableau)

Test avec un tableau tr s grand pour valuer les performances

17

19

Black Box Testing

Il faut tester tous les cas difficiles

Stress tests

Situations complexes

Il faut tester les choix dimpl mentation

Tests v rifiant les structures de donn es utilis es, les it rations mises en

Suvre

Tests permettant de v rifier une coop ration entre objets

White Box Testing

18

Le Framework JUnit

JUnit est un environnement de test tr s populaire qui est utilis massivement

dans lindustrie

Poss de des alter go dans norm ment de langages

Int gr dans la plupart des IDE actuels (dont Eclipse et IntelliJ)

Il permet de d tecter les erreurs mais ne les corrige pas !

Dans JUnit, les tests sorganisent tr s simplement en cr ant un r pertoire test

contenant les m mes paquetages que le code tester

20

Le Framework JUnit 4 & 5

Un test unitaire propose une m thode de test par m thode de la classe

tester

Code source:

&

public int calculer(){

&

}

Simple dutilisation

Test associ :

&

@Test

public void testCalculer(){

int res = ref.calculer();

}

Chaque m thode annot e par @Test est ex cut e ind pendamment des autres

Propose des m thodes assert& permettant d valuer les r sultats renvoy s

par lex cution du code

Lorsquune assertion choue, le test sarr te

Les assertions JUnit

Pour les tests d galit

assertEquals(expected, actual)

assertEquals(message, expected, actual)

assertEquals(expected, actual, delta)

assertEquals(message, expected, actual, delta)

Pour les conditions

assertFalse(condition)

assertFalse(message, condition)

assertTrue(condition)

assertTrue(message, condition)

Pour les objets

assert(Not)Null(object)

assert(Not)Null(message, object)

assert(Not)Same(expected, actual)

assert(Not)Same(message, expected, actual)

Exemple de test

Classe tester

Test associ

21

23

Comment crire un test unitaire?

En g n ral, on retrouve lobjet tester en attribut de la classe de test

Cet objet est construit dans la m thode setUp()

Cette m thode est appel e avant chaque test gr ce lannotation

@BeforeEach

@BeforeAll pour annoter une m thode appel e avant tous les tests

Il est galement possible de faire les initialisations de lobjet dans cette

m thode

Les m thodes annot es par @BeforeEach sont appel es avant chaque test

Ceci permet davoir des tests ind pendants les uns des autres

De mani re similaire, il est possible dutiliser une m thode tearDown()

annot e par @AfterEach qui est appel e apr s chaque test

Permet de fermer un fichier ou une connexion vers une base de donn e par

exemple

@BeforeAll permet dannoter une m thode avant tous les tests

22

24

Prime Factors Kata

Object Mentor, Inc.

www.objectmentor.com

blog.objectmentor.com

fitnesse.org

www.junit.org

Copyright 2005 by Object Mentor, Inc

All copies must retain this page unchanged.

The Requirements.

Prime Factors

+ generate(n : int)

" Write a class named PrimeFactors that

has one static method: generate.

The generate method takes an integer

argument and returns a List<Integer>. That

list contains the prime factors in numerical

sequence.

Generating Prime Factors.

Although quite short, this kata is fascinating in the way it shows how if statements

Publicité

become while statements as the number of test cases increase. Its also a

wonderful example of how algorithms sometimes become simpler as they become

more general.

I stumbled upon this little kata one evening when my son was in 7th grade. He

had just discovered that all numbers can be broken down into a product of primes

and was interested in exploring this further. So I wrote a little ruby program, test-

first, and was stunned by how the algorithm evolved.

I have done this particular kata in Java 5.0. This should give you a feel for the

power and convenience of some of the new features.

Begin.

" Create a project named PrimeFactors

" Create a package named primeFactors

" Create a unit test named

PrimeFactorsTest

package primeFactors;

import junit.framework.TestCase;

public class PrimeFactorsTest extends TestCase {

}

No tests found in primeFactors.PrimeFactorsTest

The first test.

The first test.

package primeFactors;

import junit.framework.TestCase;

public class PrimeFactorsTest extends TestCase {

public void testOne() throws Exception {

assertEquals(list(),PrimeFactors.generate(1));

}

}

package primeFactors;

import junit.framework.TestCase;

import java.util.List;

public class PrimeFactorsTest extends TestCase {

public void testOne() throws Exception {

assertEquals(list(),PrimeFactors.generate(1));

}

private List<Integer> list() {

return null;

}

}

The first test.

package primeFactors;

public class PrimeFactors {

}

package primeFactors;

import junit.framework.TestCase;

import java.util.List;

public class PrimeFactorsTest extends TestCase {

public void testOne() throws Exception {

assertEquals(list(),PrimeFactors.generate(1));

}

private List<Integer> list() {

return null;

}

}

The first test.

package primeFactors;

import junit.framework.TestCase;

import java.util.List;

public class PrimeFactorsTest extends TestCase {

public void testOne() throws Exception {

assertEquals(list(),PrimeFactors.generate(1));

}

package primeFactors;

import java.util.*;

public class PrimeFactors {

public static List<Integer> generate(int n) {

return new ArrayList<Integer>();

}

}

private List<Integer> list() {

return null;

}

}

expected:<null> but was:<[]>

The first test.

package primeFactors;

import junit.framework.TestCase;

import java.util.*;

public class PrimeFactorsTest extends TestCase {

public void testOne() throws Exception {

assertEquals(list(),PrimeFactors.generate(1));

}

private List<Integer> list() {

return new ArrayList<Integer>();

}

}

package primeFactors;

import java.util.*;

public class PrimeFactors {

public static List<Integer> generate(int n) {

return new ArrayList<Integer>();

}

}

The first test.

package primeFactors;

import junit.framework.TestCase;

import java.util.*;

package primeFactors;

import java.util.*;

public class PrimeFactors {

public static List<Integer> generate(int n) {

public class PrimeFactorsTest extends TestCase {

return new ArrayList<Integer>();

private List<Integer> list() {

return new ArrayList<Integer>();

}

}

}

public void testOne() throws Exception {

assertEquals(list(),PrimeFactors.generate(1));

}

}

The Second Test

The Second test.

package primeFactors;

import junit.framework.TestCase;

import java.util.*;

package primeFactors;

import java.util.*;

public class PrimeFactors {

public static List<Integer> generate(int n) {

public class PrimeFactorsTest extends TestCase {

return new ArrayList<Integer>();

private List<Integer> list() {

return new ArrayList<Integer>();

}

}

}

public void testOne() throws Exception {

assertEquals(list(),PrimeFactors.generate(1));

}

public void testTwo() throws Exception {

assertEquals(list(2),PrimeFactors.generate(2));

}

}

The Second test.

The Second test.

package primeFactors;

import java.util.*;

public class PrimeFactors {

public static List<Integer> generate(int n) {

return new ArrayList<Integer>();

varargs

}

}

package primeFactors;

import junit.framework.TestCase;

import java.util.*;

public class PrimeFactorsTest extends TestCase {

private List<Integer> list(int... ints) {

List<Integer> list = new ArrayList<Integer>();

for (int i : ints)

list.add(i);

return list;

}

public void testOne() throws Exception {

assertEquals(list(),PrimeFactors.generate(1));

}

public void testTwo() throws Exception {

assertEquals(list(2),PrimeFactors.generate(2));

}

}

expected:<[2]> but was:<[]>

package primeFactors;

import junit.framework.TestCase;

import java.util.*;

public class PrimeFactorsTest extends TestCase {

private List<Integer> list(int... ints) {

List<Integer> list = new ArrayList<Integer>();

for (int i : ints)

list.add(i);

return list;

}

public void testOne() throws Exception {

assertEquals(list(),PrimeFactors.generate(1));

}

public void testTwo() throws Exception {

assertEquals(list(2),PrimeFactors.generate(2));

}

}

package primeFactors;

import java.util.*;

public class PrimeFactors {

public static List<Integer> generate(int n) {

List<Integer> primes = new ArrayList<Integer>();

if (n > 1) {

primes.add(2);

}

return primes;

}

}

The Second test.

package primeFactors;

import static primeFactors.PrimeFactors.generate;

import junit.framework.TestCase;

import java.util.*;

public class PrimeFactorsTest extends TestCase {

private List<Integer> list(int... ints) {

List<Integer> list = new ArrayList<Integer>();

for (int i : ints)

list.add(i);

return list;

}

public void testOne() throws Exception {

assertEquals(list(),generate(1));

}

public void testTwo() throws Exception {

assertEquals(list(2),generate(2));

}

}

package primeFactors;

import java.util.*;

public class PrimeFactors {

public static List<Integer> generate(int n) {

List<Integer> primes = new ArrayList<Integer>();

if (n > 1) {

primes.add(2);

}

return primes;

}

}

The Third Test

The Third test.

The Third test.

package primeFactors;

import static primeFactors.PrimeFactors.generate;

import junit.framework.TestCase;

import java.util.*;

public class PrimeFactorsTest extends TestCase {

private List<Integer> list(int... ints) {

List<Integer> list = new ArrayList<Integer>();

for (int i : ints)

list.add(i);

return list;

}

package primeFactors;

import java.util.*;

public class PrimeFactors {

public static List<Integer> generate(int n) {

List<Integer> primes = new ArrayList<Integer>();

if (n > 1) {

primes.add(2);

}

return primes;

}

}

public void testOne() throws Exception {

assertEquals(list(),generate(1));

}

public void testTwo() throws Exception {

assertEquals(list(2),generate(2));

}

public void testThree() throws Exception {

assertEquals(list(3),generate(3));

}

}

expected:<[3]> but was:<[2]>

package primeFactors;

import static primeFactors.PrimeFactors.generate;

import junit.framework.TestCase;

import java.util.*;

public class PrimeFactorsTest extends TestCase {

private List<Integer> list(int... ints) {

List<Integer> list = new ArrayList<Integer>();

for (int i : ints)

list.add(i);

return list;

}

public void testOne() throws Exception {

assertEquals(list(),generate(1));

}

public void testTwo() throws Exception {

assertEquals(list(2),generate(2));

}

public void testThree() throws Exception {

assertEquals(list(3),generate(3));

}

}

package primeFactors;

import java.util.*;

public class PrimeFactors {

public static List<Integer> generate(int n) {

List<Integer> primes = new ArrayList<Integer>();

if (n > 1) {

primes.add(n);

}

return primes;

}

Publicité

}

The Fourth Test

The Fourth test.

package primeFactors;

import static primeFactors.PrimeFactors.generate;

import junit.framework.TestCase;

import java.util.*;

public class PrimeFactorsTest extends TestCase {

private List<Integer> list(int... ints) {

List<Integer> list = new ArrayList<Integer>();

for (int i : ints)

list.add(i);

return list;

}

package primeFactors;

import java.util.*;

public class PrimeFactors {

public static List<Integer> generate(int n) {

List<Integer> primes = new ArrayList<Integer>();

if (n > 1) {

primes.add(n);

}

return primes;

}

}

public void testOne() throws Exception {

assertEquals(list(),generate(1));

}

public void testTwo() throws Exception {

assertEquals(list(2),generate(2));

}

public void testThree() throws Exception {

assertEquals(list(3),generate(3));

}

public void testFour() throws Exception {

assertEquals(list(2,2),generate(4));

}

}

expected:<[2, 2]> but was:<[4]>

The Fourth test.

package primeFactors;

import java.util.*;

public class PrimeFactors {

public static List<Integer> generate(int n) {

List<Integer> primes = new ArrayList<Integer>();

if (n > 1) {

if (n%2 == 0) {

primes.add(2);

n /= 2;

}

if (n > 1)

primes.add(n);

}

return primes;

}

}

package primeFactors;

import static primeFactors.PrimeFactors.generate;

import junit.framework.TestCase;

import java.util.*;

public class PrimeFactorsTest extends TestCase {

private List<Integer> list(int... ints) {

List<Integer> list = new ArrayList<Integer>();

for (int i : ints)

list.add(i);

return list;

}

public void testOne() throws Exception {

assertEquals(list(),generate(1));

}

public void testTwo() throws Exception {

assertEquals(list(2),generate(2));

}

public void testThree() throws Exception {

assertEquals(list(3),generate(3));

}

public void testFour() throws Exception {

assertEquals(list(2,2),generate(4));

}

}

The Fifth test.

package primeFactors;

import java.util.*;

public class PrimeFactors {

public static List<Integer> generate(int n) {

List<Integer> primes = new ArrayList<Integer>();

if (n > 1) {

if (n%2 == 0) {

primes.add(2);

n /= 2;

}

if (n > 1)

primes.add(n);

}

return primes;

}

}

package primeFactors;

import static primeFactors.PrimeFactors.generate;

import junit.framework.TestCase;

import java.util.*;

public class PrimeFactorsTest extends TestCase {

private List<Integer> list(int... ints) {

List<Integer> list = new ArrayList<Integer>();

for (int i : ints)

list.add(i);

return list;

}

public void testOne() throws Exception {

assertEquals(list(),generate(1));

}

public void testTwo() throws Exception {

assertEquals(list(2),generate(2));

}

public void testThree() throws Exception {

assertEquals(list(3),generate(3));

}

public void testFour() throws Exception {

assertEquals(list(2,2),generate(4));

}

public void testSix() throws Exception {

assertEquals(list(2,3),generate(6));

}

}

The Fifth Test

The Sixth Test

The Sixth test.

The Sixth test.

package primeFactors;

import java.util.*;

public class PrimeFactors {

public static List<Integer> generate(int n) {

List<Integer> primes = new ArrayList<Integer>();

if (n > 1) {

if (n%2 == 0) {

primes.add(2);

n /= 2;

}

if (n > 1)

primes.add(n);

}

return primes;

}

}

package primeFactors;

import static primeFactors.PrimeFactors.generate;

import junit.framework.TestCase;

import java.util.*;

public class PrimeFactorsTest extends TestCase {

private List<Integer> list(int... ints) {

List<Integer> list = new ArrayList<Integer>();

for (int i : ints)

list.add(i);

return list;

}

public void testOne() throws Exception {

assertEquals(list(),generate(1));

}

public void testTwo() throws Exception {

assertEquals(list(2),generate(2));

}

public void testThree() throws Exception {

assertEquals(list(3),generate(3));

}

public void testFour() throws Exception {

assertEquals(list(2,2),generate(4));

}

public void testSix() throws Exception {

assertEquals(list(2,3),generate(6));

}

public void testEight() throws Exception {

assertEquals(list(2,2,2),generate(8));

}

}

expected:<[2, 2, 2]> but was:<[2, 4]>

package primeFactors;

import static primeFactors.PrimeFactors.generate;

import junit.framework.TestCase;

import java.util.*;

public class PrimeFactorsTest extends TestCase {

private List<Integer> list(int... ints) {

List<Integer> list = new ArrayList<Integer>();

for (int i : ints)

list.add(i);

return list;

}

public void testOne() throws Exception {

assertEquals(list(),generate(1));

}

public void testTwo() throws Exception {

assertEquals(list(2),generate(2));

}

public void testThree() throws Exception {

assertEquals(list(3),generate(3));

}

public void testFour() throws Exception {

assertEquals(list(2,2),generate(4));

}

public void testSix() throws Exception {

assertEquals(list(2,3),generate(6));

}

public void testEight() throws Exception {

assertEquals(list(2,2,2),generate(8));

}

}

package primeFactors;

import java.util.*;

public class PrimeFactors {

public static List<Integer> generate(int n) {

List<Integer> primes = new ArrayList<Integer>();

if (n > 1) {

while (n%2 == 0) {

primes.add(2);

n /= 2;

! ! !

}

if (n > 1)

primes.add(n);

}

return primes;

}

}

The Seventh Test

package primeFactors;

The Seventh test.

package primeFactors;

import java.util.*;

public class PrimeFactors {

public static List<Integer> generate(int n) {

List<Integer> primes = new ArrayList<Integer>();

if (n > 1) {

while (n%2 == 0) {

primes.add(2);

n /= 2;

}

if (n > 1)

primes.add(n);

}

return primes;

}

}

import static primeFactors.PrimeFactors.generate;

import junit.framework.TestCase;

import java.util.*;

public class PrimeFactorsTest extends TestCase {

private List<Integer> list(int... ints) {

List<Integer> list = new ArrayList<Integer>();

for (int i : ints)

list.add(i);

return list;

}

public void testOne() throws Exception {

assertEquals(list(),generate(1));

}

public void testTwo() throws Exception {

assertEquals(list(2),generate(2));

}

public void testThree() throws Exception {

assertEquals(list(3),generate(3));

}

public void testFour() throws Exception {

assertEquals(list(2,2),generate(4));

}

public void testSix() throws Exception {

assertEquals(list(2,3),generate(6));

}

public void testEight() throws Exception {

assertEquals(list(2,2,2),generate(8));

}

public void testNine() throws Exception {

assertEquals(list(3,3),generate(9));

}

}

expected:<[3, 3]> but was:<[9]>

package primeFactors;

The Seventh test.

package primeFactors;

The Seventh test.

package primeFactors;

import java.util.*;

public class PrimeFactors {

public static List<Integer> generate(int n) {

List<Integer> primes = new ArrayList<Integer>();

if (n > 1) {

int candidate = 2;

while (n%candidate == 0) {

primes.add(candidate);

n /= candidate;

Publicité

}

if (n > 1)

primes.add(n);

}

return primes;

}

}

import static primeFactors.PrimeFactors.generate;

import junit.framework.TestCase;

import java.util.*;

public class PrimeFactorsTest extends TestCase {

private List<Integer> list(int... ints) {

List<Integer> list = new ArrayList<Integer>();

for (int i : ints)

list.add(i);

return list;

}

public void testOne() throws Exception {

assertEquals(list(),generate(1));

}

public void testTwo() throws Exception {

assertEquals(list(2),generate(2));

}

public void testThree() throws Exception {

assertEquals(list(3),generate(3));

}

public void testFour() throws Exception {

assertEquals(list(2,2),generate(4));

}

public void testSix() throws Exception {

assertEquals(list(2,3),generate(6));

}

public void testEight() throws Exception {

assertEquals(list(2,2,2),generate(8));

}

public void testNine() throws Exception {

assertEquals(list(3,3),generate(9));

}

}

package primeFactors;

import java.util.*;

public class PrimeFactors {

public static List<Integer> generate(int n) {

List<Integer> primes = new ArrayList<Integer>();

if (n > 1) {

int candidate = 2;

while (n % candidate == 0) {

primes.add(candidate);

n /= candidate;

}

}

if (n > 1)

primes.add(n);

return primes;

}

}

import static primeFactors.PrimeFactors.generate;

import junit.framework.TestCase;

import java.util.*;

public class PrimeFactorsTest extends TestCase {

private List<Integer> list(int... ints) {

List<Integer> list = new ArrayList<Integer>();

for (int i : ints)

list.add(i);

return list;

}

public void testOne() throws Exception {

assertEquals(list(),generate(1));

}

public void testTwo() throws Exception {

assertEquals(list(2),generate(2));

}

public void testThree() throws Exception {

assertEquals(list(3),generate(3));

}

public void testFour() throws Exception {

assertEquals(list(2,2),generate(4));

}

public void testSix() throws Exception {

assertEquals(list(2,3),generate(6));

}

public void testEight() throws Exception {

assertEquals(list(2,2,2),generate(8));

}

public void testNine() throws Exception {

assertEquals(list(3,3),generate(9));

}

}

expected:<[3, 3]> but was:<[9]>

expected:<[3, 3]> but was:<[9]>

package primeFactors;

The Seventh test.

package primeFactors;

The Seventh test.

package primeFactors;

import java.util.*;

public class PrimeFactors {

public static List<Integer> generate(int n) {

List<Integer> primes = new ArrayList<Integer>();

int candidate = 2;

if (n > 1) {

while (n % candidate == 0) {

primes.add(candidate);

n /= candidate;

}

}

if (n > 1)

primes.add(n);

return primes;

}

}

import static primeFactors.PrimeFactors.generate;

import junit.framework.TestCase;

import java.util.*;

public class PrimeFactorsTest extends TestCase {

private List<Integer> list(int... ints) {

List<Integer> list = new ArrayList<Integer>();

for (int i : ints)

list.add(i);

return list;

}

public void testOne() throws Exception {

assertEquals(list(),generate(1));

}

public void testTwo() throws Exception {

assertEquals(list(2),generate(2));

}

public void testThree() throws Exception {

assertEquals(list(3),generate(3));

}

public void testFour() throws Exception {

assertEquals(list(2,2),generate(4));

}

public void testSix() throws Exception {

assertEquals(list(2,3),generate(6));

}

public void testEight() throws Exception {

assertEquals(list(2,2,2),generate(8));

}

public void testNine() throws Exception {

assertEquals(list(3,3),generate(9));

}

}

package primeFactors;

import java.util.*;

public class PrimeFactors {

public static List<Integer> generate(int n) {

List<Integer> primes = new ArrayList<Integer>();

int candidate = 2;

if (n > 1) {

while (n % candidate == 0) {

primes.add(candidate);

n /= candidate;

}

}

if (n > 1)

primes.add(n);

return primes;

}

}

import static primeFactors.PrimeFactors.generate;

import junit.framework.TestCase;

import java.util.*;

public class PrimeFactorsTest extends TestCase {

private List<Integer> list(int... ints) {

List<Integer> list = new ArrayList<Integer>();

for (int i : ints)

list.add(i);

return list;

}

public void testOne() throws Exception {

assertEquals(list(),generate(1));

}

public void testTwo() throws Exception {

assertEquals(list(2),generate(2));

}

public void testThree() throws Exception {

assertEquals(list(3),generate(3));

}

public void testFour() throws Exception {

assertEquals(list(2,2),generate(4));

}

public void testSix() throws Exception {

assertEquals(list(2,3),generate(6));

}

public void testEight() throws Exception {

assertEquals(list(2,2,2),generate(8));

}

public void testNine() throws Exception {

assertEquals(list(3,3),generate(9));

}

}

expected:<[3, 3]> but was:<[9]>

expected:<[3, 3]> but was:<[9]>

package primeFactors;

The Seventh test.

package primeFactors;

The Seventh test.

package primeFactors;

import java.util.*;

public class PrimeFactors {

public static List<Integer> generate(int n) {

List<Integer> primes = new ArrayList<Integer>();

int candidate = 2;

while (n > 1) {

! ! !

while (n % candidate == 0) {

primes.add(candidate);

n /= candidate;

}

candidate++;

}

if (n > 1)

primes.add(n);

return primes;

}

}

import static primeFactors.PrimeFactors.generate;

import junit.framework.TestCase;

import java.util.*;

public class PrimeFactorsTest extends TestCase {

private List<Integer> list(int... ints) {

List<Integer> list = new ArrayList<Integer>();

for (int i : ints)

list.add(i);

return list;

}

public void testOne() throws Exception {

assertEquals(list(),generate(1));

}

public void testTwo() throws Exception {

assertEquals(list(2),generate(2));

}

public void testThree() throws Exception {

assertEquals(list(3),generate(3));

}

public void testFour() throws Exception {

assertEquals(list(2,2),generate(4));

}

public void testSix() throws Exception {

assertEquals(list(2,3),generate(6));

}

public void testEight() throws Exception {

assertEquals(list(2,2,2),generate(8));

}

public void testNine() throws Exception {

assertEquals(list(3,3),generate(9));

}

}

import static primeFactors.PrimeFactors.generate;

import junit.framework.TestCase;

import java.util.*;

public class PrimeFactorsTest extends TestCase {

private List<Integer> list(int... ints) {

List<Integer> list = new ArrayList<Integer>();

for (int i : ints)

list.add(i);

return list;

}

public void testOne() throws Exception {

assertEquals(list(),generate(1));

}

public void testTwo() throws Exception {

assertEquals(list(2),generate(2));

}

public void testThree() throws Exception {

assertEquals(list(3),generate(3));

}

public void testFour() throws Exception {

assertEquals(list(2,2),generate(4));

}

public void testSix() throws Exception {

assertEquals(list(2,3),generate(6));

}

public void testEight() throws Exception {

assertEquals(list(2,2,2),generate(8));

}

public void testNine() throws Exception {

assertEquals(list(3,3),generate(9));

}

}

package primeFactors;

import java.util.*;

public class PrimeFactors {

public static List<Integer> generate(int n) {

List<Integer> primes = new Array...