Created
August 19, 2013 01:30
-
-
Save IuryAlves/6265079 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Fatorial { | |
public int fatorial(int num) throws InvalidNumberException { | |
if (num < 0) | |
return 0; | |
if (num <= 1) { | |
return 1; | |
} else { | |
return fatorial(num - 1) * num; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package edu.fatec.tsw; | |
import junit.framework.Assert; | |
import org.junit.Before; | |
import org.junit.Test; | |
public class FatorialTest { | |
private Fatorial calculador; | |
private int resultado; | |
@Before | |
public void iniciar() { | |
calculador = new Fatorial(); | |
} | |
@Test | |
public void validaCalculoFatorialZero() { | |
analisarResultado(0, 1); | |
} | |
@Test | |
public void validaCalculoFatorialUm() { | |
analisarResultado(1, 1); | |
} | |
@Test | |
public void validaCalculoFatorialCinco() { | |
analisarResultado(5, 120); | |
} | |
@Test(expected=InvalidNumberException.class) | |
public void validaCalculoFatorialNumNegativo() throws InvalidNumberException { | |
calculador.fatorial(-1); | |
} | |
private void analisarResultado(int entrada, int resultadoEsperado) { | |
try { | |
resultado = calculador.fatorial(entrada); | |
Assert.assertEquals(resultadoEsperado, resultado); | |
} catch(InvalidNumberException ex) { | |
Assert.fail("Exce��o inesperada no c�lculo do fatorial."); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment