function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
srikanth kambampati 6srikanth kambampati 6 

Help me to write the test class for the below code.

Hi, i wrote one trigger on account object, when even an account is created automatically create a contact for that account. And my trigger is 
trigger acc_con on Account (after insert) {
    List<Contact> cons = new List<Contact>();
    for(Account a:trigger.new) {
        Contact c =new Contact();
        c.lastName = a.Name;
        c.Phone = a.phone;
        c.AccountId = a.id;
        cons.add(c);
    }
    insert cons;
}
Best Answer chosen by srikanth kambampati 6
Raj VakatiRaj Vakati
Updated  --- 100 % code coverage 
 
@isTest
private class TestAccountTrigger {
    static testmethod void testEx2(){
        Test.startTest();
        Account a = new Account();
        a.Name = 'test';
        insert a;
        Test.stopTest();
        
    }
}

 

All Answers

Raj VakatiRaj Vakati
@isTest
private class TestAccountTrigger {
  static testmethod void testEx2(){
	   Account a = new Account();
  a.Name = 'test';
  insert a;

}
}

 
Raj VakatiRaj Vakati
Updated  --- 100 % code coverage 
 
@isTest
private class TestAccountTrigger {
    static testmethod void testEx2(){
        Test.startTest();
        Account a = new Account();
        a.Name = 'test';
        insert a;
        Test.stopTest();
        
    }
}

 
This was selected as the best answer
balaji Jayararmanbalaji Jayararman
Hi srikanth,

Please find the below code:
@isTest
private class Acc_Con_Test {
	//Success case   
	public static testMethod void test_method_one() {
		Account acc = new Account();
		acc.name = 'Test Account';
		acc.BillingStreet='Test Street';
		acc.BillingCity='Test City';
		acc.BillingState='Test State';
		acc.BillingCountry='USA';
		acc.BillingPostalCode='33442';
		acc.Phone = '9952991642';
		Test.startTest();
		insert acc;
		Test.stopTest();
		Contact con = [SELECT Id, lastName , Phone FROM Contact WHERE AccountId = :acc.Id LIMIT 1];
		System.assertEquals(acc.name, con.lastName, 'Account Name must be equal to Contact LastName');
		System.assertEquals(acc.Phone, con.Phone, 'Account Phone must be equal to Contact Phone');
	}
}


can you please let me know if it helps or not.

if it helps don't forget to mark as best answer.

Thanks,
Balaji J
Ajay K DubediAjay K Dubedi
Hi Srikanth,

How to write test class. You can learn from given below URL.
I hope you understand this.

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_qs_test.htm

https://trailhead.salesforce.com/en/modules/apex_testing/units/apex_testing_intro

//100% code coverage

@isTest
private class AccountTrigger_Test {
    static testmethod void testEx2(){    
        Account a = new Account();
        a.Name = 'Test';
        a.Phone ='12345';
        Test.startTest();
        insert a;
        Test.stopTest();        
    }
}

Please mark it as best Answer if you find it helpful.


Thank You
Ajay Dubedi
srikanth kambampati 6srikanth kambampati 6
Hi,
thanks to all for giving reply to my question and all are giving 100% code coverage.

Thanks & regards
srikanth k