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
John Neilan 18John Neilan 18 

Test Class Cover Save Method

Hi,

I am struggling on how to cover lines 46 - 48 in my Test Class below.  Am I missing something in my test class? I don't get an error, the lines jsut aren't covered.

Test Class
 
@isTest 
private class Test_NewRecController {

    static testMethod void testMethod1()
    {
        Account testAccount = test_CreateRecords.createAcct(0);
        insert testAccount;
        
        Contact testContact = test_CreateRecords.createCont(testAccount.Id);
        insert testContact;
        
		PageReference pageRef = Page.NewRecType;
            pageRef.getParameters().put('contId','testContact.Id');
            pageRef.getParameters().put('acctId','testAccount.Id');

	Test.StartTest();
        
        NewRecController testNewRec2 = new NewRecController();
	        testNewRec2.addRecType();

        NewRecController testNewRec3 = new NewRecController();
        	Pagereference pageRef1 = Page.NewRecType;
                pageRef1.getParameters().put('rec','0');
        		Test.setCurrentPage(pageRef1);
	        testNewRec3.removeType();

	Test.StopTest();
    }

    static testMethod void testMethod2()
    {
        Account testAccount = test_CreateRecords.createAcct(0);
        insert testAccount;
        
        Contact testContact = test_CreateRecords.createCont(testAccount.Id);
        insert testContact;
        
        Rec_Type__c testRec1 = new Rec_Type__c();
            testRec1.Account__c = testAccount.Id;
            testRec1.Contact__c = testContact.Id;
            testRec1.Rec_Type__c = 'Celebrity';
        insert testRec1;

	Test.StartTest();

        NewRecController testNewRec = new NewRecController();

        PageReference pageRef = Page.NewRecType;

	            pageRef.getParameters().put('contId','testContact.Id');
    	        pageRef.getParameters().put('acctId','testAccount.Id');
    	        pageRef.getParameters().put('Rec_Type__C','Celebrity');

        	Test.setCurrentPage(pageRef);

        testNewRec.saveType();
//        System.assertNotEquals(null,testNewRec.saveType());
        
	Test.StopTest();
    }
}

VF Controller
public class NewRecController {

    Rec_Type__c rec1 = new Rec_Type__c();
    public list<Rec_Type__c> listRecType{ get; set; }

    Id acctId;
    Id contId;
    Public Integer rec {get; set;}
    Public List<Rec_Type__c> ContRecList {get; set;}
    
//    Constructor 

	public NewRecController() { 
        acctId = ApexPages.currentPage().getParameters().get('acctId');
        contId = ApexPages.currentPage().getParameters().get('contId');
        
        vip.Account__c = acctId;
        vip.Contact__c = contId;
        
        ContRecList = [SELECT Rec_Type__c
                      FROM Rec_Type__c
                      WHERE Contact__c =: contId];
system.debug('@@@@@@ - ContactTypes: '+ContVIPList);

        listRecType = new list<Rec_Type__c>();
        listRecType.add(rec1);
	} 

    Public void addRecType() {
        Rec_Type__c rec1a = new Rec_Type__c();
    		rec1a.Contact__c = contId;
        	rec1a.Account__c = acctId;
		listRecType.add(rec1a);
system.debug('@@@### - listVIPType: '+listVIPType);
        }

	public PageReference RemoveType() {
    	rec = Integer.valueOf(ApexPages.currentPage().getParameters().get('rec'));
        listRecType.remove(rec);
        return null;
    }
     
    public PageReference saveType() {
    	try {
        	insert listRecType;
        		PageReference contRecord = new PageReference('/'+contId);
       			contRecord.setRedirect(true);
    	return contRecord;
    	} catch(DmlException e) {
        	ApexPages.addMessages(e);
        return null;
    	}
    }
}

 
AbhishekAbhishek (Salesforce Developers) 
https://salesforce.stackexchange.com/questions/244794/how-do-i-increase-my-code-coverage-or-why-cant-i-cover-these-lines

Try the methods as mentioned in the above developer discussion.


I hope you find the above information is helpful. If it does, please mark as Best Answer to help others too.

Thanks.