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
Saie Shendage 7Saie Shendage 7 

Writing test class for apex controller?

public class Page1_Controller {
    //Add Society
    public Society__c soc{get; set;}
    
    //Add Members
    public List<wrapMember> wrapMemberList {get; set;}
    public List<Member__c> selectedMembers{get;set;}
    public Id socId;
    
    //Add Secretary
    public String selectedSecretary{get;set;}
    public List<selectOption> societyMembers{get;set;}
    
    public Page1_Controller(){
        //Add Society
        soc=new Society__c();
        
        //Add Members
        if(wrapMemberList == null) {
            wrapMemberList = new List<wrapMember>();
            for(Member__c a: [select Id,Name from Member__c]) {
                wrapMemberList.add(new wrapMember(a));
            }
        }
        
        //Add Secretary
        //selectedSecretary='';
        societyMembers=new List<selectOption>();
        societyMembers.add(new selectoption('--Select Secretary--','--Select Secretary--'));
        for(Member__c m:[select id,name from member__c]){
            societyMembers.add(new SelectOption(m.id,m.Name));
        }
        
    }
    
    //Add Society
	public PageReference save()  
    {
        try{
            upsert soc; 
            socId=soc.Id;
        }catch(DmlException ex)
        {
   			ApexPages.addMessage(new ApexPages.message(ApexPages.Severity.FATAL, ex.getMessage()));
			return ApexPages.currentPage();
        }
        
     	return ApexPages.currentPage();
    }   
    
    //Add Members
    public void processSelected() {
    selectedMembers = new List<Member__c>();
 
        for(wrapMember wrapMemberObj : wrapMemberList) {
            if(wrapMemberObj.selected == true) {
                selectedMembers.add(wrapMemberObj.mem);
            }
        }
        
    }
 
    
    public class wrapMember {
        public Member__c mem {get; set;}
        public Boolean selected {get; set;}
 
        public wrapMember(Member__c a) {
            mem = a;
            selected = false;
        }
    }
    
    public void addMembers(){
        for(Member__c c:selectedMembers){
            c.Society_Name__c=socId;
        }
        update selectedMembers;
    }
    
    public PageReference addSecretary(){
        soc.Secretary__c=selectedSecretary;
        update soc;
        return ApexPages.currentPage();
    }
    
}
 
@istest
public class TestPage1 {
    @istest public static void testSaveSociety(){
        Society__c testSoc=new Society__c();
        testSoc.Name='Some Name';
        testSoc.Reg_Code__c='12345';
        testSoc.Location__c='Some Location';
        testSoc.Formation_Year__c='2012';
        
        Test.setCurrentPage(Page.Page1);
        Page1_Controller controller=new Page1_Controller();
        controller.soc=testSoc;
        PageReference actual=controller.save();
        PageReference expected=Page.Page1;
        system.assertEquals(expected.getUrl(), actual.getUrl());
    }
}
I need to write test class for above custom controller. I have written just one test method, but not able to figure out how to write other tests. Can someone give me code for test class? Please help.
Best Answer chosen by Saie Shendage 7
Malika Pathak 9Malika Pathak 9

Please find the solution of Page2_Controller class.

Test class

@isTest
public class Page2_ControllerTest {
@isTest
    public static void page2(){
         Society__c testSoc=new Society__c();
        testSoc.Name='Some Name';
        testSoc.Reg_Code__c='12345';
        testSoc.Location__c='Some Location';
        testSoc.Formation_Year__c='2012';
        insert testSoc;
        
        Member__c memObj=new Member__c();
        memObj.Name='check';
        memObj.Society_Name__c=testSoc.id;
        insert memObj;
        Member__c memObj2=new Member__c();
        memObj2.Name='check2';
        memObj2.Society_Name__c=testSoc.id;
        insert memObj2;
        test.startTest();
        Page2_Controller page2Obj=new Page2_Controller();
        page2Obj.getSocieties();
        page2Obj.previous();
        page2Obj.next();
        test.stopTest();
    }
}
 

