-4

I worte the below helper class and it is working perfectly, but I have an issue with the deployment because of the test class

public class CESExtension {

  public CES__c CES;

  public opportunity Opp;
  public CES_Line_Item__c lineitem;

  public CESExtension (ApexPages.StandardController stdController) {

    CES__c CES = (CES__c )stdController.getRecord();

    List<opportunity> opp = [SELECT Id, type

                              FROM opportunity
                              WHERE Id = :CES.Opportunity_Name__c];

        if(opp.size() > 0){

            CES.Opportunity_Name__r= opp[0];

        }
    }   
}

Test class

@isTest

Private class TestCESExtension {
    static testmethod void CESExtension (){

        Test.startTest();

        Account a2 = new Account(Name ='icrm testing acc');

        insert a2;

        opportunity opp = new opportunity(Name='testing DIE 4/6/2015' , AccountId= a2.Id,StageName = 'Prospecting',
                               type= 'parts', Description= 'describe',   CloseDate = System.today().addDays(30));

        insert opp;

        CES__c CES = new CES__c (
            Estimated_Order_Date__c = System.today().addDays(30),
            Opportunity_Name__c = opp.id );

       insert CES;

       Test.stopTest();
    }

    static testmethod void updateCES(){
        Account a2 = new Account(Name ='icrm testing acc');

        insert a2;

        opportunity opp = new opportunity(Name='testing DIE 4/6/2015' , AccountId= a2.Id,StageName = 'Prospecting',

                               type= 'parts', CloseDate = System.today().addDays(30));

        insert opp;

        CES__c CES = new CES__c(
            id = 'a158E000000mEyO',
            CurrencyIsoCode = 'USD',
            Delivery_status__c = 'Delivered',
            Estimated_Order_Date__c = System.today().addDays(30),
            Reseller_site_address__c = 'address' );

       update CES;
    }
}
  • 2
    And what is the issue? – Tushar Sharma Nov 4 '16 at 16:48
  • 1
    Most likely this line id = 'a158E000000mEyO', is causing your issue. and the fact that your test methods are not testing anything......they are just attempting to cause code to execute without regard for what should or should not happen – Eric Nov 4 '16 at 16:51
1

Its because you never called your actual class in your test class. Until your class is used as trigger handler you need to explicitly call it.

CES__c CES = new CES__c (
            Estimated_Order_Date__c = System.today().addDays(30),
            Opportunity_Name__c = opp.id );
 insert CES;
ApexPages.StandardController stdController = new ApexPages.StandardController(CES);
CESExtension  ext = new CESExtension (stdController );

Also use assert to validate the code

system.assert(ext.CES != null);

This will give you coverage.

| improve this answer | |
0

Required reading: Best Practice: Avoid Hardcoding IDs

Also note from Isolation of Test Data from Organization Data in Unit Tests:

Starting with Apex code saved using Salesforce API version 24.0 and later, test methods don’t have access by default to pre-existing data in the organization, such as standard objects, custom objects, and custom settings data, and can only access data that they create. However, objects that are used to manage your organization or metadata objects can still be accessed in your tests such as:

  • User
  • Profile
  • Organization
  • AsyncApexJob
  • CronTrigger
  • RecordType
  • ApexClass
  • ApexTrigger
  • ApexComponent
  • ApexPage

Any data you want to exist in your test, you should set up yourself. This will always include record instantiation and an insert statement.

CES__c record = new CES__c(/*required fields*/); // instantiate
insert record; // insert

If you want to update a record, you should insert it first.

Related Reading:

| improve this answer | |

Not the answer you're looking for? Browse other questions tagged or ask your own question.