In my Spring boot app I want to write a test to assert SQLIntegrityConstraintViolationException
@Test
public void deleteCar_whenIntegrityConstraintViolationException()
{
doThrow( new SQLIntegrityConstraintViolationException() ).when( carRepository ).deleteCar( anyInt() );
CustomResponse response = carService.deleteCar( 5 );
Assert.assertEquals( "unsuccessful", response.getStatus() );
}
I have set CustomResonse
to be unsuccessful when Exception occurred.
but the problem is Mockito don't allow me to test arbitrary exception as mentioned in this stack answer
If you really want to do this, then have Mockito throw a new RuntimeException() or even better throw a new ArrayIndexOutOfBoundsException() since the API specifies that that is the only valid Exception to be thrown.
is there any other way to test this SQLIntegrityConstraintViolationException
?.