0

I created a small class to render in service console to show previous cases for a specific customer.

Public Class PrevCaseController
{
    public List<Case> Cases {get; set;} 

    public PrevCaseController(ApexPages.StandardController controller)
    { 

      List<Case> C = [SELECT  ContactEmail, SuppliedEmail, Status FROM Case WHERE Id =:controller.getId()];

      String Email;

      if (C[0].SuppliedEmail <> NULL)
      {

      Email = C[0].SuppliedEmail;
          this.Cases = [SELECT Id, CaseNumber, Status FROM Case WHERE SuppliedEmail =: Email]; 
      }
      else
      {
      Email = C[0].ContactEmail;
          this.Cases = [SELECT Id, CaseNumber, Status FROM Case WHERE ContactEmail =: Email]; 
      }

We only use email to case so always will have either contact email or web email although I will add in additional conditions to check once I get this working.

I then created Test class, but still getting 0% on my class. I have tried to change by having more conditions or changing number of cases but still nothing. Can anyone point me in right direction

@isTest
private class PrevCase_TEST 
{

static testMethod void PrevCase()
        {

              test.startTest();  
                Case caseObj = new Case(
                Status = 'New',
                Origin = 'Phone',
                SuppliedEmail = 'Scott@test.com');

                insert caseObj;

                List<Case> C = [Select SuppliedEmail, ContactEmail from Case where Id=:caseobj.id];
                string stremail;

                if (C[0].SuppliedEmail <> NULL)       

                {
                Case caseObj2 = new Case(
                Status = 'New',
                Origin = 'Phone',
                SuppliedEmail = 'Scott@test.com');

                insert caseObj2;
                stremail = C[0].SuppliedEmail;
                }


             List<Case> C2 = [Select Id, CaseNumber, Status from Case where SuppliedEmail=:stremail];

              System.assertEquals(2, C2.size());  

            test.stopTest();

        }


}
  • There's a lot of problems with your test code. I recommend you to read this documentation. First of all, the logic about your insert of cases doesn't make any sense. You insert a case. Then you request the case you inserted but in a list. Aftet that, you check if the SuppliedEmail is not null. But you already set it in your insert. And finally, you request the cases you already inserted in a list. And that, without having called your controller to test.... – Martin Lezer Mar 7 '17 at 8:46
-1

You did not invoked your controller from test class. Try adding this code also in test method.

ApexPages.StandardController sc = new ApexPages.StandardController(caseObj);
PrevCaseController obj = new PrevCaseController(sc);

Points to improve:

You don't actually need to query records repeatedly. Always try to optimise your code for efficiency.

  • 1
    That's nice to try to help him with the call of its controller. But it could be more helpful to tell him that its case insert doesn't make any sense. He won't improve himself with answers like this. – Martin Lezer Mar 7 '17 at 8:47
  • Thanks Reshma, I just added in the object into the constructor and worked. Martin, as I said I had added stuff into the test class to try to get it to actually cover anything. It started out as simple insert and select which it is again. Appreciate the comments though – SLM3573 Mar 7 '17 at 9:01
  • @SLM3573 I guess you've got what was wrong, but I think there is problem with your controller too. Have a look at my below answer. – Rohit Mourya Mar 7 '17 at 9:23
3

As @Martin pointed out, there are couple of problems with your test class.

  1. No need to fetch Case once again if you've inserted it. You can directly use that object without querying it.
  2. You haven't created object of your controller PrevCaseController. That is why your class is not getting invoked.

Also there is also some issue with your controller.

  1. You're unnecessary executing common code irrespective of if else conditions.
  2. Also always check if List size is greater that 0. This will prevent unnecessary exceptions in your code.

This is how your Controller should be:

 Public Class PrevCaseController
{
    public List<Case> Cases {get; set;} 

    public PrevCaseController(ApexPages.StandardController controller)  { 

        List<Case> C = [SELECT  ContactEmail, SuppliedEmail, Status FROM Case WHERE Id =:controller.getId()];

        String Email;
        if(C.size () > 0) {
            if (C[0].SuppliedEmail <> NULL)
                Email = C[0].SuppliedEmail;
            else
                Email = C[0].ContactEmail;

        }
    }
}

And your Test class:

@isTest
private class PrevCase_TEST {

    static testMethod void PrevCase() {

        test.startTest();  
        Case caseObj = new Case(
        Status = 'New',
        Origin = 'Phone',
        SuppliedEmail = 'Scott@test.com');

        insert caseObj;

        ApexPages.StandardController controller = new ApexPages.StandardController(caseObj);
        PrevCaseController pv = new PrevCaseController(controller );
        System.assertEquals(1, C2.size());  

        test.stopTest();
    }
}
  • 2
    That's nice to try to help him with the call of its controller. But it could be more helpful to tell him that its case insert doesn't make any sense. He won't improve himself with answers like this. – Martin Lezer Mar 7 '17 at 8:47
  • @MartinLezer Done Martin. – Rohit Mourya Mar 7 '17 at 9:03

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.