Find your content:

Search form

You are here

Invalid Type for Exception error

 
Share

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:

@isTest
public class MilestoneUtilsTest {

    public static testMethod void buildMilestone_shouldThrowException_nullBusinessHoursId() {

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

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

Save error: Invalid type: BusinessHoursIdNullException

Why is this type invalid?


Attribution to: Joe

Possible Suggestion/Solution #1

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

catch(MilestoneUtils.BusinessHoursIdNullException e) {
            System.assert(true);
}

Attribution to: Samuel De Rycke
This content is remixed from stackoverflow or stackexchange. Please visit https://salesforce.stackexchange.com/questions/1879

My Block Status

My Block Content