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
vanessa veronvanessa veron 

Test Class Example

Hi...
global class AAAA implements System.Schedulable {
global String str{get;set;}
global String mail{get;set;}
global String nomJob{get;set;}
global String inputText{get;set;}
global String heure{get;set;}
global String minute{get;set;}
global String jourMois{get;set;}
global String mois{get;set;}
global String jourSemaine{get;set;}

global void execute(SchedulableContext sc) {
  newPublier();
}

global AAAA () {

}

global AAAA (String mail, String inputText, String heure, String minute, String jourMois, String mois, String jourSemaine) {
this.mail= mail;
this.inputText = inputText;
this.heure= heure;
this.minute= minute;
this.jourMois= jourMois;
this.mois= mois;
this.jourSemaine= jourSemaine;
}

public void setMail(String mail) {
    this.mail= mail;
}

public String getMail() {
    return mail;
}

public void setRequete(String inputText) {
    this.inputText= inputText;
}

public String getRequete() {
    return inputText;
}

public void setHeure(String heure) {
    this.heure= heure;
}

public String getHeure() {
    return heure;
}

public void setMinute(String minute) {
    this.minute= minute;
}

public String getMinute() {
    return minute;
}

public void setJourMois(String jourMois) {
    this.jourMois= jourMois;
}

public String getJourMois() {
    return jourMois;
}

public void setMois(String mois) {
    this.mois= mois;
}

public String getMois() {
    return mois;
}

public void setJourSemaine(String jourSemaine) {
    this.jourSemaine= jourSemaine;
}

public String getJourSemaine() {
    return jourSemaine;
}


public void schedulejob(){
        String aaa = getMail();
        String req = getRequete();
        String heu = getHeure();
        String min = getMinute();
        String jMois = getJourMois();
        String leMois = getMois();
        String jSemaine = getJourSemaine();
                
        String NomJobSchedulable = nomJob;
        AAAA p = new AAAA (aaa, req, heu, min, jMois, leMois,jSemaine);
        String sch = '0'+' '+min+' '+heu+' '+jMois+' '+leMois+' '+jSemaine;
 
        system.schedule(NomJobSchedulable , sch, p);   
}

public void newPublier(){

    String query=inputText;
    String premier=query.substringAfter('select ');   
    premier=  premier.substringBefore('from');
      
    string titre= premier+'\n';
    string contenuCSV = titre;

    string queryResultatString = '';

    list<sObject> queryResultat = (List<sObject>)database.query(inputText);
    for(sObject a: queryResultat)
    {

        queryResultatString = queryResultatString + string.valueof(a);
        
    }
    System.debug('Query result string:'+queryResultatString);

    list<string> queryLignes = queryResultatString.split('}');

    for(string s:queryLignes){
        list<string> queryColonnes = s.split(',');
        for(string st:queryColonnes){
            contenuCSV = contenuCSV + st.substringAfter('=') + ',';
        }

        contenuCSV = contenuCSV.substringBeforeLast(',').substringBeforeLast(',') + '\n';
    }

    String lignes = contenuCSV;
    List<String> parts = lignes.split('\n');
    integer lineNumber = queryResultat.size();

    integer nbLignesPJ = 1000;
    integer compterParties=0;

    for(integer i=0;i<lineNumber;i++){

      string fichierParties = parts[0] + '\n';

      if(math.mod(i,nbLignesPJ)<>0) continue;
      if((lineNumber-i)<nbLignesPJ){

        for(integer j=1;j<=(lineNumber-i);j++){
            fichierParties = fichierParties + parts[i+j] + '\n';
        }
      }
      if((lineNumber-i)>=nbLignesPJ){
         for(integer j=1;j<=nbLignesPJ;j++){
            fichierParties = fichierParties + parts[i+j] + '\n';
        }
      }
      //Envoyer le Mail
      Messaging.EmailFileAttachment csvPJ = new Messaging.EmailFileAttachment();
      blob csvBlob = Blob.valueOf(fichierParties);
      string csvNom = 'cases_fermes_'+Date.today().format()+'.csv';
      csvPJ.setFileName(csvNom);
      csvPJ.setBody(csvBlob);
      Messaging.SingleEmailMessage email =new Messaging.SingleEmailMessage();
      String[] adressMail = new list<string> {mail};
      compterParties++;
      String subject;
          subject = 'CSV - '+Date.today().format();
      email.setSubject(subject);
      email.setToAddresses(adressMail);
      email.setPlainTextBody('message');   
      email.setFileAttachments(new Messaging.EmailFileAttachment[]{csvPJ});
      Messaging.SendEmailResult [] envoyer = Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email});
    }
  }   
}



