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
Amit Kr SinghAmit Kr Singh 

How to write test class for this class

My controller:-

public class Auditcontroller
{
       public Auditcontroller(ApexPages.StandardController controller) {
       inventAudit=new Inventory_Audit__c();
       productcount=new List<Product_Count__c>();
       poid=ApexPages.currentPage().getParameters().get('id');

       productcount=new List<Product_Count__c>();
       
       if(poid !=null)
       {
       
       inventAudit=[select name,Account__c,Status__c,Audit_Date__c,product__c from Inventory_Audit__c where id=:poid];
       }
       
       ps=inventAudit.Account__c;
       p=new Product2();
    }
 public Product2 p{get;set;}
 public list<Account> acc{get;set;}
 public list<Asset> invent{get;set;}
 public Inventory_Audit__c inventAudit{get;set;}
 public List<Product_Count__c> productcount;
 public List<InventWrapper> lstInventWrapper {get;set;}
 public Product_Count__c pc{get;set;}
 Public string sp{get;set;}
 public Integer call{get;set;}
 public Id stockdcid {get;set;}
  public String currentStockInventory{get;set;}
 Public Id Poid{get;set;}
public Asset ass{get;set;}
public string ps;

 public void  productDetails() //on click 'Go' button based on seach condition displaying inventory details on page
  {
   if( inventAudit.Account__c!=null && inventAudit.Status__c=='open')
   {
     invent=[select id,Audit__c,name,Product2Id,AccountId,SerialNumber,Status,Reason1__c  from Asset where (Audit__c='Not present' And AccountId=:inventAudit.Account__c) OR (Audit__c='' And AccountId=:inventAudit.Account__c)];
     
      currentStockInventory=apexpages.currentpage().getparameters().get('sfdc.tabName');
     inventAudit.Status__c='Open';
    // insert inventAudit;
     lstInventWrapper =new List<InventWrapper> ();
      for(Asset indetail:invent)
       {
           InventWrapper w = new InventWrapper();
           w.ind=indetail;
           //w.PhysicalCount = null;
           w.Comments='';
           w.Reason='-None-';
           lstInventWrapper.add(w);
           
           System.debug('The list value@@@@@@@@@@@@@@@: ' + lstInventWrapper);
      }
      
    }  
   else
    {
     ApexPages.addMessage(new ApexPages.message(ApexPages.severity.INFO,'Select the search condition'));
    }
    
    }
        
Public void Saveasset()
      {
    string sid=inventAudit.id;
     System.debug('The value is@@@@@@@@@@@@@@@: ' + lstInventWrapper.size());
   
       for(Integer i=0;i<lstInventWrapper.size();i++)
       {
          
           
               pc=new Product_Count__c (Name=lstInventWrapper[i].ind.name ,Asset_Barcode__c=lstInventWrapper[i].ind.SerialNumber,Inventory_Audit__c=inventAudit.Id,Product__c=lstInventWrapper[i].ind.Product2Id,Reasons__c=lstInventWrapper[i].ind.Reason1__c,Asset__c=lstInventWrapper[i].ind.id);
                productcount.add(pc);
            
     }
     upsert productcount;
    
  ApexPages.AddMessage(new ApexPages.Message(ApexPages.Severity.CONFIRM,'Record entered Successfully.Thank you!'));  
   
  
}


 public class InventWrapper
   {
    public Asset ind {get;set;}
   // public Integer PhysicalCount{get;set;}
    public String Comments{get;set;}
    public String Reason{get;set;}
   }
}


My Testclass:-

@isTest
Private class Auditcontroller_Test
{
 
    
 static testMethod void Testme()
  {
  
   PageReference pageRef = Page.Missmatch;
    Test.setCurrentPage(pageRef);
    
   
    list<Account> acc1=new list<Account>();
    list<Asset> invent1=new list<Asset>();
     
     string sp;
     sp='34563';
     Integer call;
     Id stockdcid;
     String currentStockInventory;
      Id Poid;
      string b;
      b='12345';
   
             
   RecordType RecType = [Select Id, Name, DeveloperName From RecordType  Where SobjectType =: 'Account' and name=:'Dealer'];
            
        Account acc= new Account();
            acc.Name = 'Test Account';
            acc.RecordTypeid = RecType.id;
            acc.Email_Address_POC__c='aksingh028@gmail.com';
            acc.Phone = '7676716272';
            acc.Vat__c=10;
            acc.No_of_Asset__c=20; 
            insert acc;
            
        Contact cc = new Contact();
            cc.LastName = 'Spiderman';
            cc.Email = 'spiderman@gmail.com';
            cc.AccountId= acc.Id;
            insert cc;
            
          Product2 prod = new Product2(Name = 'Laptop X200', 
            Family = 'Standrad');
        insert prod;
            
         Asset asst=new Asset();
            asst.Name='Test Assest';
            asst.AccountId=acc.id;
            asst.ContactId=cc.id;
            asst.Product2Id=prod.id;
            asst.Audit__c='Not present';
            //asst.Purchase_Order__c=po.id;
            Insert asst;
     
       
           
     Inventory_Audit__c invaudit= new Inventory_Audit__c();
           //invaudit.Account__c=acc.id;
           //invaudit.Status__c='Approved';
           invaudit.Status__c='Open';
           invaudit.Audit_Date__c=system.today();
           invaudit.Product__c=prod.id;
          
           Insert invaudit;
           
     ApexPages.CurrentPage().getparameters().put('id',invaudit.id);
      string cid1=ApexPages.currentPage().getParameters().get('id');

         invaudit.Status__c='open';
         List<Inventory_Audit__c> lstcon = new List<Inventory_Audit__c>();  
         
         lstcon.add(invaudit);
         
              
           
     Product_Count__c pc= new Product_Count__c();
           pc.Name='testpc';
           pc.Asset__c=asst.id;
           pc.Inventory_Audit__c=invaudit.id;
           
           insert pc;
           
           
     
     
  Test.startTest();

  Auditcontroller AuditCon= new Auditcontroller(new ApexPages.StandardController(invaudit));
  Auditcontroller.InventWrapper invwrapper=new Auditcontroller.InventWrapper();
     
     invwrapper.Reason= 'None';
     invwrapper.Comments= 'hi';
     invwrapper.ind=asst;
     
     
     
  
  AuditCon.productDetails();
  
  //Asset a =[select id,Audit__c,name,Product2Id,AccountId,SerialNumber,Status,Reason1__c  from Asset where (Audit__c='Not present' And AccountId=:inventAudit.Account__c) OR (Audit__c='' And AccountId=:inventAudit.Account__c)];
  //AuditCon.Saveasset();
  
  Test.stopTest();
  
  }
  }


Please help me where i am doing mistake only i am getting 39% code coverage 
please help me
Best Answer chosen by Amit Kr Singh
Amit Chaudhary 8Amit Chaudhary 8
NOTE:- As per fielter lookup your Type(In Use) field value should be "Branch".  I am not sure the API name of that field please update API in below test class is required.

PLease try below code.
@isTest
Private class Auditcontroller_Test
{
	static testMethod void Testme()
	{
   
		list<Account> acc1=new list<Account>();
		list<Asset> invent1=new list<Asset>();
     
			string sp;
			sp='34563';
			Integer call;
			Id stockdcid;
			String currentStockInventory;
			Id Poid;
			string b;
			b='12345';
   
             
		RecordType RecType = [Select Id, Name, DeveloperName From RecordType  Where SobjectType =: 'Account' and name=:'Dealer'];
			
		Account acc= new Account();
            acc.Name = 'Test Account';
			acc.Type= 'Branch';
            acc.RecordTypeid = RecType.id;
            acc.Email_Address_POC__c='aksingh028@gmail.com';
            acc.Phone = '7676716272';
            acc.Vat__c=10;
            acc.No_of_Asset__c=20; 
            insert acc;
            
        Contact cc = new Contact();
            cc.LastName = 'Spiderman';
            cc.Email = 'spiderman@gmail.com';
            cc.AccountId= acc.Id;
        insert cc;
            
        Product2 prod = new Product2(Name = 'Laptop X200', 
            Family = 'Standrad');
        insert prod;
            
        Asset asst=new Asset();
            asst.Name='Test Assest';
            asst.AccountId=acc.id;
            asst.ContactId=cc.id;
            asst.Product2Id=prod.id;
            asst.Audit__c='Not present';
        Insert asst;
           
		Inventory_Audit__c invaudit= new Inventory_Audit__c();
           invaudit.Account__c=acc.id;
           invaudit.Status__c='Open';
           invaudit.Audit_Date__c=system.today();
           invaudit.Product__c=prod.id;
        Insert invaudit;
           
		ApexPages.CurrentPage().getparameters().put('id',invaudit.id);
		ApexPages.CurrentPage().getparameters().put('sfdc.tabName','Test');

         invaudit.Status__c='open';
         List<Inventory_Audit__c> lstcon = new List<Inventory_Audit__c>();  
         
         lstcon.add(invaudit);
         
              
           
     Product_Count__c pc= new Product_Count__c();
           pc.Name='testpc';
           pc.Asset__c=asst.id;
           pc.Inventory_Audit__c=invaudit.id;
           
           insert pc;
           
           
     
     
	Test.startTest();

		PageReference pageRef = Page.Missmatch;
		Test.setCurrentPage(pageRef);

		Auditcontroller AuditCon= new Auditcontroller(new ApexPages.StandardController(invaudit));
		  Auditcontroller.InventWrapper invwrapper=new Auditcontroller.InventWrapper();
		 
		 invwrapper.Reason= 'None';
		 invwrapper.Comments= 'hi';
		 invwrapper.ind=asst;
		 
		 try
		 {
		  AuditCon.productDetails();
		  AuditCon.Saveasset();
		 }
		 Catch(Exception ee)
		 {}
	Test.stopTest();
  
  }
}


Let us know if this will help you

All Answers

Amit Chaudhary 8Amit Chaudhary 8
TRy below code.
@isTest
Private class Auditcontroller_Test
{
 
    
 static testMethod void Testme()
  {
  
	PageReference pageRef = Page.Missmatch;
	Test.setCurrentPage(pageRef);
    
   
    list<Account> acc1=new list<Account>();
    list<Asset> invent1=new list<Asset>();
     
		string sp;
		sp='34563';
		Integer call;
		Id stockdcid;
		String currentStockInventory;
		Id Poid;
		string b;
		b='12345';
   
             
   RecordType RecType = [Select Id, Name, DeveloperName From RecordType  Where SobjectType =: 'Account' and name=:'Dealer'];
            
        Account acc= new Account();
            acc.Name = 'Test Account';
            acc.RecordTypeid = RecType.id;
            acc.Email_Address_POC__c='aksingh028@gmail.com';
            acc.Phone = '7676716272';
            acc.Vat__c=10;
            acc.No_of_Asset__c=20; 
            insert acc;
            
        Contact cc = new Contact();
            cc.LastName = 'Spiderman';
            cc.Email = 'spiderman@gmail.com';
            cc.AccountId= acc.Id;
            insert cc;
            
          Product2 prod = new Product2(Name = 'Laptop X200', 
            Family = 'Standrad');
        insert prod;
            
         Asset asst=new Asset();
            asst.Name='Test Assest';
            asst.AccountId=acc.id;
            asst.ContactId=cc.id;
            asst.Product2Id=prod.id;
            asst.Audit__c='Not present';
            //asst.Purchase_Order__c=po.id;
            Insert asst;
     
       
           
		Inventory_Audit__c invaudit= new Inventory_Audit__c();
           invaudit.Account__c=acc.id;
           invaudit.Status__c='Open';
           invaudit.Audit_Date__c=system.today();
           invaudit.Product__c=prod.id;
           Insert invaudit;
           
     ApexPages.CurrentPage().getparameters().put('id',invaudit.id);
      string cid1=ApexPages.currentPage().getParameters().get('id');

         invaudit.Status__c='open';
         List<Inventory_Audit__c> lstcon = new List<Inventory_Audit__c>();  
         
         lstcon.add(invaudit);
         
              
           
     Product_Count__c pc= new Product_Count__c();
           pc.Name='testpc';
           pc.Asset__c=asst.id;
           pc.Inventory_Audit__c=invaudit.id;
           
           insert pc;
           
           
     
     
  Test.startTest();

	  Auditcontroller AuditCon= new Auditcontroller(new ApexPages.StandardController(invaudit));
	  Auditcontroller.InventWrapper invwrapper=new Auditcontroller.InventWrapper();
     
     invwrapper.Reason= 'None';
     invwrapper.Comments= 'hi';
     invwrapper.ind=asst;
     
	 try
	 {
	  AuditCon.productDetails();
	  AuditCon.Saveasset();
	 }
	 Catch(Exception ee)
	 {}
  Test.stopTest();
  
  }
  }

 
Amit Kr SinghAmit Kr Singh
Thanks Amit Chaudhary 8 
Now i am gettting 45% only code coverage.. plz help me
Amit Chaudhary 8Amit Chaudhary 8
PLease try below test class.
@isTest
Private class Auditcontroller_Test
{
	static testMethod void Testme()
	{
   
		list<Account> acc1=new list<Account>();
		list<Asset> invent1=new list<Asset>();
     
			string sp;
			sp='34563';
			Integer call;
			Id stockdcid;
			String currentStockInventory;
			Id Poid;
			string b;
			b='12345';
   
             
		RecordType RecType = [Select Id, Name, DeveloperName From RecordType  Where SobjectType =: 'Account' and name=:'Dealer'];
			
		Account acc= new Account();
            acc.Name = 'Test Account';
            acc.RecordTypeid = RecType.id;
            acc.Email_Address_POC__c='aksingh028@gmail.com';
            acc.Phone = '7676716272';
            acc.Vat__c=10;
            acc.No_of_Asset__c=20; 
            insert acc;
            
        Contact cc = new Contact();
            cc.LastName = 'Spiderman';
            cc.Email = 'spiderman@gmail.com';
            cc.AccountId= acc.Id;
        insert cc;
            
        Product2 prod = new Product2(Name = 'Laptop X200', 
            Family = 'Standrad');
        insert prod;
            
        Asset asst=new Asset();
            asst.Name='Test Assest';
            asst.AccountId=acc.id;
            asst.ContactId=cc.id;
            asst.Product2Id=prod.id;
            asst.Audit__c='Not present';
        Insert asst;
           
		Inventory_Audit__c invaudit= new Inventory_Audit__c();
           invaudit.Account__c=acc.id;
           invaudit.Status__c='Open';
           invaudit.Audit_Date__c=system.today();
           invaudit.Product__c=prod.id;
        Insert invaudit;
           
		ApexPages.CurrentPage().getparameters().put('id',invaudit.id);
		ApexPages.CurrentPage().getparameters().put('sfdc.tabName','Test');

         invaudit.Status__c='open';
         List<Inventory_Audit__c> lstcon = new List<Inventory_Audit__c>();  
         
         lstcon.add(invaudit);
         
              
           
     Product_Count__c pc= new Product_Count__c();
           pc.Name='testpc';
           pc.Asset__c=asst.id;
           pc.Inventory_Audit__c=invaudit.id;
           
           insert pc;
           
           
     
     
	Test.startTest();

		PageReference pageRef = Page.Missmatch;
		Test.setCurrentPage(pageRef);

		Auditcontroller AuditCon= new Auditcontroller(new ApexPages.StandardController(invaudit));
		  Auditcontroller.InventWrapper invwrapper=new Auditcontroller.InventWrapper();
		 
		 invwrapper.Reason= 'None';
		 invwrapper.Comments= 'hi';
		 invwrapper.ind=asst;
		 
		 try
		 {
		  AuditCon.productDetails();
		  AuditCon.Saveasset();
		 }
		 Catch(Exception ee)
		 {}
	Test.stopTest();
  
  }
}
Let us know if this will help you
 
Amit Kr SinghAmit Kr Singh
I am getting Error

Time Started2/5/2016 12:06 PM
ClassAuditcontroller_Test
Method NameTestme
Pass/FailFail
Error MessageSystem.DmlException: Insert failed. First exception on row 0; first error: FIELD_FILTER_VALIDATION_EXCEPTION, Value does not exist or does not match filter criteria.: [Account__c]
Stack TraceClass.Auditcontroller_Test.Testme: line 76, column 1
Amit Kr SinghAmit Kr Singh
After solving this error  I am geeting 43% Code coverage..
 
Amit Chaudhary 8Amit Chaudhary 8
Please check your Loopup field Account__c. It look like your field id filter lookup. Please setup account data accordingly.
Post your test class which you are using
Amit Kr SinghAmit Kr Singh
@isTest
Private class Auditcontroller_Test
{
    static testMethod void Testme()
    {
   
        list<Account> acc1=new list<Account>();
        list<Asset> invent1=new list<Asset>();
     
            string sp;
            sp='34563';
            Integer call;
            Id stockdcid;
            String currentStockInventory;
            Id Poid;
            string b;
            b='12345';
   
             
        RecordType RecType = [Select Id, Name, DeveloperName From RecordType  Where SobjectType =: 'Account' and name=:'Dealer'];
            
        Account acc= new Account();
            acc.Name = 'Test Account';
            acc.RecordTypeid = RecType.id;
            acc.Email_Address_POC__c='aksingh028@gmail.com';
            acc.Phone = '7676716272';
            acc.Vat__c=10;
            acc.No_of_Asset__c=20; 
            insert acc;
            
        Contact cc = new Contact();
            cc.LastName = 'Spiderman';
            cc.Email = 'spiderman@gmail.com';
            cc.AccountId= acc.Id;
        insert cc;
            
        Product2 prod = new Product2(Name = 'Laptop X200', 
            Family = 'Standrad');
        insert prod;
            
        Asset asst=new Asset();
            asst.Name='Test Assest';
            asst.AccountId=acc.id;
            asst.ContactId=cc.id;
            asst.Product2Id=prod.id;
            asst.Audit__c='Not present';
        Insert asst;
           
        Inventory_Audit__c invaudit= new Inventory_Audit__c();
           //invaudit.Account__c=acc.id;
           invaudit.Status__c='Open';
           invaudit.Audit_Date__c=system.today();
           invaudit.Product__c=prod.id;
        Insert invaudit;
           
        ApexPages.CurrentPage().getparameters().put('id',invaudit.id);
        ApexPages.CurrentPage().getparameters().put('sfdc.tabName','Test');

         invaudit.Status__c='open';
         List<Inventory_Audit__c> lstcon = new List<Inventory_Audit__c>();  
         
         lstcon.add(invaudit);
         
              
           
     Product_Count__c pc= new Product_Count__c();
           pc.Name='testpc';
           pc.Asset__c=asst.id;
           pc.Inventory_Audit__c=invaudit.id;
           
           insert pc;
           
           
     
     
    Test.startTest();

        PageReference pageRef = Page.Missmatch;
        Test.setCurrentPage(pageRef);

        Auditcontroller AuditCon= new Auditcontroller(new ApexPages.StandardController(invaudit));
          Auditcontroller.InventWrapper invwrapper=new Auditcontroller.InventWrapper();
         
         invwrapper.Reason= 'None';
         invwrapper.Comments= 'hi';
         invwrapper.ind=asst;
         
         try
         {
          AuditCon.productDetails();
          AuditCon.Saveasset();
         }
         Catch(Exception ee)
         {}
    Test.stopTest();
  
  }
}


==================================================================
Invoice   
Inventory_Audit__c invaudit= new Inventory_Audit__c();
           //invaudit.Account__c=acc.id;

i have comment it then i am getting 43% plz tell me where i am doing mistake
Amit Chaudhary 8Amit Chaudhary 8
Without Account__c value on Inventory_Audit__c object your code coverage will not increase . you need to set Account lookup on Inventory_Audit__c object.

It look like your lookup ia filter lookup. Please post the screen shot of account lookup.
Amit Kr SinghAmit Kr Singh
Screen shot for inventory
Plz Check now
Amit Chaudhary 8Amit Chaudhary 8
Please post screen shot of Account field from objects metadata not page layout.
Object->Inventory_Audit__c --> Account__c

Please post account lookup screen shot
 
Amit Kr SinghAmit Kr Singh
User-added image
Amit Kr SinghAmit Kr Singh
Hi Amit Chaudhary 8 plz tell me where i am doing mistake and what all are modification i have to do in code

Please help me
Amit Kr SinghAmit Kr Singh
User-added image
plz check now..
Amit Chaudhary 8Amit Chaudhary 8
Hi Amit,

Please follow below step :-

Object->Inventory_Audit__c --> Account__c

User-added image

Then click on account lookup field then post screen shot


 
Amit Kr SinghAmit Kr Singh
User-added image

Screen shot for Account lookup ...Plz help me
Amit Chaudhary 8Amit Chaudhary 8
NOTE:- As per fielter lookup your Type(In Use) field value should be "Branch".  I am not sure the API name of that field please update API in below test class is required.

PLease try below code.
@isTest
Private class Auditcontroller_Test
{
	static testMethod void Testme()
	{
   
		list<Account> acc1=new list<Account>();
		list<Asset> invent1=new list<Asset>();
     
			string sp;
			sp='34563';
			Integer call;
			Id stockdcid;
			String currentStockInventory;
			Id Poid;
			string b;
			b='12345';
   
             
		RecordType RecType = [Select Id, Name, DeveloperName From RecordType  Where SobjectType =: 'Account' and name=:'Dealer'];
			
		Account acc= new Account();
            acc.Name = 'Test Account';
			acc.Type= 'Branch';
            acc.RecordTypeid = RecType.id;
            acc.Email_Address_POC__c='aksingh028@gmail.com';
            acc.Phone = '7676716272';
            acc.Vat__c=10;
            acc.No_of_Asset__c=20; 
            insert acc;
            
        Contact cc = new Contact();
            cc.LastName = 'Spiderman';
            cc.Email = 'spiderman@gmail.com';
            cc.AccountId= acc.Id;
        insert cc;
            
        Product2 prod = new Product2(Name = 'Laptop X200', 
            Family = 'Standrad');
        insert prod;
            
        Asset asst=new Asset();
            asst.Name='Test Assest';
            asst.AccountId=acc.id;
            asst.ContactId=cc.id;
            asst.Product2Id=prod.id;
            asst.Audit__c='Not present';
        Insert asst;
           
		Inventory_Audit__c invaudit= new Inventory_Audit__c();
           invaudit.Account__c=acc.id;
           invaudit.Status__c='Open';
           invaudit.Audit_Date__c=system.today();
           invaudit.Product__c=prod.id;
        Insert invaudit;
           
		ApexPages.CurrentPage().getparameters().put('id',invaudit.id);
		ApexPages.CurrentPage().getparameters().put('sfdc.tabName','Test');

         invaudit.Status__c='open';
         List<Inventory_Audit__c> lstcon = new List<Inventory_Audit__c>();  
         
         lstcon.add(invaudit);
         
              
           
     Product_Count__c pc= new Product_Count__c();
           pc.Name='testpc';
           pc.Asset__c=asst.id;
           pc.Inventory_Audit__c=invaudit.id;
           
           insert pc;
           
           
     
     
	Test.startTest();

		PageReference pageRef = Page.Missmatch;
		Test.setCurrentPage(pageRef);

		Auditcontroller AuditCon= new Auditcontroller(new ApexPages.StandardController(invaudit));
		  Auditcontroller.InventWrapper invwrapper=new Auditcontroller.InventWrapper();
		 
		 invwrapper.Reason= 'None';
		 invwrapper.Comments= 'hi';
		 invwrapper.ind=asst;
		 
		 try
		 {
		  AuditCon.productDetails();
		  AuditCon.Saveasset();
		 }
		 Catch(Exception ee)
		 {}
	Test.stopTest();
  
  }
}


Let us know if this will help you
This was selected as the best answer