+ Start a Discussion

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(

//insert and update record
insert ps;

update ps;
update po;
Pagereference p = new PageReference('/'+ps.id);


return p;  





simple test method:


static testMethod void myTest() {

        //how to parse throgh the controller


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


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));




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