1

I have a simple class that validates user input and assigns cases.

Class -

public with sharing class CaseAssign {
public Case c { get; set; }
public boolean choice(){

    if( (c.First_Selection__c == true && c.Second_Selection__c == true) || (c.First_Selection__c != true && c.Second_Selection__c != true) ){
    ApexPages.Message myCho = new ApexPages.Message(ApexPages.Severity.FATAL, 'Please select one checkbox:');
        apexpages.addmessage(myCho);
        return false;
        }

if((c.First_Selection__c == true && c.First_Topics__c == null)){
    ApexPages.Message myOpt1 = new ApexPages.Message(ApexPages.Severity.FATAL, 'Please select a Topic');
        apexpages.addmessage(myOpt1);
        return false;
        }

if((c.Second_Selection__c == true && (c.Branch__c == null || c.Office__c == null)) ){
  ApexPages.Message myOpt2 = new ApexPages.Message(ApexPages.Severity.FATAL, 'Please select both branch and office');
        apexpages.addmessage(myOpt2);
        return false;
  }

else {
        return true;
    }

}

public boolean allsel(){
    if(c.Aff__c == null || c.Email__c == null || c.Country__c == null || c.Zip_Code__c == null || c.Question__c == null ){
    ApexPages.Message mySel = new ApexPages.Message(ApexPages.Severity.FATAL, 'Please fill all the required information');
        apexpages.addmessage(mySel);
        return false;
        }
    else {
        return true;
}

}

public PageReference verify() {


    boolean sel = choice();
boolean asl = allsel();

     system.debug('---res is -->'+res);
if(sel == false || asl == false)
{
return null;
  }
else
{
String emailId = c.Email__c; 
     String queueName;

    if(c.First_Selection__c == true){
    c.Branch__c = null;
c.Office__c = null;
    queueName='Second Support';


         if(c.First_Topics__c == 'XYZ'){

             queueName='Third Support'; 
               }
          }
else if (c.Second_Selection__c == true ){
     c.First_Topics__c = null;
     queueName='First Support';
    }

    id queueGroupId= [Select Id from Group where type='Queue' and Name= :queueName].Id;
      c.origin = 'Web';
      c.ownerId = queueGroupId;
      c.status = 'Assigned';
      INSERT c;

          return (new pagereference('/apex/SurveyPage').setredirect(true));
         }

  }
}

Test Class -

@isTest
Private class CaseAssignTest{
static testmethod void myUnitTest(){

Test.startTest();

            CaseAssign CA = new CaseAssign();
            Case objC = new Case();
            objc.origin='web';
            objc.Email__c='Test1@gmail.com';
            objc.status='Assigned';
            objc.zip_code__c='23000'; 
            objc.country__c='United States';
            objc.First_Selection__c = true;
            objc.First_Topics__c = null;
            insert objC;

    Case objC3 = new Case();
            objc3.origin='Assigned';
            objc3.Email__c='Test4@gmail.com';
            objc3.status='Web';
            objc3.zip_code__c='23000'; 
            objc3.country__c='United States';
            objc3.Second_Selection__c = true;
            objc3.Office__c= 'cheese';
            objc3.Branch__c=null;
            insert objC3;


     try{ CA.choice()    }
             catch(exception e){}

    try{  CA.allsel();
             }
             catch(exception e){}
try{
             CA.verify();
             }
             catch(exception e){}

After reading thru previous posted questions i tried something for the apex page messages.

But no luck... I get an error message assertion failed...

    Test.setCurrentPage(Page.caseassign);

    List<Apexpages.Message> msgs = ApexPages.getMessages();

    boolean isErrorMessage = false;

    for(Apexpages.Message msg : msgs){
        if (msg.getDetail().contains('Please select a Topic') )
            isErrorMessage  = true;
    }
    system.assert(isErrorMessage );

     Test.stopTest();
    }

 }

Any help is appreciated..thank you!

4
  • can put complete test class here Oct 8 '16 at 23:45
  • @SantanuBoral Hi, my complete test class is included in the question...
    – Khyber
    Oct 8 '16 at 23:49
  • then, where you have added all these page message related code? Oct 8 '16 at 23:52
  • @SantanuBoral Hi, i didn't get you, i included the complete code for my class(contains the code for page message) and test class ( including the test coverage for apex page message that i tried with) in the question.
    – Khyber
    Oct 9 '16 at 0:03
1

First, create test data which could throw error. For example: 'Please select one checkbox:' error scenario.

CaseAssign CA = new CaseAssign();
List<Apexpages.Message> msgs = ApexPages.getMessages();

Case objC = new Case();
objc.origin='web';
objc.Email__c='Test1@gmail.com';
objc.status='Assigned';
objc.zip_code__c='23000'; 
objc.country__c='United States';
objc.First_Selection__c = true;
objc.Second_Selection__c  = true; //here both cannot be true
insert objC;
CA.c = objC;
CA.choice(); //it will throw error messages 'Please select one checkbox:'
boolean isErrorMessage = false;

//now retrieve the error messages
for(Apexpages.Message msg : msgs){
if (msg.getDetail().contains('Please select one checkbox')
    isErrorMessage  = true;
}
system.assert(isErrorMessage );

Now retrieve the Case record and update respective attributes to throw the error and the same way you can capture the error message and verify assert.

Hope it helps!

5
  • Hi, same result, it says assertion failed...
    – Khyber
    Oct 9 '16 at 0:27
  • I have tried to give you the logic to capturing the messages. You could debug and implement the code according to your need Oct 9 '16 at 0:30
  • updated the msg checking if clause Oct 9 '16 at 0:33
  • you know what, i removed the assertion and just creating the negative test data that mimics each page message increased the coverage, right now at 73%...and it shows no coverage for the verify() right from this line 'String emailId = c.Email__c;' to the end of the code, can you suggest something here?
    – Khyber
    Oct 9 '16 at 0:43
  • creating the positive test data for the same along with Q assignment did the trick, appreciate your help man, thanks a ton!
    – Khyber
    Oct 9 '16 at 0:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.