You need to sign in to do that
Don't have an account?
Ram S 49
Hi I want to write a test case for a method which throws custom exception, how can i achieve.
Method containing custom exception
/*
public static decimal CelsiusToKelvin (decimal degree)
{
if(degree<-273.16)
{ result = -273.16;
throw new InvalidDataException('Value cannot be less than -273.16');
}
}
*/
My Test method
class TemperatureTest {
@isTest static void testCelsiusToKelvin()
{
//What to do here?
}
}
/*
public static decimal CelsiusToKelvin (decimal degree)
{
if(degree<-273.16)
{ result = -273.16;
throw new InvalidDataException('Value cannot be less than -273.16');
}
}
*/
My Test method
class TemperatureTest {
@isTest static void testCelsiusToKelvin()
{
//What to do here?
}
}
In your test method write something like this with your class name
This is the sample class i have tried and it has 100% coverage.
SampleClass
SampleClassTest
Let me know if you have other questions. If this works for you mark this reply as best answer.
Thanks.
lnalluri, thnk for your reply, How will you check this test class passed using System.assertion.
bcz I am getting error, as the method CelsiusToKelvin() will return the exception.
System.assertEquals(//what to do);
How to check if custom exception have been thrown? using system.assert methods.
I'm attaching a sample code for the same Sample Test class for the same
Hope this will help if yes then give a thumbs up.