Apex controller

public class Page2_Controller {
    private final integer MAX_RECORDS_PER_PAGE = 5;
    public integer startingFrom {get; set;}
    private integer socRecordCount; 
    //View Society
    public Page2_Controller(){
        if(startingFrom == NUll)
            startingFrom=0;
        socRecordCount = [select count() from Society__c];
    }
    public List<Society__c> getSocieties(){
        List<Society__c> results=[select name,Secretary__c,No_Of_Members__c,location__c 
                                  from Society__c limit :MAX_RECORDS_PER_PAGE
                                  offset : startingFrom];
        return results;  
    }
    
    public PageReference previous()
    {
        if(startingFrom <= 0)
        {   
            startingFrom = 0;
        } else if(startingFrom >= socRecordCount)
            startingFrom = startingFrom - MAX_RECORDS_PER_PAGE;
        else if(startingFrom < socRecordCount)
        {
            startingFrom = startingFrom - MAX_RECORDS_PER_PAGE;
        }
        
        return ApexPages.currentPage();
    }
    
    public PageReference next()
    {
        if((socRecordCount - startingFrom) > MAX_RECORDS_PER_PAGE)
            startingFrom = startingFrom + MAX_RECORDS_PER_PAGE;
        return ApexPages.currentPage();
    }  
}


Please appreciate my effort with giving me Best Answer.

Thanks.

All Answers

Saie Shendage 7Saie Shendage 7
<apex:page controller="Page1_Controller">
    <script type="text/javascript">
        function selectAllCheckboxes(obj,receivedInputID){
            var inputCheckBox = document.getElementsByTagName("input");
            for(var i=0; i<inputCheckBox.length; i++){
                if(inputCheckBox[i].id.indexOf(receivedInputID)!=-1){
                    inputCheckBox[i].checked = obj.checked;
                }
            }
        }
    </script>
    <apex:form >
    <apex:pageBlock >
    	<apex:pageBlockSection title="Add Society" collapsible="true">
                <apex:panelGrid columns="4">
                    <apex:outputText value="Name" />
                	<apex:inputField value="{!soc.Name}" required="true"/>
                    
                    <apex:outputText value="Reg Code" />
                    <apex:inputField value="{!soc.Reg_Code__c}"/>
                    
                    <apex:outputText value="Location" />
                    <apex:inputField value="{!soc.Location__c}"/>
                    
                    <apex:outputText value="Formation Year" />
                    <apex:inputField value="{!soc.Formation_Year__c}"/>
                    
                    <apex:commandButton action="{!save}" value="Save Society"/>
                </apex:panelGrid>
        </apex:pageBlockSection>
        
        <apex:pageBlockSection title="Add Members" columns="2" collapsible="true">
            	<apex:pageBlockTable value="{!wrapMemberList}" var="memWrap" id="table" title="All Members">
                <apex:column >
                    <apex:facet name="header">
                    	<apex:inputCheckbox onclick="selectAllCheckboxes(this,'inputId')"/>
                    </apex:facet>
                	<apex:inputCheckbox value="{!memWrap.selected}" id="inputId"/>
                </apex:column>
                <apex:column value="{!memWrap.mem.Name}" />
            </apex:pageBlockTable>
                
            <apex:pageBlockTable value="{!selectedMembers}" var="c" id="table2" title="Selected Members">
            	<apex:column value="{!c.Name}" headerValue="Name"/>  
            </apex:pageBlockTable>
            <apex:commandButton value="Show Selected Members" action="{!processSelected}" rerender="table2"/>
			<apex:commandButton value="Add Selected Members" action="{!addMembers}"/>
        </apex:pageBlockSection>
        
        <apex:pageBlockSection title="Add Secretary" collapsible="true">
        	Secretary:
            <apex:selectList value="{!selectedSecretary}" size="1">
                <apex:selectOptions value="{!societyMembers}"/>
            </apex:selectList>
            <apex:commandButton action="{!addSecretary}" value="Add Secretary" rerender="pbt"/>
        </apex:pageBlockSection>
    </apex:pageBlock>
    </apex:form>
</apex:page>

This is my vf page. Yes, Member is child of Society with lookup relationship. And Society is also child of Member for Secreatary field(which would be one of the members).
Malika Pathak 9Malika Pathak 9

Hi Saie,

Please find the solution.  Writing test class for apex controller?

You just need to change one line in your apex i.e.   selected = true; then test covrage would come 92% and if selected = false; then test coverage would come 87%.

Test class

@istest public static void testSaveSociety(){
        Society__c testSoc=new Society__c();
        testSoc.Name='Some Name';
        testSoc.Reg_Code__c='12345';
        testSoc.Location__c='Some Location';
        testSoc.Formation_Year__c='2012';
        insert testSoc;
        
        Member__c memObj=new Member__c();
        memObj.Name='check';
        memObj.Society_Name__c=testSoc.id;
        insert memObj;
        Member__c memObj2=new Member__c();
        memObj2.Name='check2';
        memObj2.Society_Name__c=testSoc.id;
        insert memObj2;
        
        test.startTest();
        Page1_Controller page1=new Page1_Controller();
        page1.save();
        page1.processSelected();
        page1.addMembers();
        page1.addSecretary();
        test.stopTest();
        
    }
Apex controller
public class Page1_Controller {
    //Add Society
    public Society__c soc{get; set;}
    
    //Add Members
     public List<wrapMember> wrapMemberList {get; set;}
    public List<Member__c> selectedMembers{get;set;}
    public Id socId;
    
    //Add Secretary
    public String selectedSecretary{get;set;}
    public List<selectOption> societyMembers{get;set;}
    
    public Page1_Controller(){
        //Add Society
        soc=new Society__c();
        
        //Add Members
        if(wrapMemberList == null) {
            wrapMemberList = new List<wrapMember>();
            for(Member__c a: [select Id,Name from Member__c]) {
                wrapMemberList.add(new wrapMember(a));
            }
        }
        system.debug('wrapMemberList '+wrapMemberList);
        
        //Add Secretary
        //selectedSecretary='';
        societyMembers=new List<selectOption>();
        societyMembers.add(new selectoption('--Select Secretary--','--Select Secretary--'));
        for(Member__c m:[select id,name from member__c]){
            societyMembers.add(new SelectOption(m.id,m.Name));
        }
         system.debug('societyMembers '+societyMembers);
    }
    
    //Add Society
	public PageReference save()  
    {
        try{
            upsert soc; 
            socId=soc.Id;
        }catch(DmlException ex)
        {
   			ApexPages.addMessage(new ApexPages.message(ApexPages.Severity.FATAL, ex.getMessage()));
			return ApexPages.currentPage();
        }
        
     	return ApexPages.currentPage();
    }   
    
    //Add Members
    public   void processSelected() {
    selectedMembers = new List<Member__c>();
 
        for(wrapMember wrapMemberObj : wrapMemberList) {
            if(wrapMemberObj.selected == true) {
                selectedMembers.add(wrapMemberObj.mem);
            }
        }
        
    }
 
    
    public class wrapMember {
        public Member__c mem {get; set;}
        public Boolean selected {get; set;}
 
        public wrapMember(Member__c a) {
            mem = a;
            selected = true;
        }
    }
    
    public void addMembers(){
        for(Member__c c:selectedMembers){
            c.Society_Name__c=socId;
        }
        update selectedMembers;
    }
    
    public PageReference addSecretary(){
        soc.Secretary__c=selectedSecretary;
        update soc;
        return ApexPages.currentPage();
    }
    
}


Saie ,I hope this solution is helpful for you.Please let me know it is working or not??

Please mark best answer so that other people would take reference from it.

Thanks

