I want to test that an exception is being thrown in one of my apex classes. This is the beginning of my class that I want to test. It declares a custom exception class and throws that exception from a method:

public class MilestoneUtils {

    public class BusinessHoursIdNullException extends Exception {}

    public static Milestone__c buildMilestone(Case parentCase, Integer durationInMins, Id businessHoursId) {
        if(businessHoursId == null) {
            throw new BusinessHoursIdNullException('BusinessHoursId cannot be null in MilestoneUtils.buildMilestone(parentCase, durationInMins, businessHoursId)');

This is the start of my test class:

public class MilestoneUtilsTest {

    public static testMethod void buildMilestone_shouldThrowException_nullBusinessHoursId() {

        Case c = getValidCase();
        try {
            MilestoneUtils.buildMilestone(c, 100, null);
        } catch(BusinessHoursIdNullException e) {

I am getting the following error when I try to save my test class:

Save error: Invalid type: BusinessHoursIdNullException

Why is this type invalid?


It's an innerclass, adding MilestoneUtils to the type definition should work.

catch(MilestoneUtils.BusinessHoursIdNullException e) {

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.