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
AlexOAlexO 

Test Coverage. 75%+ on Development account but unable to deploy to production

Hi guys,

I am new to Apex development so probably this is a very easy one.

 

I created a very simple Apex Class in my Development account. I also created a Test Class and manage to run it with 90% test coverage (I run it both on the UI and on Eclipse).

 

When Deploying and/or Validating the deployment on Eclipse I am getting a 100% coverage on my test class and 0% coverage on my production one (I use Eclipse Version: 3.4.2 on MAC).

 

I am even using "system.debug" on my Production Class to ensure that my tests methods are calling my production ones and I can see that it is actually running (Test on Development account)

 

It seems like somehow I have to link the Test Class with the production one as when I try to "Validate" or to "Deploy" the Production class is always at 0% coverage even if the test one goes all the way up to 100% (BTW, I am not using hardcodes... I am creating my test data within my test class).

 

Is there anything I am missing here?

 

Thanks in advance 

RajManiaRajMania
It might happen, you are selecting any record, which present in test but not in prod, and so null or no record found kind of error. but in eclipse after fail it will show you line not covered, please check that also
EMHDevEMHDev

Post your code so that we can take a look.  It is very difficult to advise without seeing the code. One question - did you deploy the test class as well?  Otherwise it won't see your tests when running them in the production org.

 

Erica

AlexOAlexO

Thank you Erica.

Yes, I tried to deploy both my classes and my test classes.

I took a subset of my code (smallest class) and tried to deploy this one along with its testing class... and got the very same error. Code follows... and again, thanks for your help.

 

Class:

 

public with sharing class OA_Opportunity {
   
    ApexPages.StandardController o_controller;
    Account o_account;
    Opportunity o_opportunity;
    String s_advertiserId;
   
    public OA_Opportunity (ApexPages.StandardController stdController){
        this.o_controller = stdController;
        o_opportunity = (Opportunity)stdController.getRecord();
    }
   
    public void sets_advertiserId (String s_newAdvertiserId) {
        s_advertiserId = s_newAdvertiserId;
    }
   
    public String gets_advertiserId () {
        return s_advertiserId;
    }

    // Defaults the Advertiser's Agency to its preferred Agency as configured on its Account record
    public PageReference preferredAgency () {
        // Run only if the Agency has been defined (Not NULL -> Not 000000000000000)
        if (s_advertiserId != '000000000000000') {
            try {
                o_account = [select id, name, Account_Type__c, Preferred_Agency__c from Account where id = :s_advertiserId];
                if (o_account.Account_Type__c == 'Advertiser') {
                    o_opportunity.Agency__c = o_account.Preferred_Agency__c;
                    system.debug('####### Account Type = Advertiser');
                }
                else if (o_account.Account_Type__c == 'Agency & Advertiser') o_opportunity.Agency__c = s_advertiserId;
            }
            catch (Exception e) {
                return null;
            }
        }
        return null;
    }

}

 

Test Class:

 

@isTest
private class OA_Opportunity_Test {
   
    static testMethod void myUnitTest() {
       
        try {
           
            // Lets create an Agency
            Account o_AgencyTest = new Account();
            o_AgencyTest.Name = 'TestCoverageAgency';
            o_AgencyTest.Account_Type__c = 'Agency';
            insert o_AgencyTest;
            system.debug('####### Agency ID ' + o_AgencyTest.Id);
           
            // Lets create an Advertiser defaulting its preferred Agency
            Account o_AdvertiserTest = new Account();
            o_AdvertiserTest.Name = 'TestCoverageAdvertiser';
            o_AdvertiserTest.Account_Type__c = 'Advertiser';
            o_AdvertiserTest.preferred_Agency__c = o_AgencyTest.Id;
            insert o_AdvertiserTest;
            system.debug('####### Advertiser ID ' + o_AdvertiserTest.Id);
                       
            // Time to Create the Opportunity
            Opportunity o_oppTest = new Opportunity();
            o_oppTest.Name = 'TestCoverageOpportunity';
            o_oppTest.Advertiser__c = o_AdvertiserTest.Id;
            o_oppTest.Buy_Type__c = 'Guarantee Buy';
            o_oppTest.StageName = 'Prospecting';
            o_oppTest.CloseDate = date.today();
            o_oppTest.Agency__c = null;
            insert o_oppTest;
            system.debug('####### Opportunity ID ' + o_oppTest.Id);
           
            // Lets create our Custom Object
            ApexPages.StandardController o_controller = New ApexPages.StandardController(o_oppTest);
            OA_Opportunity o_OA_Opportunity = new OA_Opportunity(o_controller);

            // OK, first thing comes first, lets define the Advertiser Id used on the class
            o_OA_Opportunity.sets_advertiserId(o_AdvertiserTest.Id);
            String test_Advertiser = o_OA_Opportunity.gets_advertiserId();
            system.debug('####### Class Advertiser ID ' + test_Advertiser);
           
            // First real test: Lets set the Preferred Agency
            o_OA_Opportunity.preferredAgency();
            system.debug('####### PreferredAgeny Tested');
            // Test Opportunity Save
            o_OA_Opportunity.OppSave();
            system.debug('####### OppSave Tested');
           
        }
        catch (Exception e) {
            String error = 'error';
        }
    }
}

 

EMHDevEMHDev

Alex,

 

In your dev organisation, did you get 90%+ coverage on your OA_Opportunity class as well?  Basically, your production class should be identical to your development class.  The methodology is that you develop in your Dev org or your Sandbox (I use the Sandbox so that the custom objects are the same as the production org as far as possible), write the unit tests, do all the testing that you can in your Dev Org/Sandbox.  Once ALL code has the required coverage in the test environment, you then deploy the code (choose class and test class together, if in separate classes)  to production.  I have found that the test coverage goes down a bit when deploying it, presumably because of the different data, but it shouldn't be by much unless you are relying on the org data in your testing.  You will always get 100% on a test class because it itself doesn't require testing, so what matters is the coverage on the code being tested.

 

That aside, in your tests, you should put in some System.AssertEquals (or NotEquals) to check that the data is what you expect it to be.  So you insert your data, call the relevant method that would be called by your VF page, and then assert that the result is what you'd expect.

 

You should also test your error conditions, so do a test where the preferred agency has NOT been set and check that the relevant error is caught using System.AssertEquals on the error.

 

Hope this helps,

Erica

Message Edited by m62Dev on 02-03-2010 11:20 AM
AlexOAlexO

Thanks Erica,

 

I manage to deploy my classess. I ended up deploying the objects one by one starting with the Standard Objects, then Custom Objects... until I got to the Classes. Then I deployed the entire project with no errors :)

 

On another note, I apreciate you suggestions for improving my test class. revised version follows:


 

@isTest
private class OA_Opportunity_Test {
   
    static testMethod void myUnitTest() {
       
        try {
           
            // 1st Test - Valid Preferred Agency
            // Lets create an Agency
            Account o_AgencyTest = new Account();
            o_AgencyTest.Name = 'TestCoverageAgency';
            o_AgencyTest.Account_Type__c = 'Agency';
            insert o_AgencyTest;
            system.debug('####### Agency ID ' + o_AgencyTest.Id);
           
            // Lets create an Advertiser defaulting its preferred Agency
            Account o_AdvertiserTest = new Account();
            o_AdvertiserTest.Name = 'TestCoverageAdvertiser';
            o_AdvertiserTest.Account_Type__c = 'Advertiser';
            o_AdvertiserTest.preferred_Agency__c = o_AgencyTest.Id;
            insert o_AdvertiserTest;
            System.assertEquals(o_AgencyTest.Id, o_AdvertiserTest.preferred_Agency__c);
            system.debug('####### Advertiser ID ' + o_AdvertiserTest.Id);
                       
            // Time to Create the Opportunity
            Opportunity o_oppTest = new Opportunity();
            o_oppTest.Name = 'TestCoverageOpportunity';
            o_oppTest.Buy_Type__c = 'Guarantee Buy';
            o_oppTest.StageName = 'Prospecting';
            o_oppTest.CloseDate = date.today();
            o_oppTest.Agency__c = null;
            insert o_oppTest;
            system.debug('####### Opportunity ID ' + o_oppTest.Id);
           
            // Lets create our Custom Object
            ApexPages.StandardController o_controller = New ApexPages.StandardController(o_oppTest);
            OA_Opportunity o_OA_Opportunity = new OA_Opportunity(o_controller);

            // OK, first thing comes first, lets define the Advertiser Id used on the class
            o_OA_Opportunity.sets_advertiserId(o_AdvertiserTest.Id);
            String test_Advertiser = o_OA_Opportunity.gets_advertiserId();
            system.debug('####### Class Advertiser ID ' + test_Advertiser);
           
            // First real test: Lets set the Preferred Agency
            o_OA_Opportunity.preferredAgency();
            system.debug('####### PreferredAgency Tested');
            // Lets confirm that the Agency was preselected
            System.assertEquals(o_AgencyTest.Id, o_oppTest.Agency__c);
            system.debug('####### PreferredAgeny ID ' + o_oppTest.Agency__c);
           
            // Test Opportunity Save
            o_OA_Opportunity.OppSave();
            system.debug('####### OppSave Tested');


            // 2nd Test - Invalid Preferred Agency
            // Time to Create the Opportunity
            Opportunity o_oppTest2 = new Opportunity();
            o_oppTest2.Name = 'TestCoverageOpportunity';
            o_oppTest2.Buy_Type__c = 'Guarantee Buy';
            o_oppTest2.StageName = 'Prospecting';
            o_oppTest2.CloseDate = date.today();
            o_oppTest2.Agency__c = null;
            insert o_oppTest2;
            system.debug('####### Opportunity2 ID ' + o_oppTest2.Id);
           
            // Lets create our Custom Object
            ApexPages.StandardController o_controller2 = New ApexPages.StandardController(o_oppTest2);
            OA_Opportunity o_OA_Opportunity2 = new OA_Opportunity(o_controller2);
           
            // Lets run a Preferred Agency exception
            o_OA_Opportunity2.preferredAgency();
            system.debug('####### PreferredAgency Exception Tested');
            // Lets confirm that the Agency was preselected
            System.assertEquals(null, o_oppTest2.Agency__c);
           
        }
        catch (Exception e) {
            String error = 'error';
        }
    }
}

 

Thank you again,

Alex