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
Daniel GageDaniel Gage 

Code Coverage and the Live system

Okay... So I'm very new to Salesforce. And I'm trying to deploy 1 package that has 86 percent coverage in the Sandbox.

But when I go to deploy it in the live system, it is telling me
" Details: Average test coverage across all Apex Classes and Triggers is 55%, at least 75% test coverage is required."

However... when I go to the Develop section and look at the Manage Your Apex Classes., I do the Estimate Code Coverage... and it is listing as 81 percent.

What on earth is the problem? And how do I go about fixing it? 

I've cleared all the data, and re-run all the tests... but no go... and I'm seriously stumped. I can't find anything online that could help me.

Best Answer chosen by Daniel Gage
Roy LuoRoy Luo
//replace ?? with the your CustomObject
System.runAs(user){
    PageReference p = Page.YourVfPage;
    p.getParameters().put('dest','http://www.google.com'); 
    Test.setCurrentPage(p); 
    
    SSORedirectController src = new SSORedirectController(new ApexPages.StandardController(??));
 
    src.redirectToPage();
}


All Answers

Roy LuoRoy Luo
Every file has to have at least 75% code coverage. The overall average doesn't help. One file with less than 75% coverage will give you a no-go though you might have every other files 100% code coverage. In Develper Cosole at the Tests tag, you would see the code coverages for each file.
Daniel GageDaniel Gage

Thank you. And that makes sense.. to an extent..... but the Deployment package only contains 1 Class, and it's test class coverage is at 86 percent in the Sandbox for this one Apex Class (it's corrosponding Apex Class is at 0 in the Live system-- which is why we want to update this)....How on earth did it make it to the live system with 0 coverage?

Or does it look at all the classes and tests in the sandbox first?  Is there a way to tell why this one is showing 55 percent on the import, but 86 percent in the Sandbox?

Is there something online I can read to better understand this whole process?
 

 

Roy LuoRoy Luo
Do you have @IsTest(SeeAllData=true) in the test? If yes, then when data change, the code coverage might be different. If you could post some codes here, it would be easier to track it down.

BTW @IsTest(SeeAllData=true) is evil, don't use it by all means.
Daniel GageDaniel Gage

Roy, Thank you for your time.

A lot of this code isn't mine, and so I'm trying to track down what I can do to resolve this. So I'm not sure which code to post up here. (All of the code I've written is above the 75 percent code coverage). I've found 6 Classes and/or triggers that are under 75 percent.... They aren't part of the deployment packaged, but are you saying they can affect deployment? - I still can't figure out what the 55 percent is coming from.

If so, I've asked the company which of these are unneccessary (so they can just be removed) and which one need to have better test classes created for them.

 

To clarifiy. In the test classes I have written... I have used @isTest in front of them.. but have not used the (SeeAllData=True) I assume that I'm okay with that method?

---- Here is the Class that is being attempted to deploy from Sandbox to Live.... in the Sandbox it is listing as 86 percent Coverage.

But when trying to deploy it, it says it's at 55 percent??

public class SSORedirectController{
    String url;
    String ContactId;
    public SSORedirectController(){
        url = ApexPages.currentPage().getParameters().get('dest');
        String userId = userInfo.getUserId();
        ContactId = [Select id, ContactId From User Where id =: userId].ContactId;
    }
    
    public PageReference redirectToPage(){
        if(ContactId != null){
            PageReference pg = new PageReference(url);
            pg.setRedirect(true);
            return pg;
        }
        else{
            ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, 'Cannot redirect to '+url+' for this user.'));
            return null;
        }
    }
    
    @isTest
    public static void test1() {
    
    Account a = new Account(Name='Test Account Name');
  insert a;
 
  Contact c = new Contact(LastName = 'Contact Last Name', AccountId = a.id);
  insert c;
 
  User user = new User();
  user.ProfileID = [Select Id From Profile Where Name='LEE Member Portal'].id;
  user.EmailEncodingKey = 'ISO-8859-1';
  user.LanguageLocaleKey = 'en_US';
  user.TimeZoneSidKey = 'America/New_York';
  user.LocaleSidKey = 'en_US';
  user.FirstName = 'first';
  user.LastName = 'last';
  user.Username = 'test@appirio.com';   
  user.CommunityNickname = 'testUser123';
  user.Alias = 't1';
  user.Email = 'no@email.com';
  user.IsActive = true;
  user.ContactId = c.Id;
 
  insert user;
        
        

System.runAs(user){
            ApexPages.currentPage().getParameters().put('dest','http://www.google.com');
            SSORedirectController src = new SSORedirectController();
            src.redirectToPage();
        }
   }
}

Roy LuoRoy Luo
//replace ?? with the your CustomObject
System.runAs(user){
    PageReference p = Page.YourVfPage;
    p.getParameters().put('dest','http://www.google.com'); 
    Test.setCurrentPage(p); 
    
    SSORedirectController src = new SSORedirectController(new ApexPages.StandardController(??));
 
    src.redirectToPage();
}


This was selected as the best answer
Daniel GageDaniel Gage

I am not sure I understand where the YourVFPage comes from or what should go in the ??

Probably a lot easier than my brain is making it out to look.

Roy LuoRoy Luo
Your controller SSORedirectController is used in a VF page and the page should have the first line as:
<apex:page standardController="YourCustomObject" extensions="SSORedirectController" id="page">
or 
<apex:page controller="SSORedirectController" id="page">

YourVFPage is the VF page name, not the label. If your page has something like standardController="Acount", then
Account acct = new Account(...);
insert acct;
SSORedirectController src = new SSORedirectController(newApexPages.StandardController(acct));
 
Daniel GageDaniel Gage
ah. gotcha. this one wasn't my code. So I wasn't/am not Fully understanding it. But this helps. thank you again. you have been very helpful.