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
NikiG22NikiG22 

Test Class

Can someone help me with adding a test class on my controller. thank you!

 

public class dataTableCon{
public Candidate__c cc{set;get;}
public list<Candidate__c> cvd{get;set;}

public dataTableCon(ApexPages.StandardController controller) {
cvd = new list<Candidate__c>();

cvd = [select Full_Name__c, JobSeeker_Type__c,Years_of_exp__c from Candidate__c ];

    }
    
   

 

Best Answer chosen by Admin (Salesforce Developers) 
NikiG22NikiG22

Thank you again for your help!! I really need to work on my test classes.

 

here is the final code:

public class dataTableCon{
public Candidate__c cc{set;get;}
public list<Candidate__c> cvd{get;set;}

public dataTableCon(ApexPages.StandardController controller) {
cvd = new list<Candidate__c>();

cvd = [select Full_Name__c, JobSeeker_Type__c,Years_of_exp__c from Candidate__c ];

    }
    
    
   static testmethod void testOne() {

           Candidate__c candidate = new Candidate__c( 
           First_Name__c = 'Test Candidate',
           Last_Name__c = 'Test LastName',
           JobSeeker_Type__c = 'Test',
           Years_of_exp__c = '2' );

            insert candidate;

            ApexPages.StandardController stc = new ApexPages.StandardController( candidate );

           dataTableCon dtc = new dataTableCon( stc );  

}
  }

 Cheers,

Niki

All Answers

ClintLeeClintLee

It looks like you just need to create a Candidate__c record and then instantiate an object of your controller class.

 

static testmethod void testOne() {

           Candidate__c candidate = new Candidate__c( Name = 'Test Candidate'

                                                                                                ,Field2__c = 'Value2'

                                                                                                , ... );

            insert candidate;

 

            dataTableCon dtc = new dataTableCon();

            System.assertEquals( 1, dtc.cvd.size() );

}

 

Hope that helps,

 

~ Clint

Srikant JoshiSrikant Joshi
//Hi, I guess you are asking a Test class for a controller extension.

static testmethod void Testmethod1() { Candidate__c candidateRec = new Candidate__c( Name = 'test'); insert candidateRec; ApexPages.StandardController stdController = new ApexPages.StandardController( candidateRec); dataTableCon dataTableCOntroller = new dataTableCon(); }

Regards,

joshisrik@gmail.com

Senior Technical Analyst | Schneider Electric.

NikiG22NikiG22

Ok -I get the following error?

 

Compile Error: Constructor not defined: [dataTableCon].<Constructor>() at line 22 column 32

 

 

 

 

this is what i have:

public class dataTableCon{
public Candidate__c cc{set;get;}
public list<Candidate__c> cvd{get;set;}

public dataTableCon(ApexPages.StandardController controller) {
cvd = new list<Candidate__c>();

cvd = [select Full_Name__c, JobSeeker_Type__c,Years_of_exp__c from Candidate__c ];

    }

    
           static testmethod void Testmethod1() {

           Candidate__c candidate = new Candidate__c( 
           First_Name__c = 'Test Candidate',
           JobSeeker_Type__c = 'Test',
           Years_of_exp__c = '2' );

           insert candidate;

            dataTableCon dtc = new dataTableCon();

            System.assertEquals( 1, dtc.cvd.size() );

}
}

 

ClintLeeClintLee

Sorry, I skimmed over the class.  Like Srikant mentioned above you'll need to instantiate a StandardController and pass it to the constructor. 

 

ApexPages.StandardController stc = new ApexPages.StandardController( Candidate__c );  // add this line

dataTableCon dtc = new dataTableCon( stc );                                                                          // modify this line to add the standard controller to the constructor

 

 

~ Clint

NikiG22NikiG22

Thank you again for your help!! I really need to work on my test classes.

 

here is the final code:

public class dataTableCon{
public Candidate__c cc{set;get;}
public list<Candidate__c> cvd{get;set;}

public dataTableCon(ApexPages.StandardController controller) {
cvd = new list<Candidate__c>();

cvd = [select Full_Name__c, JobSeeker_Type__c,Years_of_exp__c from Candidate__c ];

    }
    
    
   static testmethod void testOne() {

           Candidate__c candidate = new Candidate__c( 
           First_Name__c = 'Test Candidate',
           Last_Name__c = 'Test LastName',
           JobSeeker_Type__c = 'Test',
           Years_of_exp__c = '2' );

            insert candidate;

            ApexPages.StandardController stc = new ApexPages.StandardController( candidate );

           dataTableCon dtc = new dataTableCon( stc );  

}
  }

