+ Start a Discussion
Mamadou Diallo 14Mamadou Diallo 14 

0% code coverage

Hello,
I have a controller and its test class. The code coverage is currently at 0%. I tried different scenarios but I had the same issue. I'm sharing the 2 Apex classes. There are 2 formula fields Number_Of__Employees_Summary__c and Winning_Team__c. The custom field Date_Time_Closed__c is updated by workflow when an opportunity is won or renewed.
@isTest
public class MyControllerTest {
    static testMethod void MyController(){
        Integer numAccts;
        Integer numOppsPerAcct;
        List<Account> accts = new List<Account>();
        
        for(Integer i=0;i<numAccts;i++) {
            Account a = new Account(Name='TestAccount' + i,
                                   RecordTypeId = '012800000003UY8', 
                                   Type = 'Prospect',  
                                   Number_Of_Employees__c = 190 + i
                                   );
            accts.add(a);
        }
        insert accts;
        
        List<Opportunity> opps = new List<Opportunity>();
        for (Integer j=0;j<numAccts;j++) {
            Account acct = accts[j];
            // For each account just inserted, add opportunities
            for (Integer k=0;k<numOppsPerAcct;k++) {
                opps.add(new Opportunity(Name=acct.Name + ' Opportunity ' + k,
                                       CloseDate=System.today().addMonths(1),
                                       StageName = 'Closed Won',
                                       Number_Of_Employees_From_Opp = 200 + k,
                                       AccountId=acct.Id));
            }
        }
        // Insert all opportunities for all accounts.
        insert opps;
        //Populate the formula fields
       List<Opportunity> testOppAfterInsert = new List<Opportunity>();
         testOppAfterInsert = [select Name, Owner.Name, Number_Of__Employees_Summary__c, Number_Of_Employees_From_Opp, Account.Number_Of__Employees__c, Winning_Team__c, StageName, Date_Time_Closed__c from Opportunity WHERE Id IN :opps];
        }  
         MyController xyz = new MyController();
          }
}

Here is the Apex controller
 
public with sharing class MyController{ 
public List<Opportunity> Records {get; set;} 
public MyController(){ 
Records = [select Name, Owner.Name, Date_Time_Closed__c, Winning_Team__c, Number_Of_Employees_Summary__c, StageName from Opportunity 
 WHERE Number_Of_Employees_Summary > 200 AND Date_Time_Closed__c != null ORDER BY Date_Time_Closed__c DESC Limit 1 ]  ; 
} 
}

Thank you for your help.
Amit Chaudhary 8Amit Chaudhary 8
Update your test class like below
@isTest
public class MyControllerTest 
{
    static testMethod void MyController()
	{
		Account a = new Account(Name='TestAccount',
							   Type = 'Prospect',  
							   Number_Of_Employees__c = 201
							   );

		insert a;
			
			
		Opportunity opp = new Opportunity(Name=a.Name + ' Opportunity ' + 1,
							   CloseDate=System.today().addMonths(1),
							   StageName = 'Closed Won',
							   Number_Of_Employees_From_Opp__c = 200 + 1,
							   AccountId=a.Id));
							   
        insert opp;
		
        MyController xyz = new MyController();

	}
}

Let us know if this will help you
Mamadou Diallo 14Mamadou Diallo 14
Thanks Amit for your help but this did not work. I think the problem is the formula fields who are not queried. It's why in my code I queried them from line 33 to 35.

Again, thank you for taking your time to help me.
Mamadou Diallo 14Mamadou Diallo 14
0% code coverage.
Mamadou Diallo 14Mamadou Diallo 14
Yes
Amit Chaudhary 8Amit Chaudhary 8
Please post your Apex class and Test class which you are using
Mamadou Diallo 14Mamadou Diallo 14
I copied exactly your code. Here is the test class
 
@isTest
public class MyControllerTest 
{
    static testMethod void MyController()
	{
		Account a = new Account(Name='TestAccount',
							   Type = 'Prospect',  
							   Number_Of_Employees__c = 201
							   );

		insert a;
			
			
		Opportunity opp = new Opportunity(Name=a.Name + ' Opportunity ' + 1,
							   CloseDate=System.today().addMonths(1),
							   StageName = 'Closed Won',
							   Number_Of_Employees_From_Opp__c = 202,
							   AccountId=a.Id));
							   
        insert opp;
		
        MyController xyz = new MyController();

	}
}

 
Mamadou Diallo 14Mamadou Diallo 14
Here is the Apex class
public with sharing class MyController{ 
public List<Opportunity> Records {get; set;} 
public MyController(){ 
Records = [select Name, Owner.Name, Date_Time_Closed__c, Winning_Team__c, Number_Of_Employees_Summary__c, StageName from Opportunity 
 WHERE Number_Of_Employees_Summary > 200 AND Date_Time_Closed__c != null ORDER BY Date_Time_Closed__c DESC Limit 1 ]  ; 
} 
}


 
Mamadou Diallo 14Mamadou Diallo 14
In Sandox, the test will show 100% coverage but when I move it to Production with the Apex Class, it will fail with 0% coverage.
Amit Chaudhary 8Amit Chaudhary 8
After executing your test class you are getting any error ?
Mamadou Diallo 14Mamadou Diallo 14
no error in Sandbox but in Production, it failed with 0% coverage. I checked the validation rules and there's no new one.
Amit Chaudhary 8Amit Chaudhary 8
Can you please post your error and which error you are getting. Please post your validation also
Mamadou Diallo 14Mamadou Diallo 14
Thank you Amit. I cannot post real metadata (fields names and validation rules) from my company. Here is what I will do:
  1. I will create a new Sandbox. This will ensure that Production and the new sandbox match
  2. I will use your code in the sandbox to see if I'll have the same error
Can you send me an email at salesforcemd@gmail.com? I can share the real codes with you.

Again I appreciate you taking your weekend to help me.