+ Start a Discussion
Dhilip DussaDhilip Dussa 

Need Help Lightining test class

Hi,
I need Help on Test class with assert how to add assert in this test class

public class TestClass{
 @AuraEnabled
    public static Account returnAccount(Id ParentId) {
        return [Select Name, FirstName, LastName, Id from Account where id =: ParentId];
    }
    
    }
    ====================================================================
    @isTest
    public static void test(){
    Account a = new Account();
    a.Name='Test'
    insert a;
    
    TestClass.returnAccount(a.id);
    //need add assert here if I comment above line assert has to fail o/w it hass to pass
    }

Thanks
Alain CabonAlain Cabon

Account a = TestClass.returnAccount(a.id);
System.assertEquals('Test', a.Name,'Account Name different');


System.assertEquals(expected, actual, msg)
Asserts that the first two arguments are the same. If they are not, a fatal error is returned that causes code execution to halt.
msg Type: Object (Optional) Custom message returned as part of the error message.
Deepali KulshresthaDeepali Kulshrestha
Hi Dhilip,


I have gone through your problem, Please try below code:

@isTest
public class TestClass_Test {
    
    @isTest private static void test()
    {
        Account a = new Account();
        a.Name='Test';
        insert a;
        
        TestClass.returnAccount(a.id);
        Test.startTest();
        Account acc= [select Id,Name from Account];
        system.assertEquals('Test', acc.Name);
        Test.stopTest();
    }
}
 

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Deepali Kulshrestha
www.kdeepali.com