Last active
May 11, 2017 16:55
-
-
Save fkenjikamei/3d0f710374e018ec35e05cbf979cea79 to your computer and use it in GitHub Desktop.
Java: Testes Unitários e Exceções
This file contains hidden or 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 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()); | |
} | |
} | |
} |
This file contains hidden or 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 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