For testing purposes, at times I'd like to see how my code handles having an unexpected runtime exception thrown, from various parts of the code.
For example, say I have a method like so:
public int getSum(int x, int y)
{
return x + y;
}
Is there a way for me to cause this method to fail whenever it's called by throwing an exception instead of returning a valid result? I know I could add in explicit code to do this, but I'd like to know if it's possible to replace a method in any class with a new method that just throws an exception at runtime, or something like that ;-) Kind of like how Mockito can do the following:
doThrow(new Exception()).when(mockedObject).methodReturningVoid(...);
I'd prefer any solutions to not require any extra tools - just the standard JVM. Thanks!