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
BeekmanBeekman 

Visual force page test method

Hello,

 

I have a Visual Force Page that I need to test the controller.  Both are attached.  I cannot figure out how to create a testmethod to perform test against this page.  The controller code needs to execute so it can pass testing.

 

Any help with code would be appreciated.

 

Thanks

 

Visual Page Code:

<apex:page showheader="false" controller="systemStatusController">
	<style>
		ul li {
			margin: 10px 0;
		}
		ul.supportLevels li {
			margin: 5px 0;
			list-style: none;
			width:375px;
		}
		ul.supportLevels li span {
			float:right;
		}
	</style>
	<p class="systemDetails">
		<ul>
			<li>You have <a href="#">{!envrCount}</a> Locations with Envysion&nbsp;&nbsp;<a href="mailto:sales@envysion.com?subject=Add%20a%20Site%20to%20my%20Envysion%20Service">add new location</a></li>
			<li>You have {!cameraCount} Cameras across your Locations&nbsp;&nbsp;<a href="mailto:sales@envysion.com?subject=New%20Cameras%20for%20a%20Site">add/replace cameras</a></li>
			<li>Your support levels are:
				<ul class="supportLevels">
					<li>{!notifiesCount} Sites with <i>Envysion Notifies</i><span><a href="mailto:sales@envysion.com?subject=Upgrade%20my%20Envysion%20Support">upgrade</a></span></li>
					<li>{!respondsCount} Sites with <i>Envysion Responds</i><span><a href="mailto:sales@envysion.com?subject=Upgrade%20my%20Envysion%20Support">upgrade</a></span></li>
					<li>{!maintainsCount} Sites with <i>Envysion Maintains</i><span><a href="mailto:sales@envysion.com?subject=Upgrade%20my%20Envysion%20Support">upgrade</a></span></li>
				</ul>
			</li>
		</ul>
	</p>
</apex:page>

 

 

Class Controller code:

public with sharing class systemStatusController {
	private User currentUser;
	private Account currentAccount;
	private String defAcct;
	
	public void setDefaultAccount(String acctID) {
		this.defAcct = acctID;
	}
	
	public User getCurrentUser() {
		if(this.currentUser == NULL) {
			this.currentUser = [SELECT Id, FirstName, LastName, Alias, AccountId FROM User WHERE id = :UserInfo.getUserId()];
		}
		return this.currentUser;
	}
	
	public Account getCurrentAccount() {
		if(this.currentAccount == NULL) {
			User curUser = this.getCurrentUser();
			String accID = curUser.AccountId;
			if(accID == NULL || accID == '') {
				accID = this.defAcct;
			}
			this.currentAccount = [SELECT Id, Name FROM Account WHERE Id = :accID];
		}
		return this.currentAccount;
	}
	
	public Integer getEnvrCount() {
		Account curAcct = this.getCurrentAccount();
		Integer envrCount = [SELECT COUNT() FROM Envr__c WHERE Account__c = :curAcct.Id];
		return envrCount;
	}
		
	public Integer getCameraCount() {
		Account curAcct = this.getCurrentAccount();
		Integer camCount = [SELECT COUNT() FROM Camera__c WHERE Account__c = :curAcct.Id];
		return camCount;
	}
			
	public Integer getNotifiesCount() {
		Account curAcct = this.getCurrentAccount();
		Integer siteCount = [SELECT COUNT() FROM EnvySite__c WHERE Account__c = :curAcct.Id AND Support_Level__c = 'Notifies'];
		return siteCount;
	}
				
	public Integer getRespondsCount() {
		Account curAcct = this.getCurrentAccount();
		Integer siteCount = [SELECT COUNT() FROM EnvySite__c WHERE Account__c = :curAcct.Id AND Support_Level__c = 'Responds'];
		return siteCount;
	}
				
	public Integer getMaintainsCount() {
		Account curAcct = this.getCurrentAccount();
		Integer siteCount = [SELECT COUNT() FROM EnvySite__c WHERE Account__c = :curAcct.Id AND Support_Level__c = 'Maintains'];
		return siteCount;
	}
}

 

MellowRenMellowRen

Hi

 

I can start you off. You don't give much details about the page itself. Does this page require any parameters? It doesn't seem like it from the controller  so with this test code I am assuming not.

 

@isTest(seeAllData=true)
public with sharing class VFControllerTest_systemStatusController {
    
static TestMethod void testsystemStatusController() { // Create required Objects for the test // None? Test.startTest(); // Test starts here as the above will trigger triggers :-) // load the controller systemStatusController ssc = new systemStatusController(); //test check the Envysion location count Integer ec = ssc.getEnvrCount(); //a system.assert or two here would be advisable.
//test check the Camera count
Integer cc = ssc.getCameraCount();
//again, a system.assert or two here would be advisable.
//etc
//etc
Test.stopTest(); } }

 

This approach should allow you to reach your code coverage requirement. Adding some assert tests will make it a safer bet though.

 

Regards

MellowRen

nbeekmannbeekman

hello MellowRen,

 

Yes it needs an Account, which I cannot figure out how to pass it.  I normally pass it with this type of code (example from others I have tested)

 

ApexPages.StandardController sc = new ApexPages.standardController(case_list2.get(0));
CaseExtension ext = new CaseExtension(sc);
List<CaseWrapper> open_cases = ext.getOpenCases();

 

But I cannot get it to work with this VF page (which is also attached with the previous post)

 

When I try with your code it comes back with an error that its missing or doesnt have the sObject list.

 

Thanks

MellowRenMellowRen

nbeekman

 

Hmmm, OK, try this addition:

 

@isTest(seeAllData=true)
public with sharing class VFControllerTest_systemStatusController {
    
static TestMethod void testsystemStatusController() { // Create required Objects for the test // Account
Account a1 = new Account(Name = 'Test Account');
INSERT a1; Test.startTest(); // Test starts here as the above will trigger triggers :-) //load the Account page
PageReference pageRef = Page.theVFpageNameGoesHere;
pageRef.getParameters().put('Id',a1.Id);
Test.setCurrentPageReference(pageRef);

// load the controller systemStatusController ssc = new systemStatusController();
//test check the Envysion location count Integer ec = ssc.getEnvrCount(); //a system.assert or two here would be advisable.
//test check the Camera count
Integer cc = ssc.getCameraCount();
//again, a system.assert or two here would be advisable.
//etc
//etc
Test.stopTest(); } }

 

I usually write extensions rather than direct controllers but this should work.

 

Regards

MellowRen