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
emuelasemuelas 

Help needed to write a simple test method for Apex class to deploy in production

Hi ,

 

I have a simple  Apex class that retrieves and updates data  which i want to deploy to production.

Iam totally new to test classes/methods.

 

Please help me to write a simple test method for my Apex Class.

 

public with sharing class serpinv {
String pid = ApexPages.currentPage().getParameters().get('id');

Product_inventory__c po;


     public serpinv(ApexPages.StandardController controller)
{  

//retrieve record
  this.po = (product_inventory__c)controller.getRecord();  
  }               
 public pagereference saveChanges()
 {  

//update record
    update this.po;
product_serial__c ps=new product_serial__c(
serial_number__c=po.serial_number__c);

//insert and update record
insert ps;

ps.account__c=po.supidtest__c;
update ps;
po.serial_number__c=null;
po.warehouse__c=null;
po.bin__c=null;
 po.orderpinv__c=null;  
 po.price__c=null;     
update po;
Pagereference p = new PageReference('/'+ps.id);

p.setRedirect(true);

return p;  
}
}

 

 

 

 

simple test method:

 

static testMethod void myTest() {
     
     

        //how to parse throgh the controller
 
 }

Pradeep_NavatarPradeep_Navatar

Tryout the test method given below:

static testMethod void myTest()
{
ApexPages.StandardController controller= ApexPages.StandardController(new product_inventory__c());    //create instance of standard controller
serpinv  srv= new serpinv (controller);        //   Create instance of controller class
srv.saveChanges();                                 // call the method
}

emuelasemuelas

Hi Pradeep,

 

Thank you for your reply!

 

I tried the following test code:

static testMethod void myTest()
{
 product2 po=new product2(name='test');
 product_inventory__c controller=new product_inventory__c(product__c=po.id);
                               

serpinv srv = new serpinv(new ApexPages.StandardController(controller));

srv.saveChanges();

}

 

The call to save changes is throwing an error when i try to run the tests:

DmlException: Update failed. First exception on row 0; first error: MISSING_ARGUMENT, Id not specified in an update call: []

 

for the first  line  in the save changes method:

 update this.po;

 

I tried removing the line -srv.saveChanges();

but this doesnt help for my code coverage percentage.

 

Please help