-2

I am trying to write a test class for a simple Apex Class which sets the status of the Case to be Closed like below

public class updateCaseRecord {    
     @AuraEnabled
    public static void updateCheck(String caseId){
        List<Case> records = [select id,Status from Case WHERE Id = :caseId];
        for (Case record : records)
        {
            record.Status = 'Closed';
        }
        update records;     
    }
}

The test class I have written so far is like below I have created a test account,contact and a case.

@IsTest
public class updateCaseRecordTest {
@IsTest(SeeAllData=true) static void testIsUpdateCheck() {
    ID parentRecType = '012U0000000PqA9IAK';
    ID csRecType     = '012U0000000QGv4IAG'; 

    Account pa = new Account(Name='Test Parent Account', recordTypeId=parentRecType);
    insert pa;

    Account testAccount = new Account(Name='test', Industry='Biotech',Territory__c='EUR',BillingCity='Test',BillingCountry='Germany',ParentId=pa.id);
    insert testAccount; 

    Contact c = new Contact(LastName='Test',LeadSource='Customer Service',AccountId=testAccount.Id);
    insert c;

    Case newCase = new Case();
    newCase.Subject = 'testStatusClosed';
    newCase.Status = 'New';
    newCase.Category__c = 'Inquiry';
    newCase.Sub_Category__c = 'Other';
    newCase.AccountId = testAccount.Id;
    newCase.ContactId = c.id;
    newCase.recordTypeId=csRecType;
    insert newCase;

     ApexPages.standardController std = New ApexPages.standardController(newCase); 

updateCaseRecord uC = new updateCaseRecord();
uC.updateCheck(std);
}}

I get the error like Method does not exist or incorrect signature: void updateCheck(ApexPages.StandardController) from the type updateCaseRecord. How can I test the apex class and can have the assertion statement. I am writing first time test scripts and I am kind of lost. Any help is greatly appreciated

0
3

In your class updateCheck method is static. you can't invoke static method from an instance of the class in Apex. It should be invoked from a class:

instead of

updateCaseRecord uC = new updateCaseRecord();
uC.updateCheck(std);

should be

updateCaseRecord.updateCheck(newCase.Id);

you don't need any standard controller, as for VF controller.


I recommend you to read Get Started with Apex trailhead, not hardcode ids and if possible not to use SeeAllData=true in tests

3
  • Thank you that worked. How can I write an assertion statement here
    – user81642
    Apr 7 '20 at 13:47
  • @user81642 you will need to select this case and verify, that Status is updated with the expected value. Apr 7 '20 at 13:59
  • 1
    Please review the resources in How do I write an Apex unit test? to learn about standard testing practices.
    – David Reed
    Apr 7 '20 at 14:01

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