Someone give me the sample example of a test class to my class.
I want to publish my application but I do not know to do a test class.

Best Answer chosen by vanessa veron
magicforce9magicforce9
Hi,

Please see the sample code below to begein with..You'll need to populate data into standard/custom objects that will satisfactorily get pulled by the logic you've written in some of the methods of scheduler. Also please correct the values for any of the variables that I've used.

@isTest
public with sharing class Test_AAAA {

    private static String TEST_SCHEDULE_NAME = 'TEST for AAAA Scheduler';

    //Populate the test data
    private static String mail = 'testScheduler@mailinator.com';
    private static String inputText = 'SomeText';
    private static String heure = '16';
    private static String minute = '10';
    private static String jourMois = '*';
    private static String mois = '*';
    private static String jourSemaine = '?';

    /***
    You'll also need to populate data into any standard or custom object that will 
    satisfactorily get pulled by the logic you've written in newPublier() method
    ***/

    static testMethod void test_method_should_pass() {

        String cronExpression = '0'+' '+minute+' '+heure+' '+jourMois+' '+mois+' '+jourSemaine;

        AAAA testScheduler;
        Test.startTest();
            testScheduler = new AAAA(mail, inputText, heure, minute, jourMois, mois, jourSemaine);
            testScheduler.SCHEDULE_NAME = TEST_SCHEDULE_NAME;

            //This should schedule the test job for 16:10 hours
            testScheduler.schedulejob();
        Test.stopTest();

        //This will assert if the job is scheduled for the given time
        System.assertEquals(newCronExpression, [SELECT Id, CronExpression 
                                                FROM CronTrigger 
                                                WHERE id = :testScheduler.scheduledJobId].CronExpression);
    }
}

Hope it helps !

Thanks,
Mohammed

All Answers

magicforce9magicforce9
Hi,

Please see the sample code below to begein with..You'll need to populate data into standard/custom objects that will satisfactorily get pulled by the logic you've written in some of the methods of scheduler. Also please correct the values for any of the variables that I've used.

@isTest
public with sharing class Test_AAAA {

    private static String TEST_SCHEDULE_NAME = 'TEST for AAAA Scheduler';

    //Populate the test data
    private static String mail = 'testScheduler@mailinator.com';
    private static String inputText = 'SomeText';
    private static String heure = '16';
    private static String minute = '10';
    private static String jourMois = '*';
    private static String mois = '*';
    private static String jourSemaine = '?';

    /***
    You'll also need to populate data into any standard or custom object that will 
    satisfactorily get pulled by the logic you've written in newPublier() method
    ***/

    static testMethod void test_method_should_pass() {

        String cronExpression = '0'+' '+minute+' '+heure+' '+jourMois+' '+mois+' '+jourSemaine;

        AAAA testScheduler;
        Test.startTest();
            testScheduler = new AAAA(mail, inputText, heure, minute, jourMois, mois, jourSemaine);
            testScheduler.SCHEDULE_NAME = TEST_SCHEDULE_NAME;

            //This should schedule the test job for 16:10 hours
            testScheduler.schedulejob();
        Test.stopTest();

        //This will assert if the job is scheduled for the given time
        System.assertEquals(newCronExpression, [SELECT Id, CronExpression 
                                                FROM CronTrigger 
                                                WHERE id = :testScheduler.scheduledJobId].CronExpression);
    }
}

Hope it helps !

Thanks,
Mohammed
This was selected as the best answer
vanessa veronvanessa veron
Thank you!!!
vanessa veronvanessa veron
I could not test my method newPublier.
I'm 56% coverage of the code and I can not move.
You can help me to create unit tests for my method newPublier?

Thank you!!!
Michael VerhovskiMichael Verhovski
Vanessa, you could the check code coverage with Developers Console (https://help.salesforce.com/HTViewHelpDoc?id=code_dev_console_tests_coverage.htm) Lines of code that are covered by tests are blue. Lines of code that aren’t covered are red. Lines of code that don’t require coverage are left white.
vanessa veronvanessa veron
Thank you...