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
dev_sfdc1dev_sfdc1 

Test Class for apex class

Hi..

I'm having error like this.. so I'm not getting any code coverage..
Class.AssetParts.<init>: line 10, column 1
Class.AssetPartsTestClass.AssetPartsTestClass: line 26, column 1

Apex Class:

public with sharing class AssetParts {
  public Asset_Part__c astpart;
  public Physical_Asset__c asset{get;set;}
  public string assetname;
    public AssetParts(ApexPages.StandardController con)
    {
        astpart=(Asset_Part__c)con.getRecord();
        String astname = ApexPages.currentPage().getParameters().get('retURL');
        string[] strsplit = astname.split('/');
        assetname = strsplit[1];---------------> here class error
         asset= new Physical_Asset__c();
       
           
    }
    public String errmsg{get;set;}
    public PageReference Save()
    {
            astpart.Asset_Name__c =assetname;
            insert astpart;
                           
            PageReference ref= new PageReference('/'+assetname);
            ref.setredirect(true);
            return ref;
        }
    }  
   My test class is:

@isTest
public class AssetPartsTestClass
{
     static TestMethod void AssetPartsTestClass()
    {
      PageReference ref = Page.AssetParts;
      Test.setcurrentPage(ref);
     
     
      Account testAccount = new Account(Name='Test Company Name');
      insert testAccount;
    
      Physical_Asset__c phy = new Physical_Asset__c(Name='Physical Asset Name',Vendor_Account__c=testAccount.id);
      insert phy;
     
      Asset_Part__c testassetpart = new Asset_Part__c(Name='Test Assetpart Name',Vendor_Account__c=testAccount.id,Asset_Name__c =phy.id);
      insert testassetpart;
     
        
     
      ApexPages.currentPage().getParameters().put('assetname',phy.id);
      ApexPages.StandardController sc = new ApexPages.StandardController(testassetpart);     
    
      AssetParts  ap = new AssetParts(sc);---------->test class error
      ap.save();
          
    }
}


Please help me from this..
Best Answer chosen by dev_sfdc1
Kamatchi Devi SargunanathanKamatchi Devi Sargunanathan
Hi,

Try changing the following highlighted line,

@isTest
public class AssetPartsTestClass
{
    static TestMethod void AssetPartsTestClass()
    {
      PageReference ref = Page.AssetParts;
      Test.setcurrentPage(ref);
   
   
      Account testAccount = new Account(Name='Test Company Name');
      insert testAccount;
  
      Physical_Asset__c phy = new Physical_Asset__c(Name='Physical Asset Name',Vendor_Account__c=testAccount.id);
      insert phy;
   
      Asset_Part__c testassetpart = new Asset_Part__c(Name='Test Assetpart Name',Vendor_Account__c=testAccount.id,Asset_Name__c =phy.id);
      insert testassetpart;
   
      ApexPages.currentPage().getParameters().put('retURL','apex/'+testassetpart.Asset_Name__c);
      ApexPages.StandardController sc = new ApexPages.StandardController(testassetpart);   
  
      AssetParts  ap = new AssetParts(sc);
      ap.save();
        
    }
}

Hope this helps you...!

Regards,
Kamatchi Devi R
Salesforce Certified Developer | Salesforce Certified Administrator

All Answers

Kamatchi Devi SargunanathanKamatchi Devi Sargunanathan

Hi,

Can you mention what error is returned in running this test class?

Regards,
Kamatchi Devi R
Salesforce Certified Developer | Salesforce Certified Administrator
dev_sfdc1dev_sfdc1
Hi Kamatchi,
Having Error like this

system.TypeException:Invalid Conversion from runtime type
SOBJECT:Physical_Asset__c to SOBJECT:Asset_Part__c

Please help me I stopped here to move further..
Thank you
Kamatchi Devi SargunanathanKamatchi Devi Sargunanathan
Hi,

Try changing the following highlighted line,

@isTest
public class AssetPartsTestClass
{
    static TestMethod void AssetPartsTestClass()
    {
      PageReference ref = Page.AssetParts;
      Test.setcurrentPage(ref);
   
   
      Account testAccount = new Account(Name='Test Company Name');
      insert testAccount;
  
      Physical_Asset__c phy = new Physical_Asset__c(Name='Physical Asset Name',Vendor_Account__c=testAccount.id);
      insert phy;
   
      Asset_Part__c testassetpart = new Asset_Part__c(Name='Test Assetpart Name',Vendor_Account__c=testAccount.id,Asset_Name__c =phy.id);
      insert testassetpart;
   
      ApexPages.currentPage().getParameters().put('retURL','apex/'+testassetpart.Asset_Name__c);
      ApexPages.StandardController sc = new ApexPages.StandardController(testassetpart);   
  
      AssetParts  ap = new AssetParts(sc);
      ap.save();
        
    }
}

Hope this helps you...!

Regards,
Kamatchi Devi R
Salesforce Certified Developer | Salesforce Certified Administrator
This was selected as the best answer
Kamatchi Devi SargunanathanKamatchi Devi Sargunanathan
There might be any updates going on in developer console by salesforce that's why you are not accessing the page. try after some time...!
dev_sfdc1dev_sfdc1
Thanks for your kind reply..
Now I'm getting 76% coverage.
But that insert only not get covered(I mean that apex class save functionality insert).What i need to include in test class..