Skip to content

Instantly share code, notes, and snippets.

@delucas
Created April 21, 2013 23:54
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 delucas/5431620 to your computer and use it in GitHub Desktop.
Save delucas/5431620 to your computer and use it in GitHub Desktop.
UNTreF - Ejemplo de Pruebas: Clase CajaDeAhorros
package ar.edu.untref.pruebas;
public class CajaDeAhorros {
private String titular;
private double saldo = 0.0;
public CajaDeAhorros(String titular) {
this.titular = titular;
}
public String getTitular() {
return titular;
}
public void depositar(double monto) {
if (monto < 0.0) {
throw new Error("No puede depositarse un monto negativo");
}
this.saldo = this.saldo + monto;
}
public void extraer(double monto) {
if (monto > this.saldo) {
throw new Error("Saldo insuficiente");
}
if (monto < 0.0) {
throw new Error("No puede extraerse un monto negativo");
}
this.saldo = this.saldo - monto;
}
public double getSaldo() {
return this.saldo;
}
}
package ar.edu.untref.pruebas;
import junit.framework.Assert;
import org.junit.Test;
public class CajaDeAhorrosTests {
@Test
public void testQuePuedoDepositar() {
CajaDeAhorros caja = new CajaDeAhorros("yo");
caja.depositar(100.0);
Assert.assertEquals(100.0, caja.getSaldo());
}
@Test(expected=Error.class)
public void testQueNoPuedoDepositarNegativos() {
CajaDeAhorros caja = new CajaDeAhorros("yo");
caja.depositar(-100.0);
}
@Test
public void testQuePuedoExtraer() {
CajaDeAhorros caja = new CajaDeAhorros("yo");
caja.depositar(100.0);
caja.extraer(10.0);
Assert.assertEquals(90.0, caja.getSaldo());
}
@Test(expected=Error.class)
public void testQueNoPuedoExtraer() {
CajaDeAhorros caja = new CajaDeAhorros("yo");
caja.extraer(10.0);
}
@Test(expected=Error.class)
public void testQueNoPuedoExtraerCantidadNegativa() {
CajaDeAhorros caja = new CajaDeAhorros("yo");
caja.depositar(100.0);
caja.extraer(-10.0);
}
@Test(expected=Error.class)
public void testQueNoPuedoExtraerCantidadNegativaSinTenerNada() {
CajaDeAhorros caja = new CajaDeAhorros("yo");
caja.extraer(-10.0);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment