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
wadams2010wadams2010 

Apex Test Class of Insert based off of Selection

I am trying to write an apex test for a class where it inserts records based off of a selection being made in a visualforce page. Right now I am getting 60% covered but I would like to get this to 100% if possible. I feel like I am not correctly testing the actual insert of the records (Associated Location) from my class. Also, I believe this is a wrapper class. What can I do to get the amount close to 100%? Thanks in advance!

Apex Class:
public with sharing class quoteAssociatedSiteExtension {

	//Our collection of the class/wrapper objects cContact 
	public Quote theQuote {get; set;}
    public List<assLoc> assLocList {get; set;}
    
    public quoteAssociatedSiteExtension(ApexPages.StandardController controller) { 

        String quoteId = ApexPages.currentPage().getParameters().get('quoteId');
		theQuote = [select Id, AccountId from Quote where Id =:quoteId limit 1];
    }

	//This method uses a simple SOQL query to return a List of Contacts
	public List<assLoc> getAssLoc() {
		if(assLocList == null) {
			assLocList = new List<assLoc>();
			for(Site__c s: [select Id, Name, Account__r.Id,	 City__c,Location_Id__c, Location_ID_Link__c, Identifier__c, Country__c,  Site_ID__c, Site_Main_Phone__c, State__c, Street__c, Zip__c
                            from Site__c 
                            where Account__r.Id=:theQuote.AccountId
                            AND Id NOT IN(Select Site__c from Associated_Location__c Where Quote__c=:theQuote.Id)
                            ]) {
				// As each contact is processed we create a new cContact object and add it to the contactList
				assLocList.add(new assLoc(s));
			}
		}
		return assLocList;
	}
    
	public PageReference addInfo() {
		List<Associated_Location__c> nmal = new List<Associated_Location__c>();
        
		for(assLoc aLoc: getAssLoc()) {
			if(aLoc.selected == true) {   
                Associated_Location__c nal = new Associated_Location__c();


                nal.Site__c = aLoc.st.Id;
                nal.Quote__c = theQuote.Id;
                nal.Remarks_Special_Instructions__c = aLoc.remark;
                System.debug(nal.Remarks_Special_Instructions__c);
                
                nmal.add(nal);
			}
		}
        
        if(nmal.size()>0)
            insert nmal; 
        
        return new PageReference('/apex/quoteEditAssociatedSite?quoteId='+ApexPages.currentPage().getParameters().get('quoteId'));
	}
    
    public PageReference editLater() {
		List<Associated_Location__c> nmal = new List<Associated_Location__c>();
        
		for(assLoc aLoc: getAssLoc()) {
			if(aLoc.selected == true) {   
                Associated_Location__c nal = new Associated_Location__c();


                nal.Site__c = aLoc.st.Id;
                nal.Quote__c = theQuote.Id;
                nal.Remarks_Special_Instructions__c = aLoc.remark;
                System.debug(nal.Remarks_Special_Instructions__c);
                
                nmal.add(nal);
			}
		}
        
        if(nmal.size()>0)
            insert nmal; 
        
        return new PageReference('/'+ApexPages.currentPage().getParameters().get('quoteId'));
	}
    
        public PageReference quickSave() {
		List<Associated_Location__c> nmal = new List<Associated_Location__c>();
        
		for(assLoc aLoc: getAssLoc()) {
			if(aLoc.selected == true) {   
                Associated_Location__c nal = new Associated_Location__c();


                nal.Site__c = aLoc.st.Id;
                nal.Quote__c = theQuote.Id;
                nal.Remarks_Special_Instructions__c = aLoc.remark;
                System.debug(nal.Remarks_Special_Instructions__c);
                
                nmal.add(nal);
			}
		}
        
        if(nmal.size()>0)
            insert nmal; 
        
        PageReference pageRef = new pageReference('/apex/AssociatedLocationPage?quoteId='+ApexPages.currentPage().getParameters().get('quoteId'));
        pageRef.setRedirect(true);
        return pageRef;
	}


	// This is our wrapper/container class. A container class is a class, a data structure, or an abstract data type whose instances are collections of other objects. In this example a wrapper class contains both the standard salesforce object Contact and a Boolean value
	public class assLoc {
		public Associated_Location__c ac {get; set;}
        public Site__c st {get; set;}
        public Quote qt {get; set;}
		public Boolean selected {get; set;}
        public String remark {get; set;}

		//This is the contructor method. When we create a new cContact object we pass a Contact that is set to the con property. We also set the selected value to false
		public assLoc(Associated_Location__c a) {
            ac = a;
		}        
        
        public assLoc(Site__c s) {
			st = s;
			selected = false;
		}
        
		public assLoc(Quote q) {
			qt = q;
			selected = false;
		}
         
	}
}

