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
sharrissharris 

Need help with test class that has %73 coverage

Can someone help me with getting my test class working? What is the test class missing?

 

Here is my Class:

 

public class NICCCloneWithArtifactsController 
{
    
    //added an instance varaible for the standard controller
    private ApexPages.StandardController controller {get; set;}
     // add the instance for the variables being passed by id on the url
    private NI_Change_Control__c r {get;set;}
    // set the id of the record that is created -- ONLY USED BY THE TEST CLASS
    public ID newRecordId {get;set;}
    
    // initialize the controller
    public NICCCloneWithArtifactsController(ApexPages.StandardController controller) 
    { 
        // initialize the stanrdard controller
        this.controller = controller;
        // load the current record
        r = (NI_Change_Control__c)controller.getRecord(); 
    }
    
    // method called from the VF's action attribute to clone the Change Request
    public PageReference cloneWithArtifacts() 
    {
        
        // setup the save point for rollback
        Savepoint sp = Database.setSavepoint();
        NI_Change_Control__c newR;
        
        try 
        {
            
            // copy the change request - ONLY INCLUDE THE FIELDS YOU WANT TO CLONE
            r = [select Id, Priority__c, Request_Summary__c, Request_Details__c, Change_Type__c, Date_Required__c, Deployment_Group__c, Data_Change__c,
                  New_Custom_Script__c, Outage_Required__c, Manager_Reviewer__c, Peer_Reviewer__c, Developer_Compiler__c, Related_Servers__c, Related_Hosted_RFC__c, NI_Change_Owner__c 
                  from NI_Change_Control__c where id = :r.id];
            
            newR = r.clone(false);
            // SANDBOX =====================================================================
            //newR.RecordTypeId = '012P00000000G42';      // Full ID = 012P00000000G42IAE
            // PRODUCTION ==================================================================
            newR.RecordTypeId = '0126000000016TJ';        // Full ID = 0126000000016TJAAY                         
            newR.Change_Status__c = 'New';
            newR.Cloned__c = true;
            newR.Cloned_From__c = r.Id;
            newR.Cloned_w_Artifacts__c = true;
            
            insert newR;
            
            // set the id of the new po created for testing
            newRecordId = newR.id;
            
            // LOOP THROUGH ALL RELATED ARTIFACTS 
            for (NICC_Artifact__c artSource : [SELECT Id, Artifact_Name__c, Type__c, Description__c, Document_Link__c, Complete__c FROM NICC_Artifact__c WHERE NI_Change_Control__c = :r.id])
            {
                
                NICC_Artifact__c artNew = artSource.clone(false);
                artNew.NI_Change_Control__c = newR.id;
                
                insert artNew;
                
                List<Attachment> Attachments = new List<Attachment>();
                for (Attachment attach : [SELECT Body, BodyLength, ContentType, Description, Name FROM Attachment WHERE ParentId = :artSource.Id]) 
                {
                     Attachment newAttach = attach.clone(false);
                     newAttach.ParentId = artNew.id;
                     Attachments.add(newAttach);
                }
                
                insert Attachments;             
            
            }

        } 
        catch (Exception e)
        {
            // roll everything back in case of error
            Database.rollback(sp);
            ApexPages.addMessages(e);
            return null;
        }
        
        return new PageReference('/'+newR.id+'/e?retURL=%2F'+newR.id);
        
    }
    
}

 

Here is my Test Class:

@isTest
private class TestNICCCloneWithArtifactsController 
{
 
    static testMethod void testArtifactCloneController() 
    {
		
        // Creae a Change Request
        NI_Change_Control__c nicc = new NI_Change_Control__c();
        //SANDBOX ====================== nicc.RecordTypeId = '012P00000000G42';
        //nicc.RecordTypeId = '012P00000000G42';
       // PRODUCTION =================== nicc.RecordTypeId = '0126000000016TJ';
        nicc.RecordTypeId = '0126000000016TJ';
        nicc.Change_Steps__c = 'test';
        nicc.Date_Required__c = system.today();
        nicc.Request_Summary__c = 'Test';
        insert nicc;
 
        // create new Artifact record
        NICC_Artifact__c ar = new NICC_Artifact__c();
        ar.NI_Change_Control__c = nicc.Id;
        ar.Type__c = 'Customer Sign-off';
        ar.Document_Link__c = 'TEST';
        ar.Description__c = 'TEST';
        insert ar;

        // create an Attachment for the Artifact
        Attachment a = [Select Body from Attachment Limit 1];
        Attachment attach = new Attachment();
        //attach.BodyLength = 10;
        attach.Name = 'test.txt';
        attach.ParentId = ar.Id;
        attach.Body = a.Body;
        insert attach;                           
         
    }
 
}

 Here's the page code:

<apex:page standardController="NI_Change_Control__c"
     extensions="NICCCloneWithArtifactsController"
     action="{!cloneWithArtifacts}">
     <apex:pageMessages />
</apex:page>

 Any tips would be greatly appreciated!

 

Best Answer chosen by Admin (Salesforce Developers) 
jeremyyjeremyy

Why don't use run the test in Eclipse or in salesforce by going to the Class and clicking "Run Test"? It will show you which lines are covered and which aren't.