Saie Shendage 7Saie Shendage 7
trigger MemberSumTrigger on Member__c (after insert, after undelete, after update) {
	Set<Id> parentIdsSet = new Set<Id>();
    List<Society__c> societyListToUpdate = new List<Society__c>();
    IF(Trigger.IsAfter){
        IF(Trigger.IsInsert || Trigger.IsUndelete || Trigger.IsUpdate){
            FOR(Member__c c : Trigger.new){
                if(c.Society_Name__c!=null){   
                   parentIdsSet.add(c.Society_Name__c); 
                }
            }
        }
        IF(Trigger.IsDelete){
            FOR(Member__c c : Trigger.Old){
                if(c.Society_Name__c!=null){   
                   parentIdsSet.add(c.Society_Name__c); 
                }
            }
        }
    }
  
    List<Society__c> societyList = new List<Society__c>([Select id ,Name, No_of_Members__c, (Select id, Name From Members__r) from Society__c Where id in:parentIdsSet]);
    FOR(Society__c s : societyList){
        List<Member__c> memberList = s.Members__r;
        s.No_of_Members__c = memberList.size();
        societyListToUpdate.add(s);
    }
    try{
        update societyListToUpdate;
    }catch(System.Exception e){
        
    }	
}
<apex:page controller="Page2_Controller">
    <apex:pageBlock >
    	<apex:pageBlockSection >
        	<apex:pageBlockTable value="{!Societies}" var="s">
                
                <apex:column value="{!s.name}"/>
                <apex:column value="{!s.Secretary__c}"/>
                <apex:column value="{!s.No_of_Members__c}"/>
                <apex:column >
                    <apex:facet name="header">Location</apex:facet>
                    <apex:outputLink value="https://maps.google.com" target="new">
                    <apex:outputText value="{!s.Location__c}"/>
                    	<apex:param name="q" value="{!s.Location__c}"/>
                    </apex:outputLink>
        		</apex:column>
    		</apex:pageBlockTable>
        </apex:pageBlockSection>
    </apex:pageBlock>
    
    <apex:form >
        <apex:inputHidden value="{!startingFrom}"/>
        <apex:panelGrid columns="2" cellpadding="2px">
            <apex:commandLink action="{!previous}" value="Previous"/>
            <apex:commandLink action="{!next}" value="Next"/>
        </apex:panelGrid>
    </apex:form>
    
</apex:page>
 
public class Page2_Controller {
    private final integer MAX_RECORDS_PER_PAGE = 5;
    public integer startingFrom {get; set;}
    private integer socRecordCount; 
	//View Society
    public Page2_Controller(){
        if(startingFrom == NUll)
            startingFrom=0;
        socRecordCount = [select count() from Society__c];
    }
    public List<Society__c> getSocieties(){
        List<Society__c> results=[select name,Secretary__c,No_Of_Members__c,location__c 
                                  from Society__c limit :MAX_RECORDS_PER_PAGE
               offset : startingFrom];
        return results;  
    }
    
    public PageReference previous()
    {
     	if(startingFrom <= 0)
        {   
            startingFrom = 0;
        } else if(startingFrom >= socRecordCount)
            startingFrom = startingFrom - MAX_RECORDS_PER_PAGE;
        else if(startingFrom < socRecordCount)
        {
            startingFrom = startingFrom - MAX_RECORDS_PER_PAGE;
        }
        
        return ApexPages.currentPage();
    }
    
	public PageReference next()
    {
        if((socRecordCount - startingFrom) > MAX_RECORDS_PER_PAGE)
            startingFrom = startingFrom + MAX_RECORDS_PER_PAGE;
        return ApexPages.currentPage();
    }  
    
    
}
Thank you so much Malika! Can you please provide test classes for above trigger and Page_Controller class also? Thank you in advance.
Malika Pathak 9Malika Pathak 9

Hi Saie,

Please find the solution of your Trigger. This Test Class coverage is 94%.

 When you write trigger then no need to call class and method your trigger class is executed automatically when you start run test.

Test Class