Test Class:
@isTest(SeeAllData=true)
private class AssociatedSiteTestClass {
static testMethod void myUnitTest() {
 
        Profile pf = [Select Id from Profile where Name = 'System Administrator'];
 
        User u = new User();
        u.FirstName = 'Test';
        u.LastName = 'User';
        u.Email = 'testuser@test123456789010.com';
        u.CompanyName = 'test.com';
        u.Title = 'Test User';
        u.Username = 'testuser@test123456789010.com';
        u.Alias = 'testuser';
        u.CommunityNickname = 'Test User';
        u.TimeZoneSidKey = 'America/Mexico_City';
        u.LocaleSidKey = 'en_US';
        u.EmailEncodingKey = 'ISO-8859-1';
        u.ProfileId = pf.Id;
        u.LanguageLocaleKey = 'en_US';
        insert u;
 
        system.runAs(u){
 
            Account a = new Account();
            a.Name = 'Test Account';
            a.Type = 'Prospect';
            insert a;
            
            Contact c = new Contact();
            c.FirstName = 'Test';
            c.LastName = 'Contact';
            c.AccountId = a.Id;
            insert c;
            
            Opportunity o = new Opportunity();
            o.Name = 'Parent Test Opportunity';
            o.AccountId = a.Id;
            o.StageName = 'General Interest';
            o.CloseDate = system.today();
            o.Type = 'Initial';
            o.Units__c = 5;
            o.Total_MRR__c = 500;
            o.Total_NRR__c = 500;
            insert o;
            
            OpportunityContactRole ocr = new OpportunityContactRole ();
            ocr.ContactId = c.Id;
            ocr.OpportunityId = o.Id;
            ocr.IsPrimary = True;
            insert ocr;
                        
            Quote q = new Quote();
            q.Name = 'Test Quote';
            q.OpportunityId = o.Id;
			q.ContactId = c.Id;
            q.New_or_Existing_Site__c = 'New Site';
            q.Request_Type__c = 'Standard';
            insert q;
            
            Site__c s1 = new Site__c();
            s1.Account__c = a.Id;
            s1.Site_ID__c = 101;
			s1.Location_ID__c = 1001;
			insert s1;

			Site__c s2 = new Site__c();
            s2.Account__c = a.Id;
            s2.Site_ID__c = 102;
			s2.Location_ID__c = 1002;
			insert s2;
            
            
            
            PageReference myVfPage = Page.AssociatedLocationPage;
    			System.Test.setCurrentPageReference(myVfPage); // use setCurrentPageReference, 
   
                ApexPages.currentPage().getParameters().put('quoteId',q.Id);
                String id = ApexPages.currentPage().getParameters().get('quoteId');
                Test.setCurrentPageReference(myVFPage);
            	system.assertEquals(true,id==q.Id);
            
            	quoteAssociatedSiteExtension qASE = new quoteAssociatedSiteExtension(new ApexPages.StandardController(q));
            		qASE.addInfo();
            		qASE.editLater();
            		qASE.quickSave();
            
            Associated_Location__c nal = new Associated_Location__c();
                nal.Quote__c = q.Id;
                nal.Site__c = s1.Id;
            	qASE.quickSave();
            Associated_location__c alq = [select Id from Associated_Location__c where Site__c =:s1.Id limit 1];
            system.assertEquals(s1.Id,alq.Id);
                
            }
	}
}

 
Best Answer chosen by wadams2010
Amit Chaudhary 8Amit Chaudhary 8
Please try below code.
@isTest
private class AssociatedSiteTestClass 
{
	static testMethod void myUnitTest() 
	{
 
        Profile pf = [Select Id from Profile where Name = 'System Administrator'];
 
        User u = new User();
        u.FirstName = 'Test';
        u.LastName = 'User';
        u.Email = 'testuser@test123456789010.com';
        u.CompanyName = 'test.com';
        u.Title = 'Test User';
        u.Username = 'testuser@test123456789010.com';
        u.Alias = 'testuser';
        u.CommunityNickname = 'Test User';
        u.TimeZoneSidKey = 'America/Mexico_City';
        u.LocaleSidKey = 'en_US';
        u.EmailEncodingKey = 'ISO-8859-1';
        u.ProfileId = pf.Id;
        u.LanguageLocaleKey = 'en_US';
        insert u;
 
        system.runAs(u)
		{
            Account a = new Account();
            a.Name = 'Test Account';
            a.Type = 'Prospect';
            insert a;
            
            Contact c = new Contact();
            c.FirstName = 'Test';
            c.LastName = 'Contact';
            c.AccountId = a.Id;
            insert c;
            
            Opportunity o = new Opportunity();
            o.Name = 'Parent Test Opportunity';
            o.AccountId = a.Id;
            o.StageName = 'General Interest';
            o.CloseDate = system.today();
            o.Type = 'Initial';
            o.Units__c = 5;
            o.Total_MRR__c = 500;
            o.Total_NRR__c = 500;
            insert o;
            
            OpportunityContactRole ocr = new OpportunityContactRole ();
            ocr.ContactId = c.Id;
            ocr.OpportunityId = o.Id;
            ocr.IsPrimary = True;
            insert ocr;
                        
            Quote q = new Quote();
            q.Name = 'Test Quote';
            q.OpportunityId = o.Id;
			q.ContactId = c.Id;
            q.New_or_Existing_Site__c = 'New Site';
            q.Request_Type__c = 'Standard';
            insert q;
            
            Site__c s1 = new Site__c();
            s1.Account__c = a.Id;
            s1.Site_ID__c = 101;
			s1.Location_ID__c = 1001;
			insert s1;

			Site__c s2 = new Site__c();
            s2.Account__c = a.Id;
            s2.Site_ID__c = 102;
			s2.Location_ID__c = 1002;
			insert s2;
            
            
            
            PageReference myVfPage = Page.AssociatedLocationPage;
			System.Test.setCurrentPageReference(myVfPage); 
			ApexPages.currentPage().getParameters().put('quoteId',q.Id);
			String id = ApexPages.currentPage().getParameters().get('quoteId');
			Test.setCurrentPageReference(myVFPage);
			system.assertEquals(true,id==q.Id);

            
			quoteAssociatedSiteExtension qASE = new quoteAssociatedSiteExtension(new ApexPages.StandardController(q));
			List<quoteAssociatedSiteExtension.assLoc> lstAccLoc = qASE.getAssLoc();
			
			for(quoteAssociatedSiteExtension.assLoc acloc : qASE.assLocList )
			{
				acloc.selected= true;
			}
			
			qASE.addInfo();
			qASE.editLater();
			qASE.quickSave();
            
            Associated_Location__c nal = new Associated_Location__c();
                nal.Quote__c = q.Id;
                nal.Site__c = s1.Id;
            	qASE.quickSave();
            Associated_location__c alq = [select Id from Associated_Location__c where Site__c =:s1.Id limit 1];
            system.assertEquals(s1.Id,alq.Id);
                
            }
	}
}

Please let us know if this will help you

Thanks
Amit Chaudhary