You need to sign in to do that
Don't have an account?
ztrank
Invalid Conversion at runtime type MyException to String
I have a class that extracts the Type name from an object Instance
In my test class I force a custom exception (declared in the test class) to be thrown, which I am expecting.
Here is the exception declaration
Any ideas?
public static String extractTypeName(Object classInstance) { return String.valueOf(classInstance).split(':').get(0); }
In my test class I force a custom exception (declared in the test class) to be thrown, which I am expecting.
catch (exception e) { System.assert(e instanceof MyException, e.getMessage()); System.assertEquals('clear exception', e.getMessage()); }This works when I run the test in the sandbox and the test succeeds. However when I run it in production it fails the first assertions and the message I get is "Invalid conversion from runtime type testClass.MyException to String"
Here is the exception declaration
private class MyException extends Exception {}
Any ideas?
It almsot sounds like you called extractTypeName with your exception as a parameter. The issue is caused because you were trying to cast MyException to a String somewhere in your code.
On another note. You have a potential Null Pointer exception in your code return String.valueOf(classInstance).split(':').get(0);
You should check if classInstance is null. and maybe surround String.valueOf(classInstance) with try catch in case someone passes a wrong value