@isTest
public class MemberSumTriggerTest {
@isTest
    public static void testTrigger(){
         Society__c testSoc=new Society__c();
        testSoc.Name='Some Name';
        testSoc.Reg_Code__c='12345';
        testSoc.Location__c='Some Location';
        testSoc.Formation_Year__c='2012';
        insert testSoc;
        
        Member__c memObj=new Member__c();
        memObj.Name='check';
        memObj.Society_Name__c=testSoc.id;
        insert memObj;
        Member__c memObj2=new Member__c();
        memObj2.Name='check2';
        memObj2.Society_Name__c=testSoc.id;
        insert memObj2;
        delete memObj2;
    }
}

Apex Controller

You just need to include either before delete or after delete in your trigger

trigger MemberSumTrigger on Member__c (after insert, after undelete, after update,before delete,after delete) {
	Set<Id> parentIdsSet = new Set<Id>();
    List<Society__c> societyListToUpdate = new List<Society__c>();
    IF(Trigger.IsAfter){
        IF(Trigger.IsInsert || Trigger.IsUndelete || Trigger.IsUpdate){
            FOR(Member__c c : Trigger.new){
                if(c.Society_Name__c!=null){   
                   parentIdsSet.add(c.Society_Name__c); 
                }
            }
        }
        IF(Trigger.IsDelete){
            FOR(Member__c c : Trigger.Old){
                if(c.Society_Name__c!=null){   
                   parentIdsSet.add(c.Society_Name__c); 
                }
            }
        }
    }
  
    List<Society__c> societyList = new List<Society__c>([Select id ,Name, No_of_Members__c, (Select id, Name From Member__r) from Society__c Where id in:parentIdsSet]);
    FOR(Society__c s : societyList){
        List<Member__c> memberList = s.Member__r;
        s.No_of_Members__c = memberList.size();
        societyListToUpdate.add(s);
    }
    try{
        update societyListToUpdate;
    }catch(System.Exception e){
        
    }	
}


Saie, Please let me know if you have any query. Happy to solve your query.

Please Mark best answer so that other people would take reference from it.

Thanks

Malika Pathak 9Malika Pathak 9

Please find the solution of Page2_Controller class.

Test class

@isTest
public class Page2_ControllerTest {
@isTest
    public static void page2(){
         Society__c testSoc=new Society__c();
        testSoc.Name='Some Name';
        testSoc.Reg_Code__c='12345';
        testSoc.Location__c='Some Location';
        testSoc.Formation_Year__c='2012';
        insert testSoc;
        
        Member__c memObj=new Member__c();
        memObj.Name='check';
        memObj.Society_Name__c=testSoc.id;
        insert memObj;
        Member__c memObj2=new Member__c();
        memObj2.Name='check2';
        memObj2.Society_Name__c=testSoc.id;
        insert memObj2;
        test.startTest();
        Page2_Controller page2Obj=new Page2_Controller();
        page2Obj.getSocieties();
        page2Obj.previous();
        page2Obj.next();
        test.stopTest();
    }
}
 

Apex controller

public class Page2_Controller {
    private final integer MAX_RECORDS_PER_PAGE = 5;
    public integer startingFrom {get; set;}
    private integer socRecordCount; 
    //View Society
    public Page2_Controller(){
        if(startingFrom == NUll)
            startingFrom=0;
        socRecordCount = [select count() from Society__c];
    }
    public List<Society__c> getSocieties(){
        List<Society__c> results=[select name,Secretary__c,No_Of_Members__c,location__c 
                                  from Society__c limit :MAX_RECORDS_PER_PAGE
                                  offset : startingFrom];
        return results;  
    }
    
    public PageReference previous()
    {
        if(startingFrom <= 0)
        {   
            startingFrom = 0;
        } else if(startingFrom >= socRecordCount)
            startingFrom = startingFrom - MAX_RECORDS_PER_PAGE;
        else if(startingFrom < socRecordCount)
        {
            startingFrom = startingFrom - MAX_RECORDS_PER_PAGE;
        }
        
        return ApexPages.currentPage();
    }
    
