+ Start a Discussion
Abby StockerAbby Stocker 

APEX TEST CLASS (aurorahandledexception)

I am hoping someone can help me :(

I have a test class that just keeps failing and I do now know what is wrong? When I run the test class, this is the error I get:
"System.AuraHandledException: Script-thrown exception" and the stack trace: Class.CaseFileFormServerController.insertCasesFile: line 56, column 1
Class.CaseFileFormServerControllerTest.insertCasesFileTest: line 55, column 1

Any help is GREATLY appreciated. Thank you!!!
@IsTest
public class CaseFileFormServerControllerTest {
	@TestSetup
    private static void setupTestData() {
        Case mockCase = new Case();
        insert MockCase;
        
        Case_File__c mockCaseFile = new Case_File__c(
        	Case__c = mockCase.Id,
        	Department__c = 'Logistics');
        insert mockCaseFile;
        
        ContentDocumentSelectorTest.createMockContentDocument();
    }
    
    private static testmethod void linkFileToCaseFile_success() {
        Id mockDocumentId = fetchContentDocumentId();
        Id mockCaseFileId = fetchMockCaseFileId();
        Id caseId = fetchCaseId();
        
        String result = CaseFileFormServerController.linkFileToCaseFile(mockDocumentId, mockCaseFileId);
        System.assertEquals('SUCCESS', result, 'An exception was caught: ' + result);
    }
    
    private static testmethod void linkFileToCaseFile_exceptionWasCaught() {
        Id mockAccountId = Id.valueOf('0014B00000WUh5tQAD');
        Id mockCaseFileId = fetchMockCaseFileId();
        Id caseId = fetchCaseId();
        
        String result = CaseFileFormServerController.linkFileToCaseFile(mockAccountId, mockCaseFileId);
        System.assert(result.contains('Exception:'), 'No exception was caught');
    }
    
   
    private static testmethod void insertCasesFileTest() {
        test.startTest();
        PricebookTestDataGenerator.generatePriceBookData();
        Id priceBookEntryId = [SELECT Id FROM PricebookEntry LIMIT 1].Id;
        Id product2Id = [SELECT Id FROM Product2].Id;
        Account account = new Account(Name = 'Test Account');
        INSERT account;
        Order__c order1 = new Order__c(AccountId__c = account.Id);
		INSERT order1;
        
        Order_Line__c orderLine1 = new Order_Line__c();
        orderLine1.Order__c = order1.Id;
        orderLine1.Quantity__c = 1;
        orderLine1.Salesperson__c = UserInfo.getUserId();
        orderLine1.Product2Id__c = product2Id;
        INSERT orderLine1;
        Id mockDocumentId = fetchContentDocumentId();
        Id mockCaseFileId = fetchMockCaseFileId();
        Id caseId = fetchCaseId();
        List<ContentDocument> mockDocument = [SELECT Id, Title FROM ContentDocument LIMIT 1];
        CaseFileFormServerController.insertCasesFile('Test',String.valueOf(caseId),'00022334','Driver',orderLine1.Id,'Product Damage','','Stone Cracked/Chipped','Test',mockDocument);
       	test.stopTest();
        // System.assertEquals('SUCCESS', result, 'An exception was caught: ' + result);
    }
    public static Id fetchContentDocumentId() {
        ContentDocument mockDocument = [SELECT Id, Title FROM ContentDocument LIMIT 1];
        System.assertNotEquals(null, mockDocument, 'Mock document came in as null');
        return mockDocument.Id;
    }
    
    public static Id fetchMockCaseFileId() {
        Case_File__c mockCaseFile = [SELECT Id, Case__c, Department__c FROM Case_File__c LIMIT 1];
        System.assertNotEquals(null, mockCaseFile, 'Mock case file came in as null');
        return mockCaseFile.Id;
    }
    
    public static Id fetchCaseId() {
        Case mockCase = [SELECT Id, CaseNumber FROM Case LIMIT 1];
        System.assertNotEquals(null, mockCase, 'Mock case came in as null');
        return mockCase.Id;
    }
}



 
Maharajan CMaharajan C
Hi Abby,

You can use the try catch to avoid this test failure:
private static testmethod void insertCasesFileTest() {
        test.startTest();
        PricebookTestDataGenerator.generatePriceBookData();
        Id priceBookEntryId = [SELECT Id FROM PricebookEntry LIMIT 1].Id;
        Id product2Id = [SELECT Id FROM Product2].Id;
        Account account = new Account(Name = 'Test Account');
        INSERT account;
        Order__c order1 = new Order__c(AccountId__c = account.Id);
        INSERT order1;
        
        Order_Line__c orderLine1 = new Order_Line__c();
        orderLine1.Order__c = order1.Id;
        orderLine1.Quantity__c = 1;
        orderLine1.Salesperson__c = UserInfo.getUserId();
        orderLine1.Product2Id__c = product2Id;
        INSERT orderLine1;
        Id mockDocumentId = fetchContentDocumentId();
        Id mockCaseFileId = fetchMockCaseFileId();
        Id caseId = fetchCaseId();
        List<ContentDocument> mockDocument = [SELECT Id, Title FROM ContentDocument LIMIT 1];
        try
        {           CaseFileFormServerController.insertCasesFile('Test',String.valueOf(caseId),'00022334','Driver',orderLine1.Id,'Product Damage','','Stone Cracked/Chipped','Test',mockDocument);
        }
        catch(exception e)
        {
            
        }     
           test.stopTest();
    }

https://developer.salesforce.com/forums/?id=9062I000000QvoqQAC

Thanks,
Maharajan.C