I intend to create a Junit test for the following enter method. I want to create a test for quantity<0, it should return a value of 1 (iCodErr=1)
public int ingresar(double cantidad) {
int iCodErr;
if (cantidad < 0)
{
System.out.println("No se puede ingresar una cantidad negativa");
iCodErr = 1;
}
else if (cantidad == -3)
{
System.out.println("Error detectable en pruebas de caja blanca");
iCodErr = 2;
}
else
{
dSaldo = dSaldo + cantidad;
iCodErr = 0;
}
return iCodErr;
}
The test I have designed is as follows:
import static org.junit.Assert.*;
import org.junit.Assert;
import org.junit.Test;
public class CCuenta_Test_1 {
@SuppressWarnings("deprecation")
@Test
public void testIngresar() {
CCuenta objCuenta = new CCuenta(); //creo instancia
double resultado = objCuenta.ingresar(-5.0); // para llamar al metodo ingresar
//variable con el resultado esperado del metodo ingrear (1.0)
double esperado = 1;
assert.assertEquals(esperado,resultado);
}
}
But eclipse underlines the last line in red and tells me: Type mismatch: cannot convert from void to boolean Can someone help me.
Simply:
assert
is a Java keyword independent of JUnit , although the meaning is more or less related. It doesn't return a value, so it's a void expression and has no methods. That is the error it tells you.What you do with JUnit is normally a static import of org.junit.Assert
and that makes it possible to call the static methods of Assert directly, among which is assertEquals . If you didn't do the import static , you would have to do (note the capital A, here references the class):