 Cheers,

Niki

This was selected as the best answer
NikiG22NikiG22

Ok - so i have another project i need help on the test class?i can only get it to 31% and i need it higher so i can move it to Prod.

 

here is the trigger:

trigger RollUp on Resume_Subscriptions__c (after update, before delete) {

    // Set to hold the IDs of the Contacts of the Subscriptions
    Set<ID> ContactIDs= new Set<ID>();
    // Set to hold the IDs of the Subscriptions
    Set<ID> SubIDs = new Set<ID>();
    // Set to hold the IDs of the Contacts where the values need to be reset
    Set<ID> ContactIDsForReset = new Set<ID>();

    if(trigger.isupdate || trigger.isInsert)
    {
        for(Resume_Subscriptions__c rsSub : trigger.new )
        {
            // If the contact of a subscription has changed, then the old contact also needs to be updated 
            if( rsSub.User__c != trigger.oldmap.get(rsSub.ID).User__c)
                if( trigger.oldmap.get(rsSub.ID).User__c != null )
                    ContactIDs.add(trigger.oldmap.get(rsSub.ID).User__c);
            if(rsSub.User__c!=null)
            {
                ContactIDs.add(rsSub.User__c);
            }
        }
        ContactIDsForReset.addAll(ContactIDs);

        // The list of Contacts where the update has to take place
        List<contact> ContactsToUpdate = new List<contact>();    

        // Querying the database to get the Number of Subscriptions under an contact and the sum of the deal values of the Subscriptions under an contact
        AggregateResult[] Subscriptionsum = [select count(ID) NoOfSubscriptions, User__c ContactID from Resume_Subscriptions__c where Active__c=true AND User__c in: ContactIDs group by User__c ];
        
        for( AggregateResult ContactDetail : Subscriptionsum )
        {
            contact ContactToUpdate = new contact(ID = (ID)(ContactDetail.get('ContactID')));
            ContactToUpdate.Active_Resumes__c = integer.valueOf(ContactDetail.get('NoOfSubscriptions'));
            ContactsToUpdate.add(ContactToUpdate);
            // The contact being handled should not be reset. Hence removing it from the ResetSet
            ContactIDsForReset.remove((ID)(ContactDetail.get('ContactID')));
        }
        
        for( ID i : ContactIDsForReset )
        {
            // Resetting the summed up values to 0 if the contact no longer has a subscription.
            contact ContactToUpdate = new contact( ID = i );
            ContactToUpdate.Active_Resumes__c = 0;
            ContactsToUpdate.add(ContactToUpdate);
        }
        
        if(ContactsToUpdate.size() > 0)
            update ContactsToUpdate;
            
        ContactsToUpdate.clear();
    }

    if(trigger.isdelete)
    {
        for(Resume_Subscriptions__c rsSub : trigger.old )
        {
            if( rsSub.User__c != null)
                ContactIDs.add(rsSub.User__c);
            SubIDs.add(rsSub.ID);
        }
        ContactIDsForReset.addAll(ContactIDs);

        // The list of Contacts where the update has to take place
        List<contact> ContactsToUpdate = new List<contact>();    
        
        // Querying the database to get the Number of Subscriptions under an contact excluding the Subscriptions being deleted
        AggregateResult[] Subscriptionsum = null;
        if(ContactIDs.size() > 0)
        {
            Subscriptionsum = [select count(ID) NoOfSubscriptions, User__c ContactID from Resume_Subscriptions__c where User__c in: ContactIDs and ID not in: SubIDs group by User__c ];

            for( AggregateResult ContactDetail : Subscriptionsum )
            {
                contact ContactToUpdate = new contact(ID = (ID)(ContactDetail.get('ContactID')));
                ContactToUpdate.Active_Resumes__c = integer.valueOf(ContactDetail.get('NoOfSubscriptions'));
                ContactsToUpdate.add(ContactToUpdate);
                // The contact being handled should not be reset. Hence removing it from the ResetSet
                ContactIDsForReset.remove((ID)(ContactDetail.get('ContactID')));
            }

            for( ID i : ContactIDsForReset )
            {
                // Resetting the summed up values to 0 if the contact no longer has a subscription.
                contact ContactToUpdate = new contact( ID = i );
                ContactToUpdate.Active_Resumes__c = 0;
                ContactsToUpdate.add(ContactToUpdate);
            }
        }
        
        if(ContactsToUpdate.size() > 0)
            update ContactsToUpdate;

        ContactsToUpdate.clear();
    }
}

 Here is the test class:

@isTest

private class RollUp_Test{

static testmethod void RollUp_Test()

{

contact con = new contact();

con.Lastname='test';
con.SystemOfOrigin__c ='test';
con.Active_Resumes__c =0;


insert con;

Resume_Subscriptions__c res = new Resume_Subscriptions__c();

            res.Product_Name__c='test';
            res.Opportunity__c ='006R00000079BaC';
            res.Views_Purchased3__c = 30;
            res.Subscription_Term3__c = 30;
            res.Price__c = 30;
            res.Daily_View_Limit3__c = 30;
            res.Account__c = '0015000000Z5lxO';

insert res;

 }
}

 please help!

Srikant JoshiSrikant Joshi

Thanks BillClint.

 

Check if this increases the coverage.

 

@isTest

private class RollUp_Test{

static testmethod void RollUp_Test()

{

contact con = new contact();

con.Lastname='test';
con.SystemOfOrigin__c ='test';
con.Active_Resumes__c =0;


insert con;

Resume_Subscriptions__c res = new Resume_Subscriptions__c();

            res.Product_Name__c='test';
            res.Opportunity__c ='006R00000079BaC';
            res.Views_Purchased3__c = 30;
            res.Subscription_Term3__c = 30;
            res.Price__c = 30;
            res.Daily_View_Limit3__c = 30;
            res.Account__c = '0015000000Z5lxO';

insert res;

update res;// update to cover the code for After update(Correct values if update throws an exception).

delete res; // Delete to cover the code for before delete(Correct values if delete throws ane xception).

}
}

 

NikiG22NikiG22

That moved me to 51% thank you soo much!