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
AmphitriteAmphitrite 

Test coverage low - controller extension

I have a test class that isn't improving my test coverage of controller extension. This class should pass an array of cases that has a field that holds a URL [all cases hold same URL]. Then after the Cancel method runs - it should redirect to the URL.

 

I think it isn't passing because the case records are not inserting properly - but not sure how to debug or fix.


Here is the test code:

 

@isTest
private class BulkPageCancelTest {

static testMethod void TestBulkUpdateCancelCRef(){

 

List<User> users = new List<User>{};

for(Integer i = 0; i < 10; i++){
User c = new User(lastname='Testing'+i);
users.add(c);
}
Insert users;

 

List<Case> cases = new List<Case>{};

for(Integer i = 0; i < 10; i++){
Case c = new Case(InEditUserId__c=users[0].Id,InEditViewURL__c='https://cs13.salesforce.com/500?fcf=00B30000002To4e');
cases.add(c);
}
Insert cases;

 

PageReference pageRef = Page.BulkUpdate;
Test.setCurrentPage(pageRef);
ApexPages.StandardSetController ssc = new ApexPages.StandardSetController(cases);


test.startTest();

BulkUpdate e = new BulkUpdate(ssc);
string ViewURL=cases[0].InEditViewURL__c;
string nextPage = e.CancelExit().getURL();
System.assertEquals(ViewURL,nextPage);

 

test.stopTest();
}

 

Best Answer chosen by Admin (Salesforce Developers) 
ericmonteericmonte
ahh opps i missed that. Lets try moving up the startTest before the PageReference.

All Answers

ericmonteericmonte

Do you think you can provide your controller codes?


Also, I helped out another person in the forum with their test cases, take a look at it and see if it helps you out.

 

http://boards.developerforce.com/t5/Visualforce-Development/Help-with-test-code-for-extension-and-question-about-extensions/td-p/697455

 

and here is another guide to testing Apex Code

http://wiki.developerforce.com/page/An_Introduction_to_Apex_Code_Test_Methods

AmphitriteAmphitrite

Here is my extension class with the relevant cancel method. If the user passes a URL into page to begin with then the InEditViewURL will hold that url value. Otherwise it should just return to the 'cases' standard tab after exit.

 

 

public with sharing class BulkUpdate {

//Global Variables
private ApexPages.StandardSetController controller;

Private string returnView;
Public String userClick { get; set; }
Public String adminClick { get; set; }
Public String cancelClick { get; set; }


// Standard Set Controller Constructor 
public BulkUpdate(ApexPages.StandardSetController Controller) {

this.controller = controller;


}

public ApexPages.StandardSetController caseRecords{

Get{
if(caseRecords==null){
caseRecords = new ApexPages.StandardSetController(Database.getQueryLocator([SELECT id FROM Case WHERE InEditUserId__c =: InEditUserId]));
caseRecords.setPageSize(1000);
}
return caseRecords;

}
Private set;
}


public List<Case> getcSelected(){
return (List<Case>) caseRecords.getRecords();
}

//Cancel and Close method


public PageReference CancelExit(){

IF(getcSelected()[0].InEditViewURL__c!=Null){returnView=getcSelected()[0].InEditViewURL__c;}
Else{returnView='/500/o';}


for(Case a :getcSelected()){

a.UserActions__c=Null;
a.AdminActions__c=Null;
a.Cancellation_Reason__c=a.Cancellation_Reason__c;
a.InEditViewURL__c=Null;

}

Update getcSelected();

PageReference pageRef = new PageReference(returnView);
pageRef.setRedirect(true);
return pageRef;

}

}

 

 

 

I'm reading through the link you provided - thanks!

ericmonteericmonte

Looking into your test Controller and Controller class.

 

Your test class doesn't hit any of your Controller methods.

 

For example I see you created users and created cases, but there is no actions that calls a method specifically hitting the public PageReference CancelExit().

 

Let me know if this make sense and if you have specific questions on the links i provided.


Thanks

AmphitriteAmphitrite

Right before system.assert line is this line:

 

string nextPage = e.CancelExit().getURL();

 

 

I'm fairly certain this runs the method and populates the URL. Is there a better way?

ericmonteericmonte
ahh opps i missed that. Lets try moving up the startTest before the PageReference.
This was selected as the best answer
AmphitriteAmphitrite

Same result. The controller extension actually loads page based on a query against the current user. I realized that maybe the method is finding an empty array - because the system user didn't match the user assigned to the case.

 

I assume I need to use a system run as user. I've added a user and system run as user. But it is still not passing.

 

I appreciate your help.

 

 

 

 

@isTest
private class BulkPageCancelTest {

static testMethod void TestBulkUpdateCancelCRef(){

User[] users = new User[]{};
        for(Integer x=0; x<10;x++){
            user u = new User(LastName='Testing'+x);
            users.add(u);
        }
Insert users;

Case[] cases = new Case[]{};
        for(Integer x=0; x<10;x++){
            case c = new Case(InEditUserId__c=users[0].Id,InEditViewURL__c='/500?fcf=00B30000002To4e');
            cases.add(c);
        }
Insert cases;

System.runAs(users[0]) {
test.startTest();

PageReference pageRef = Page.BulkUpdate;
Test.setCurrentPage(pageRef);
ApexPages.StandardSetController ssc = new ApexPages.StandardSetController(cases);
 
  BulkUpdate e = new BulkUpdate(ssc);
  string nextPage = e.CancelExit().getURL();
  System.assertEquals('/500?fcf=00B30000002To4e',nextPage);

test.stopTest();
}
}


}

AmphitriteAmphitrite

My user inserts were failing due to missing fields. Then moving the start test up and adding in the system run as did the trick.

 

thanks!

ericmonteericmonte

good stuff!! glad you got it working.