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
Ejemplo de como indicar en JUnit que se espera que un determinado escenario produzca un error. |
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 Calculadora { | |
public double dividir(double dividendo, double divisor) { | |
if (divisor == 0) { | |
throw new Error("Divisor debe ser distinto de cero"); | |
} | |
return dividendo / divisor; | |
} | |
} |
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
import org.junit.Assert; | |
import org.junit.Test; | |
public class CalculadoraTest { | |
@Test | |
public void probarQueDivideUnoEnUno() { | |
// Arrange | |
Calculadora calculadora = new Calculadora(); | |
double dividendo = 2; | |
double divisor = 1; | |
// Act | |
double division = calculadora.dividir(dividendo, divisor); | |
// Assert | |
Assert.assertEquals(1, division, 0.0); | |
// Annihilate | |
calculadora = null; | |
} | |
@Test(expected = Error.class) | |
public void probarQueDaErrorSiDivisorEsCero() { | |
// Arrange | |
Calculadora calculadora = new Calculadora(); | |
double dividendo = 1; | |
double divisor = 0; | |
// Act | |
calculadora.dividir(dividendo, divisor); | |
// Assert | |
// Annihilate | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment