0

I am testing an updatecase method in my class which look like this:

 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;
 }
 public List<Case> excase{get;set;} // stores the list of cases in the contact
 public String radioValue{get;set;}
 public Pagereference updateCase() {
     radioValue = apexpages.currentpage().getparameters().get('group');
     showtable=true;
     case cd = [select Id from Case where Id = : radioValue];
     cd.Last_Verified_by_User__c = UserInfo.getUserId();
     cd.Last_Verified_Date_Time__c = datetime.now();
     update cd;
     Pagereference ref = new PageReference('/' + cd.Id);
     return ref;
 }

I get the id of the case selected using radio button in my VF page:

<apex:pageblockTable value="{!excase}" var="exscase" id="caselist" rendered="{!showtable}">
    <apex:column headervalue="Select Case">
        <input type="radio" name="group" value="{!exscase.Id}" />
    </apex:column>
    <apex:column value="{!exscase.CaseNumber}"/>
        <apex:column value="{!exscase.Status}"/>
    <apex:column value="{!exscase.Subject}"/>
</apex:pageblockTable> 

When i use the update function it updates two fields in the case record. I am trying to test this functionality but struck at how I can assign the value to radiovalue variable in my test class to get the case and test it. So far:

   @isTest
static void testupdatecase(){        
   Contact ct= new Contact (Firstname='test_firstname',LastName = 'test_lastname');
   CreateNewCase caseobject=new CreateNewCase(new ApexPages.StandardController (ct));
   insert ct;
   PageReference createref=caseobject.createCase();
   caseobject.showtable=true;
   caseobject.first=ct.FirstName;
   caseobject.last=ct.LastName;
   List<case> testcaselist=[Select Id, CaseNumber, Status, Subject from Case where contact.Name = : caseobject.first + ' ' + caseobject.last];

// CreateCase is my VF page name
    PageReference resPg=Page.CreateCase;
    system.debug('============================> Pagereference ' + resPg.getUrl());
    Test.setCurrentPageReference (new PageReference(resPg.getUrl() + '?Name=' + testcaselist[0].Id));
    resPg = caseobject.updateCase();
    System.assertEquals (resPg.getUrl(), '/' + testcaselist[0].Id);


    }
  • 1
    using a radio-type button on each row is typically handled in the controller via a wrapper class around Case that includes a Boolean isSelected vbl and a Case cs variable – cropredy Apr 29 '16 at 21:09
  • @cropredy I am not sure why my this question was down voted, wondering how can I ask the SFSE community that why this questions was down voted. Any specific reason? – SfdcBat May 6 '16 at 17:00
  • SfdcBat -- no idea why the downvote, - click the flag link and the moderators can assist. – cropredy May 6 '16 at 17:06
  • Unfortunately we can't see who cast specific votes or why (that's in their head) though it does appear as if you've had a few down votes from a certain user. Chances are the system will reverse those as a case of serial down-voting. Afraid I can't do it manually! – Matt Lacey May 9 '16 at 0:46
  • Thanks for the response. I see downvote on this question has been removed and reverted. I hope this happens for my other question too. – SfdcBat May 9 '16 at 18:57
2

Your 'radioValue' is reading the value from the page parameters. What you need to do is set the page parameters using Test.setCurrentPageReference (https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_test.htm#apex_System_Test_setCurrentPage).

All you need to do is add this line:

Test.setCurrentPageReference (new PageReference ('<your page URL here>?group=ct.Id'));

before calling updateCase()

Having said that, I would try to use visualforce data binding to bind the radio button to your controller's property, instead of having to go into the page parameters to find it.

  • I can't use a separate value for my radio value to set it in my controller since I need the id of the radio button I selected i.e excases.id – SfdcBat Apr 29 '16 at 20:26
  • I am getting an error saying attempt to deference a null pointer exception - I added this in my test code method- List<case> testcaselist=[Select Id, CaseNumber, Status, Subject from Case where contact.Name = : caseobject.first + ' ' + caseobject.last]; PageReference caseref= caseobject.search(); Test.setCurrentPageReference (caseref.getUrl() + '?group=' + testcaselist[0].Id); – SfdcBat Apr 29 '16 at 21:06
  • Sorry , PageReference caseref= caseobject.search(); return a Null , that is why its was throwing an exception. Your line - Test.setCurrentPageReference ('<your page URL here>?group=ct.Id') , which page URL it is and how do I get it? – SfdcBat Apr 29 '16 at 21:22
  • It should be 'apex/<your page name here>'. But it should work with almost any URL. All that matters is the query string. I would suggest editing your original question to show what your code looks like now – mkorman Apr 29 '16 at 22:04
  • 2
    Sorry, see my edit. The documentation I linked originally (you read it, didn't you?) explains clearly the type reference expected by the method. A quick glance at the page told me what the issue was :) – mkorman Apr 29 '16 at 22:55

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.