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
Becky Miller 15Becky Miller 15 

Test Code (Visualforce/Apex)

Trouble getting any coverage on my test code to cover anything. 

It is a simple Visualforce page of fields already on the Pagelayout but to make it easier for the users.  
The button is on the Account Record and just pulls up the fields the sales team needs to fill out. 

Here is my Apex:
public with sharing class US_Medical_sales_mandatory {
    
    private ApexPages.StandardController c;

  
     public string getAccountId{get;set;}
       
      public US_Medical_sales_mandatory (ApexPages.StandardController controller)
      {
       
          getAccountid = ApexPages.CurrentPage().getparameters().get('ID');
      }
    

    
 public pagereference US_Medical_Sales_Mandatory()
 {

// you can fetch dummy lookup field value here and process accordingly.
  System.debug('@@@@@@@@@@@'+account.ownerid);
  return null;
 } 
}

Here is my test script:
@isTest
public class US_Medical_sales_mandatory_Test {
  
    Static testMethod void US_Medical_sales_mandatory_Test() {
     
         
        Account a = new Account(
            Name = 'testaccount'           );
        insert a;

      Account newAccount = new Account();
        
     Account acc= [Select ID,Name From Account WHERE Name='testaccount'];
      a.Id=acc.Id;
        a.AERs_in_Dept1__c = 'Manual';
        a.AER_s_in_Dept_2__c = 'Cantel ADV + 2.0';
        a.AER_s_in_Dept_3__c = 'Cantel ADV PT';
         a.AER_s_in_Dept_4__c = 'EVOTECH';
         a.AER_s_in_Dept_5__c = 'Cantel ISA';
         a.AER_s_in_Dept_6__c = 'Cantel ISA';
         a.Average_Numbers_of_Rooms__c = 45;
         a.Brush_Brand__c = 'Pull Thru';
         a.Colon_Volume_Yearly__c = 25;
         a.Department_AER_1__c = 'ENT';
         a.Department_AER_2__c =  'GI';
         a.Department_AER_3__c = 'GI';
         a.Department_AER_4__c = 'GI';
         a.Department_AER_5__c = 'GI';
         a.Department_AER_6__c = 'SPD';
          a.Disposable_Irrigation_Tubing__c =
         a.Disposable_Valves__c = 'Defendo';
         a.EGD_Volume_Yearly__c = 26;
         a.ERCP_Volume_Yearly__c = 26;
         a.EUS_Volume_Yearly__c= 27;
         a.QTY_of_AER_in_Dept_1__c= 78 ;
         a.QTY_of_AER_in_Dept_2__c= 1;
         a.QTY_of_AER_in_Dept_3__c= 1000;
         a.QTY_of_AER_in_Dept_4__c=14;
         a.QTY_of_AER_in_Dept_5__c=8;
 
        
        Test.startTest();
       Update a;
        Test.stopTest();
       
        PageReference ref = Page.US_MEDICAL_SALES_Mandatory;
        test.setCurrentPage(ref); 
            }
}
Best Answer chosen by Becky Miller 15
David Zhu 🔥David Zhu 🔥
You may put the following part in the code and try

 Static testMethod void US_Medical_sales_mandatory_Test() {
...........................

 a.QTY_of_AER_in_Dept_3__c= 1000;
  a.QTY_of_AER_in_Dept_4__c=14;
  a.QTY_of_AER_in_Dept_5__c=8;

Update a;
Test.startTest();

PageReference ref = Page.US_MEDICAL_SALES_Mandatory;
pageRef.getParameters().put('id', String.valueOf(a.Id));
 test.setCurrenref Page(ref); 

 ApexPages.StandardController sc = new ApexPages.StandardController(a);
 US_Medical_sales_mandatory FRGExt = new US_Medical_sales_mandatory (sc);         
 Test.stopTest();
}

All Answers

David Zhu 🔥David Zhu 🔥
You may put the following part in the code and try

 Static testMethod void US_Medical_sales_mandatory_Test() {
...........................

 a.QTY_of_AER_in_Dept_3__c= 1000;
  a.QTY_of_AER_in_Dept_4__c=14;
  a.QTY_of_AER_in_Dept_5__c=8;

Update a;
Test.startTest();

PageReference ref = Page.US_MEDICAL_SALES_Mandatory;
pageRef.getParameters().put('id', String.valueOf(a.Id));
 test.setCurrenref Page(ref); 

 ApexPages.StandardController sc = new ApexPages.StandardController(a);
 US_Medical_sales_mandatory FRGExt = new US_Medical_sales_mandatory (sc);         
 Test.stopTest();
}
This was selected as the best answer
Becky Miller 15Becky Miller 15
I am getting the following errors:

Method does not exist or incorrect signature: void Page(System.PageReference) from the type US_Medical_sales_mandatory_Test
Variable does not exist: setCurrentref
Variable does not exist: pageRef
Missing ';' at 'Page'
David Zhu 🔥David Zhu 🔥
try this:

Static testMethod void US_Medical_sales_mandatory_Test() {
...........................

 a.QTY_of_AER_in_Dept_3__c= 1000;
  a.QTY_of_AER_in_Dept_4__c=14;
  a.QTY_of_AER_in_Dept_5__c=8;

Update a;
Test.startTest();

PageReference ref = Page.US_MEDICAL_SALES_Mandatory;
ref .getParameters().put('id', String.valueOf(a.Id));
 test.setCurrentPage(ref); 

 ApexPages.StandardController sc = new ApexPages.StandardController(a);
 US_Medical_sales_mandatory FRGExt = new US_Medical_sales_mandatory (sc);         
 Test.stopTest();
}