package tdd.kata; import static org.junit.Assert.*; import java.util.Random; import org.junit.Before; import org.junit.Test; public class CalculadoraTest { Calculadora calculadora; @Before public void prepara(){ calculadora = new Calculadora(); } @Test public void testComUmaStringVaziaDeveRetornarZero() { Integer resultado = calculadora.add(""); assertEquals(new Integer(0), resultado); } private Integer randomPositivoMenorQueMil(){ Random numeroQualquer = new Random(); return numeroQualquer.nextInt(1000); } @Test public void testComUmaStringContendoUmNumeroRetornarUmNumero() { Integer numeroGerado = randomPositivoMenorQueMil(); Integer resultado = calculadora.add(numeroGerado.toString()); assertEquals(numeroGerado, resultado); } @Test public void testComUmaStringContendoUmEDoisRetornarTres() { Integer resultado = calculadora.add("1,2"); assertEquals(new Integer(3), resultado); } @Test public void testComUmaStringContendoUmEDoisTresRetornaSeis() { Integer resultado = calculadora.add("1,2,3"); assertEquals(new Integer(6), resultado); } @Test public void testComUmaStringContendoVirgulaeNewLine() { Integer resultado = calculadora.add("1,2\n3"); assertEquals(new Integer(6), resultado); } @Test public void testComUmDelimitadorEscolhido() { Integer resultado = calculadora.add("//|\n1|2"); assertEquals(new Integer(3), resultado); } @Test(expected=Exception.class) public void testDeveLancarExceptionComNegativos() { calculadora.add("//|\n-11|2"); } @Test public void testDeveLancarExceptionComMensagemQundoReceberNegativos() { Exception exception = null; try{ calculadora.add("//|\n-11|-22"); }catch(RuntimeException e){ exception = e; } assertTrue(exception.getMessage().contains("-11")); assertTrue(exception.getMessage().contains("-22")); } @Test public void testDeveIgnorarNumerosMaioresQueMil() { Integer resultado = calculadora.add("2,1001"); assertEquals(new Integer(2), resultado); } }