    public PageReference next()
    {
        if((socRecordCount - startingFrom) > MAX_RECORDS_PER_PAGE)
            startingFrom = startingFrom + MAX_RECORDS_PER_PAGE;
        return ApexPages.currentPage();
    }  
}


Please appreciate my effort with giving me Best Answer.

Thanks.

This was selected as the best answer
Saie Shendage 7Saie Shendage 7
<apex:page controller="Page3_Controller">
    <script type="text/javascript">
        function selectAllCheckboxes(obj,receivedInputID){
            var inputCheckBox = document.getElementsByTagName("input");
            for(var i=0; i<inputCheckBox.length; i++){
                if(inputCheckBox[i].id.indexOf(receivedInputID)!=-1){
                    inputCheckBox[i].checked = obj.checked;
                }
            }
        }
    </script>
    <apex:form >
        <apex:pageBlock >
        	<apex:pageBlockSection >
                	Society:<apex:selectList value="{!selectedSociety}" size="1">
                        <apex:selectOptions value="{!records}"/>
                    </apex:selectList>
                    
                    Year:<apex:selectList value="{!selectedYear}" size="1">
                        <apex:selectOptions value="{!years}"/>
                    </apex:selectList>
                    
                    Month:<apex:selectList value="{!selectedMonth}" size="1">
                        <apex:selectOptions value="{!months}"/>
                    </apex:selectList>
                <apex:commandButton action="{!showRecords}" value="View Defaulters" rerender="pbt"/>
                <apex:commandButton action="{!addDues}" value="Add Dues" rerender="pbt1"/>
            </apex:pageBlockSection>
            
            <apex:pageBlockSection columns="2" title="Select defaulters to send email to">
            	<apex:pageBlockTable value="{!wrapDuesList}" var="d" id="pbt" >
                    <apex:column >
                        <apex:facet name="header">
                            <apex:inputCheckbox onclick="selectAllCheckboxes(this,'inputId')"/>
                        </apex:facet>
                        <apex:inputCheckbox value="{!d.checked}" id="inputId"/>
                    </apex:column>
                  <apex:column value="{!d.due.Member_Name__c}" />
                  <apex:column value="{!d.due.Dues_Remaining__c}"/>
            </apex:pageBlockTable>
                
                <apex:pageBlockTable id="id1" value="{!checkedDues}" var="d">
            	<apex:column value="{!d.Member_Name__c}" />
                  <apex:column value="{!d.Dues_Remaining__c}"/>
            </apex:pageBlockTable>
            
            <apex:commandButton value="Show selected" action="{!showRecords}" rerender="id1"/>
            <apex:commandButton value="Send Reminder Email" action="{!sendReminderEmail}"/>
            
          
            </apex:pageBlockSection>
            
            <apex:pageBlockSection title="Add Dues">
            	<apex:pageBlockTable value="{!defaulters1}" var="d" id="pbt1">
            	<apex:column value="{!d.Member_Name__c}"/>
                <apex:column value="{!d.Total_dues__c}"/>
                <apex:column value="{!d.Dues_Remaining__c}"/>
                <apex:column headerValue="Dues Paid">                            
                	<apex:inputText value="{!d.Dues_Paid__c}" />                    
          	  	</apex:column>
            	</apex:pageBlockTable>
                <br/>
            	<apex:commandButton value="Add" action="{!add}"/>
            </apex:pageBlockSection>
            
            
        </apex:pageBlock>
    </apex:form>
</apex:page>
 
public class Page3_Controller {
	public string selectedSociety{get;set;}
    public string selectedYear{get;set;}
    public string selectedMonth{get;set;}
    
    public List<wrapDues> wrapDuesList{get;set;}
    public List<Due__c> checkedDues{get;set;}
    
   	public list<selectOption> records{get;set;}
    public list<selectOption> years{get;set;}
    public list<selectOption> months{get;set;}
    
