Skip to content

Instantly share code, notes, and snippets.

@IuryAlves
Created August 19, 2013 01:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save IuryAlves/6265079 to your computer and use it in GitHub Desktop.
Save IuryAlves/6265079 to your computer and use it in GitHub Desktop.
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;
}
}
}
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