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
santhosh konathala 17santhosh konathala 17 

can anybody Suggest how to write a Test class for my below class?I am new to Apex coding.plz help me?

public  class Notificationserviceclass {
List<Notification__c> notifylist1 = new list<Notification__c>();
List<Notification__c> notifylist2 = new list<Notification__c>();
public void onAfterInsert(Account[] newAccounts){
for(Account accname:newAccounts)
{
Notification__c notifyvar=new Notification__c();
notifyvar.name=accname.name;
notifyvar.Action__c='Add';
notifylist2.add(notifyvar);
}
insert notifylist2;
}
public void onAfterUpdate(Account[] newAccounts, map<id, Account> oldMap ){
 
for(Account Acc:newAccounts){
 
 Account oldname = oldMap.get(Acc.Id);
 
if(Acc.name!=oldname.name){
 
Notification__c memb = new Notification__c();
 
memb.name=Acc.name;
 
       memb.Action__c= 'Modify';
 
       notifylist1.add(memb);
 
       }
 
       if(notifylist1.size()>0 && notifylist1.size()!=null)
 
       {
 
       insert notifylist1;
 
       }
 
       }
 
     }
 
       }
 
Best Answer chosen by santhosh konathala 17
Medhya MahajanMedhya Mahajan
Hi Santhosh, 

Here is the Test Class you can use :
 
@isTest
private class Notificationserviceclass_Test{

	private static testMethod void testnotificationService(){
	
	Test.startTest();
		Account acc = new Account();
		acc.Name = 'Test Account';
		insert acc;
		acc.Name = 'Update Test Account';
		update acc;
	Test.stopTest();
	}

}
Mark solved if this helps.

Regards
Medhya Mahajan

All Answers

Medhya MahajanMedhya Mahajan
Hi Santhosh, 

Here is the Test Class you can use :
 
@isTest
private class Notificationserviceclass_Test{

	private static testMethod void testnotificationService(){
	
	Test.startTest();
		Account acc = new Account();
		acc.Name = 'Test Account';
		insert acc;
		acc.Name = 'Update Test Account';
		update acc;
	Test.stopTest();
	}

}
Mark solved if this helps.

Regards
Medhya Mahajan
This was selected as the best answer
santhosh konathala 17santhosh konathala 17
Great Thank you Medhya Mahajan ...
Now my class code coverage has been reached to 100%.Thank you soo much
santhosh konathala 17santhosh konathala 17
Hi medha ,

Have a small doubt how your Test class covered 100% code coverage for my Given class.

In this Test class you have not used my class name .Now how your test class refer my Apex class.plz tell me?
Medhya MahajanMedhya Mahajan
Santhosh, 

You need not specifically write the name of the Class to achieve the coverage. 

My test class is only inserting and updating account record which is the functionality of your class. 

I am guessing their might be a trigger on account for which your class is written.

When I insert or update an account in my Test class that trigger is fired causing your class to run and hence it is covered 

Regards
Medhya Mahajan
santhosh konathala 17santhosh konathala 17
Thanks for the reply.Now, I got it.