    public list<String> yearsToAdd=new List<String>{'2015','2016','2017','2018','2019','2020','2021'};
    
    public list<String> monthsToAdd=new List<String>{'Jan','Feb','March','April','May','June','July','Aug','Sept','Oct','Nov','Dec'};
        
   	public list<Due__c> defaulters{get;set;}
    public list<Due__c> defaulters1{get;set;}
    public list<Society__c> societies;
   
   
   	public Page3_Controller(){
   
       defaulters=new List<Due__c>();
       
       records=new List<selectOption>();
       years=new List<selectOption>();
       months=new List<selectOption>();
       
       societies=[select id, name from Society__c];
        for(Society__c a:societies){
            records.add(new selectoption(a.id,a.name));
        }
        
        for(String y:yearsToAdd){
            years.add(new selectoption(y,y));
        }
        
        for(String m:monthsToAdd){
            months.add(new selectoption(m,m));
        }
        

   	}
    
    public class wrapDues{
        public Due__c due{get;set;}
        public Boolean checked{get;set;}
        public wrapDues(Due__c d){
            due=d;
            checked=false;
        }
    }
    
   public pageReference showRecords(){
       defaulters=[select id, member_name__c, dues_remaining__c,Member_Name__r.Name, Member_Name__r.Email__c, Month__c from Due__c where 
                   society_name__c=:selectedSociety and year__c=:selectedYear and month__c=:selectedMonth and dues_remaining__c>0];
       if(wrapDuesList==null){
           wrapDuesList = new List<wrapDues>();
            for(Due__c a: defaulters) {
                wrapDuesList.add(new wrapDues(a));
            }
        }
       checkedDues=new List<Due__c>();
       for(wrapDues wrapDuesObj:wrapDuesList){
           if(wrapDuesObj.checked == true) {
                checkedDues.add(wrapDuesObj.due);
            }
       }
   		return null;
   }
    
    public PageReference sendReminderEmail(){
        List<Messaging.SingleEmailMessage> mails = new List<Messaging.SingleEmailMessage>();
        for(Due__c d:checkedDues){
            if(d.Member_Name__r.Email__c!=null){
                Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
                List<String> sendTo = new List<String>();
      			sendTo.add(d.Member_Name__r.Email__c);
      			mail.setToAddresses(sendTo);
                
                mail.setReplyTo('saie_shendage@persistent.com');
      			mail.setSenderDisplayName('Secretary of the Society');
    			mail.setSubject('Remaining Dues Not Paid');
              String body = 'Dear ' + d.Member_Name__r.Name + ', ';
              body += 'Your remaining dues are '+d.Dues_Remaining__c+' for the month of '+d.Month__c;
              body += ' Please pay the dues Asap, otherwise penalty would be added.';
              mail.setHtmlBody(body);
            
              mails.add(mail);
            }
        }
        Messaging.sendEmail(mails);
        return null;
    }
    
    public PageReference addDues(){
        defaulters1=[select id, member_name__c,Total_Dues__c, dues_remaining__c,Dues_Paid__c from Due__c where 
                   society_name__c=:selectedSociety and year__c=:selectedYear and month__c=:selectedMonth and dues_remaining__c>0];
      	return null;
    }
    
    public void add(){
        update defaulters1;
    }
    
}

Sorry but I am again asking you the same thing. Actually I get errors whenever I try to write test classes. This is the last Page I want test classes for. Please give me test class code for page3 controller.
Here Due object is junction object for Society and Member with master detail relationship. Please help me out.
Saie Shendage 7Saie Shendage 7
Thank you so much Malika! The codes you gave, worked perfectly. On my Page2, I am displaying table of Society object. In that table there is Location field, which on clicking directs the user to maps.google.com and displays respective location on the map. I want the location to be displayed on the same page on mouseover instead of onclick. Please help me out if you know how to do it.  
Saie Shendage 7Saie Shendage 7
Hey Malika can you tell me how to write assertEquals statements in these test classes code?
Please help.