+ Start a Discussion
satyamsatyam 

Please Help to Right the Test class for Trigger

Hi,

 

I am new to write test class for trigger Please tell me how to write the test class for below trigger

 

trigger Recruitervalidation on Recruiter__C (before Update) {  
    for (Recruiter__c a : Trigger.new) {
        system.debug('a.Status__c '+a.Status__c);
        system.debug('a.jobname__C '+a.jobname);
        
        if ((a.Status__c == 'waiting')&&(a.jobname__c==null)) {
                
            a.addError('Please fill the "Jobname" for the Job');
        } 
       else if ((a.Status__c == 'Pending')&&(a.Jobdesc==null)) {
                
            a.addError(‘Some Error Message');
        }                                                         
    }
}

 

Thanks:))))

Best Answer chosen by Admin (Salesforce Developers) 
SRajesh8SRajesh8

Hi

 

here I am getting 80% vode coverage

 

try this

 

@istest
private class Recruitervalidation_test{
public static testmethod void Recruitervalidation_test()
{
Recruiter__c rc = new Recruiter_c();
rc.status__c='waiting';
rc.jobname__c='';
rc.joblocation__C='INDIA';
rc.jobtype__C='Technical'; //see here it can be __c or as it is
insert rc;

 

rc.status__C='pending';
rc.jobdesc__C='';
rc.joblocation__C='INDIA';

try
{
    update rc;
}
Catch(Exception e)
{
  system.debug('Some Error Message:'+e);
}
}}


Plz makes as solution once it solve ur issue  so that  other can be helpful

 

Regards,

rajesh.

All Answers

SRajesh8SRajesh8

Hi

 

Try this

 

@istest

private class Recruitervalidation_test{

public static testmethod void Recruitervalidation_test()

{

Recruiter__c rc = new Recruiter_c();

rc.status='waiting';

rc.jobname='';

rc.

//like this insert the required fields in it.

rc.

insert rc;

rc.status='pending';

rc.jobdesc='';

//insert some mandatory fields

update rc;

}}

 

Plz make it solve once it solves ur issue.

Regards,

Rajesh.

 

satyamsatyam

Its showing invalid field Sobject when i am inserting the field before updating.

 

Showing problem in below line

 

Should i need to enter a new record  what i have to do

 

insert rc;

rc.status='pending';

rc.jobdesc='';

//insert some mandatory fields

update rc;

 

Thanks

SRajesh8SRajesh8

Hi

 

see that jobdesc is standard field or custom field if it is custom field means __c and if not coming send ur  total test class.

 

Regards,

Rajesh.

satyamsatyam

Hi,

 

Below is my trigger and Test Class

 

Red Colur line i am not covering in test class

Trigger:
trigger Recruitervalidation on Recruiter__C (before Update) {  
    for (Recruiter__c a : Trigger.new) {
        system.debug('a.Status__c '+a.Status__c);
        system.debug('a.jobname__C '+a.jobname);
        
        if ((a.Status__c == 'waiting')&&(a.jobname__c==null)) {
                
            a.addError('Please fill the "Jobname" for the Job');
        }
       else if ((a.Status__c == 'Pending')&&(a.Jobdesc==null)) {
                
            a.addError(‘Some Error Message');
        }                                                         
    }
}

Test Class:

@istest

private class Recruitervalidation_test{

public static testmethod void Recruitervalidation_test()

{

Recruiter__c rc = new Recruiter_c();

rc.status__c='waiting';

rc.jobname__c='';

rc.joblocation='INDIA';

rc.jobtype='Technical';

insert rc;

rc.status='pending';

rc.jobdesc='';

rc.joblocation='INDIA';


update rc;

}}

satyamsatyam

Hi,

 

Below is my trigger and Test Class

 

Red Colur line i am not covering in test class

Trigger:
trigger Recruitervalidation on Recruiter__C (before Update) {  
    for (Recruiter__c a : Trigger.new) {
        system.debug('a.Status__c '+a.Status__c);
        system.debug('a.jobname__C '+a.jobname);
        
        if ((a.Status__c == 'waiting')&&(a.jobname__c==null)) {
                
            a.addError('Please fill the "Jobname" for the Job');
        }
       else if ((a.Status__c == 'Pending')&&(a.Jobdesc==null)) {
                
            a.addError(‘Some Error Message');
        }                                                         
    }
}

Test Class:

@istest

private class Recruitervalidation_test{

public static testmethod void Recruitervalidation_test()

{

Recruiter__c rc = new Recruiter_c();

rc.status__c='waiting';

rc.jobname__c='';

rc.joblocation__C='INDIA';

rc.jobtype__C='Technical';

insert rc;

rc.status__C='pending';

rc.jobdesc__C='';

rc.joblocation__C='INDIA';


update rc;

}}

 

Thanks

SRajesh8SRajesh8

Hi

 

Try This

 

@istest

private class Recruitervalidation_test{

public static testmethod void Recruitervalidation_test()

{

Recruiter__c rc = new Recruiter_c();

rc.status__c='waiting';

rc.jobname__c='';

rc.joblocation__C='INDIA';

rc.jobtype__C='Technical';

insert rc;

system.debug('a.Status__c '+a.Status__c); //see here what valu geting
system.debug('a.jobname__C '+a.jobname);

system.debug('a.Jobdesc '+a.Jobdesc);



rc.status__C='pending';

rc.jobdesc__C='';

rc.joblocation__C='INDIA';
update rc;

system.debug('a.Status__c '+a.Status__c); //see here what valu geting
system.debug('a.jobname__C '+a.jobname);

system.debug('a.Jobdesc '+a.Jobdesc);

rc.status__C='pending';

rc.jobdesc__C='';

rc.joblocation__C='INDIA';
update rc;

system.debug('a.Status__c '+a.Status__c); //see here what valu geting
system.debug('a.jobname__C '+a.jobname);

system.debug('a.Jobdesc '+a.Jobdesc);


}}

 

Regards,

Rajesh.

satyamsatyam

Hi ,

 

Its not going inside if condition.

 

What change i need to do so that its should go inside if condition And cover that part.

 

 

Thanks

SRajesh8SRajesh8

Hi

 

here I am getting 80% vode coverage

 

try this

 

@istest
private class Recruitervalidation_test{
public static testmethod void Recruitervalidation_test()
{
Recruiter__c rc = new Recruiter_c();
rc.status__c='waiting';
rc.jobname__c='';
rc.joblocation__C='INDIA';
rc.jobtype__C='Technical'; //see here it can be __c or as it is
insert rc;

 

rc.status__C='pending';
rc.jobdesc__C='';
rc.joblocation__C='INDIA';

try
{
    update rc;
}
Catch(Exception e)
{
  system.debug('Some Error Message:'+e);
}
}}


Plz makes as solution once it solve ur issue  so that  other can be helpful

 

Regards,

rajesh.

This was selected as the best answer