0

I've been reading up on Unit testing and I'm trying to get a test done for a class. I want to delpoy this to our live Org but am pretty stuck at this: The Code itself works but I've never done unit testing before so I'm pretty lost I have a Page, a Class and a Test Class. Can some one help me with this?

Page:

<apex:page standardController="Estimate__c" 
    extensions="EstimateClone" 
    action="{!cloneRecord}">
    <apex:pageMessages />
</apex:page>

Class:

public with sharing class EstimateClone {

    //Variable to hold current record
    Estimate__c currentRecord;

    //Standard constructor method
    public EstimateClone (ApexPages.StandardController controller) 
    {
        currentRecord = (Estimate__c)controller.getRecord();
    }

    public PageReference cloneRecord()
    {
        //Variable to hold the new record
        Estimate__c newRecord;
        Savepoint sp = Database.setSavepoint();
        try
        {
            currentRecord = [Select id, name from Estimate__c where id =:currentRecord.id];
            newRecord = currentRecord.clone(false);
            newRecord.Name = currentRecord.Name + ' Copy'; 
            newRecord.recordtypeid ='012b0000000PkQU';
            insert newRecord;

            //Copy the related list 
            List<Estimate_Product_Line_Item__c> EPLL = new List<Estimate_Product_Line_Item__c>();
            for( Estimate_Product_Line_Item__c  EstPL : [SELECT Name, Root__c, Type__c, Estimate_Product__r.Name, 
                                                         Quantity__c, Price__c, Comments__c FROM  
                                                            Estimate_Product_Line_Item__c  WHERE  
                                                         Estimate__c =:currentRecord.id])
            {
                 Estimate_Product_Line_Item__c newEstPL = EstPL.clone(false);
                 newEstPL.Estimate__c = newRecord.id;
                 EPLL.add(newEstPL);
            }
            insert EPLL;


        }
        catch(Exception e)
        {
            Database.rollback(sp);
            ApexPages.addMessages(e);
            return null;
        }
        return new PageReference('/'+newRecord.id);
    }        
}

Test Class:

@istest
public class TestClass {

    static testMethod void EstimateCloneTest() {
        //EstimateClone Test

        Estimate__c es = [select Name, Comments__C, Opportunity__c, Status__c
                          from Estimate__c 
                          where id = 'a0E17000000RrB7'];

        PageReference pageRef = Page.EstClone;
        pageRef.getParameters().put('Id',es.Id);
        Test.setCurrentPage(pageRef);

        EstimateClone EC = new EstimateClone(new ApexPages.Standardcontroller(es.id));

        EC.CloneRecord();
        System.assert(EC.Name == es.Name+' Copy');                
    }
}
3
  • What is the issue you are facing? is the test class not giving coverage or the assertion fails?
    – Prady
    Nov 18, 2014 at 10:56
  • Its always a best practice to create your own test records in the test class and not depend on records in your org as these test classes would fail when you move the code to different org. If you really want to use existing records in a test class then you need to use isTest(SeeAllData=true) annotation in your test class. read more about seeAlldata here salesforce.com/us/developer/docs/apexcode/Content/…
    – Prady
    Nov 18, 2014 at 11:03
  • The Test Class is not giving any coverage, and I'm getting and error that says: Constructor not defined: [ApexPages.StandardController].&lt;Constructor&gt;(Id) on the unit test class Nov 18, 2014 at 11:13

1 Answer 1

0

As the error clearly points, the way you are using ApexPages.StandardController() is incorrect. You should pass an instance of SObject to it, not an Id. So alter you code to meet this as below.
But one thing to mention, always try creating test data and use them inside the test class. Don't use existing data because you can't confirm when they will be disappeared. Also when you are deploying, you have to change this hard coded id. So just create a new Estimate__c object, insert it and use it for the ApexPages.StandardController.

If you really need to have existing data, better use (SeeAllData=true) in the test class.

@istest(SeeAllData=true)
public class TestClass {

    static testMethod void EstimateCloneTest() {
        //EstimateClone Test

        Estimate__c es = [select Name, Comments__C, Opportunity__c, Status__c
                          from Estimate__c 
                          where id = 'a0E17000000RrB7'];

        PageReference pageRef = Page.EstClone;
        pageRef.getParameters().put('Id',es.Id);
        Test.setCurrentPage(pageRef);

        EstimateClone EC = new EstimateClone(new ApexPages.Standardcontroller(es));

        EC.CloneRecord();
        System.assert(EC.Name == es.Name+' Copy');                
    }
}
2

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .