Skip to content

Instantly share code, notes, and snippets.

@fkenjikamei
Last active May 11, 2017 16:55
Show Gist options
  • Save fkenjikamei/3d0f710374e018ec35e05cbf979cea79 to your computer and use it in GitHub Desktop.
Save fkenjikamei/3d0f710374e018ec35e05cbf979cea79 to your computer and use it in GitHub Desktop.
Java: Testes Unitários e Exceções
package bo;
import javax.swing.JOptionPane;
public class Conta {
private double saldo;
public void depositar(double valor) throws NumberFormatException, Exception {
if(valor > 0)
this.saldo += valor;
else {
throw new Exception("Valor invalido");
}
}
public static void main(String[] args) throws Exception {
Conta c = new Conta();
try {
c.depositar(0.0);
System.out.println("Teste");
}catch(NumberFormatException e) {
JOptionPane.showMessageDialog(null, e.getMessage());
}catch(Exception e) {
JOptionPane.showMessageDialog(null, e.getMessage());
}
}
}
package bo;
import static org.junit.Assert.*;
import org.junit.Test;
import junit.framework.TestCase;
public class ContaTest extends TestCase {
@Test
public void testDeveriaPermitirDepositarSeValorMaiorQueZero() throws Exception {
Conta conta = new Conta();
conta.depositar(10.0);
}
@Test
public void testNaoDevePassarSeValorMenorIgualAZero() throws Exception {
Conta conta = new Conta();
try {
conta.depositar(0.0);
fail("Não deveria ter chegado aqui!");
}catch(Exception e) {
assertFalse(false);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment