0

I am writing a test for a method in CreateNewCase class. This method in CreateNewCase class creates a new case and redirects the page to the new case's detail page.:

 public PageReference createCase() {
  Case c = new Case(Status = 'New', Origin = 'Phone', Subject = 'This is a New Case Created',Last_Verified_by_User__c=UserInfo.getUserId(),Last_Verified_Date_Time__c = datetime.now(),ContactId = cnt.Id);
  Insert c;
  Pagereference ref = new PageReference('/' + c.Id);
  return ref;
 }

I tried to test like below. I checked how to use

Pagereference pageref=Page.Mypage;
Test.setCurrentPage(pageRef);

and though I need to set the parameter as testcase.id, however Page.MyPage does not exist in my code. how do I test the redirection to the case details page in that scenerio?

@isTest
static void testcreateCase(){
    CreateNewCase caseobject=new CreateNewCase(new ApexPages.StandardController (new Contact()));
    Case testcase=new Case(Status = 'New');
    insert testcase;

    PageReference resPg = caseobject.createCase();
 //   System.assertEquals(, resPg);
}

1 Answer 1

2

I don't think you need to use Test.setCurrentPage(). That is used to set the page context, to test methods that read values from the current page URL or query strings. Your test could be something like this:

@isTest
static void TestCreateCase () {
    Contact ct= new Contact (LastName = 'Test');
    insert ct;
    CreateNewCase caseobject=new CreateNewCase(new ApexPages.StandardController (ct));

    PageReference resPg = caseobject.createCase();

    List<Case> cases = [SELECT Id, ContactId FROM case];
    // Assert that 1 and only 1 case was created
    System.assertEquals (1, cases.size());
    Case cs = cases[0];
    // Assert that the pageReference redirects to the Case
    System.assertEquals (resPg.getUrl(), '/' + cs.Id);
    // Assert that the case was assigned to the right contact
    System.assertEquals (cs.ContactId, ct.Id);
}
11
  • Yeah there is (almost) no reason to ever call that method.
    – Adrian Larson
    Commented Apr 29, 2016 at 17:42
  • 1
    Only reason to call it would be to set parameters that are got by apexPages.currentParameters().get(xxxx); or when using controller.getId() or similar
    – Eric
    Commented Apr 29, 2016 at 17:48
  • This solution does not address the use case. If there is an error inserting the case the user will be give the ugly error page. While it addresses the need / lack of need to set page reference (which was not a problem even if it was there) it does not solve the issue as a DML error will fail irrespective of the test code provided.
    – Eric
    Commented Apr 29, 2016 at 17:54
  • @Eric But you can add parameters to the default PageReference. Without ever calling it you can call ApexPages.currentPage().getParameters().put(param, value).
    – Adrian Larson
    Commented Apr 29, 2016 at 17:59
  • I agree, there has to be a condition which I need to include where the case insert fails. I will be able to write a test for that as I got an idea now. :)
    – SfdcBat
    Commented Apr 29, 2016 at 18:00

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .