+ Start a Discussion
salesforce instancesalesforce instance 

Test Class not Covering Throwing an Error

public class status{
public Candidates__c applicant;
public Blob resume {get; set;}
public String contentType {get; set;}
public String fileName {get; set;}

public status(ApexPages.StandardController stdController) {
this.applicant=(Candidates__c)stdController.getRecord();
}
public PageReference saveApplication() {

  try{
        insert(applicant);                                       
   }catch(System.DMLException e){
      ApexPages.addMessages(e);
       return null;
}

if(resume!=null){
      Attachment attach=new Attachment();
      attach.Body=resume;
      attach.Name=filename;
      attach.ContentType=contentType;
      attach.ParentID=applicant.id;
    try {
          insert(attach);
      } catch(System.DMLException e) {
          ApexPages.addMessages(e);
          return null;
      }
  }
  //PageReference p = new ApexPages.StandardController(applicant).view();
        PageReference p =Page.Resume_Parsing;
        p.setRedirect(true);
        return p;                    
}
}




Test Class:


@isTest
public class Teststatus {

public  static testMethod void teststatus(){    
              
             
            Candidates__c opp=new Candidates__c(First_Name__c='test12',Email__c='testfdc@gmail.com',Last_Name__c='fff',Phone__c='9999999999');
            insert opp;
           
            Attachment myAttach1 = new Attachment();
            myAttach1.ParentId =opp.id;
            myAttach1.name = 'Resume_Parsing.pdf';
        
            myAttach1.body = blob.valueof('test');
          
            insert myAttach1;
          
            status atc = new status(new ApexPages.StandardController(opp));
            system.debug('%%%%%'+atc);
            PageReference pageRef = Page.Resume_Parsing;
            pageRef.getParameters().put('id', String.valueOf(opp.Id));
            Test.setCurrentPage(pageRef);
            Blob b;

           ApexPages.currentPage().getParameters().put('id', opp.id);
          status atc1 = new status(new ApexPages.StandardController(myAttach1));
    

       
 
          
           atc.saveApplication();
           
           return;
       }

}


Error Message System.TypeException: Invalid conversion from runtime type SOBJECT:Attachment to SOBJECT:Candidates__c
Stack Trace Class.status.<init>: line 8, column 1
Class.Teststatus.teststatus: line 26, column 1

 
ManjunathManjunath
Hi,

In your controller you ar casting it to "Candidates__c". In your test class you are passing the "status" Class with "Attachment" object.
The problem is here in your test class --> : status atc1 = new status(new ApexPages.StandardController(myAttach1));. (Line - 26)
Change the above line in your test class with the "Candidates__C" object record. This will solve that problem.

Regrds,
MCS
salesforce instancesalesforce instance
@Manjunath 

No its throwing an error SObject constructor must use name=value pairs

 
ManjunathManjunath

Explain what change you made and the error you are getting now.

Regards,
MCS