I am new to JUnit and am practicing asserts using an array. Now, I want to create a method called addNegativeRowItemTest that checks that if one wants to add an item in a negative row, it throws an exception.
In order to test that an element cannot be added in a negative row, and that it throws an exception in case it does, I came up with something like assertsTrue(i>0, inputNumber), but I'm not sure that it really is. work
I am using JUnit 5 version.
These are the functions I have:
public class MatrizAdyacencia {
private boolean[][] _ady;
private int cantElementos=0;
public MatrizAdyacencia(int cant)
{
cantElementos = cant;
_ady = new boolean[cant][cant];
}
public void agregarElemento(int i, int j)
{
_ady[i][j] = true;
_ady[j][i] = true;
}
public void eliminarElemento(int i, int j)
{
_ady[i][j] = false;
_ady[j][i] = false;
}
public boolean existeElemento(int i, int j)
{
return _ady[i][j];
}
public int getCantidadElementos()
{
int cont=0;
for(int i=0; i<=cantElementos-1; i++)
{
for(int j=0; j<=cantElementos-1; j++)
if(_ady[i][j]==true)
cont++;
}
cont = cont/2;
return cont;
}
}
Checking that the method throws an exception when adding an element, could be as simple as:
Basically you're trying to add an element at negative indices and tell it to
JUnit
wait for an exception in that case.The full test would look like: