0

I'm trying to do a sum in a variable number, and pass a String in assertEquals return a value without changing the variable String attribute.

Calculadora.java

    class Calculadora {
    public void agregar(String resulta) {
        int mensaje = 0;
        if(resulta == "")
        {
            mensaje = 0;
        }
        else if(resulta == null)
        {
            mensaje = 0;
        }
        else if(resulta != null )
        {
            String[] cadena = resulta.split(",\\s*");
            for(int i = 0; i < cadena.length; i++)
            {
                mensaje = mensaje + Integer.parseInt(cadena[i]);
            }
        }
        System.out.println(mensaje);

} }

The test should not change any since, you must exit this result that appears. the issue is the method. CalculadoraTest.java

    import static org.junit.Assert.*;    

@Test
public void agregarRetornaCeroCuandoLaEntradaEsVacia() {

    int resultado = Calculadora.agregar("");
    assertEquals(0, resultado);
}

@Test
public void agregarRetornaCeroCuandoLaEntradaEsNula() {

    int resultado = Calculadora.agregar(null);
    assertEquals(0, resultado);
}

 @Test
    public void agregarRetornaNumeroCuandoLaEntradaSonVariosNumerosEnString() {

        String resultado = Calculadora.agregar("0,1,2,3,4,555");
        assertEquals(565, resultado);
    }

Error in Netbeans

link http://www.evernote.com/l/AcyDryZbGOhP-IkLmls4kK9_GUHboAQq820/

Croelanjr
  • 9
  • 5
  • 1
    `resulta == ""`.... [how-do-i-compare-strings-in-java](http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java). In other words use `equals` method instead of `==`, but make sure that you can use it safely, so either check if `resulta` is not null earlier (you can't call `equals` from `null`), or use Yoda condition like `"".equals(resulta)`. – Pshemo Feb 12 '15 at 17:45
  • 1
    Then once you've fixed that, how can you assert that an *integer* equals a *string*? And how are you expecting to get a result back from a void method? – Jon Skeet Feb 12 '15 at 17:48
  • first declare "resulta" final : public *String* agregar(final String resulta) Then start by making a copy of resulta resultaNew = new String(resulta) Work on the copied string. – Richard Feb 12 '15 at 17:49
  • In testing with JUnit Test, you must exit this result in the test code, you must exit the sum. @Test public void agregarRetornaNumeroCuandoLaEntradaSonVariosNumerosEnString() { String resultado = Calculadora.agregar("0,1,2,3,4,555"); assertEquals(565, resultado); } – Croelanjr Feb 12 '15 at 20:23
  • the String resultado, you must call the class Calculadora. then method agregar – Croelanjr Feb 12 '15 at 20:26

0 Answers0