+ Start a Discussion
SainSain 

what is system.assertequal and static{}?

Hi,

any one can explain me, why we use System.assertEqual() and static{ } in test class with example.

Thanks in Advance!!!
Aurélien.LavalAurélien.Laval
Hi Sain,

In test class, you have to use System.assertEqual() method when you want to verify (static method of the System class) the result of your tests to be sur your Apex code works like you want.

This is a example of an Apex test class :
@isTest
private class ApexClass_TEST {

	static Account anAccount;
	static String theName;

	static void init(){
		theName = 'My account';

		/** Account **/
			anAccount = new Account(
				Name = theName
			);
			
			insert anAccount;			
	}

    static testMethod void myTest() {
        init();
        
        Test.startTest();
        
        Account insertedAccount = [
        	SELECT Id, Name
        	FROM Account
        	WHERE Id = :anAccount.Id
        ];

        System.assertEquals(theName, insertedAccount.Name);
    }
}

I don't know why test method are static but it's said in the documentation. :)

Let me know if you understand my example.

Aurélien