You need to sign in to do that
Don't have an account?

How can I test my VisualForce page ?
Hello guys, I have the following Visualforce page, name is screen1VSF with a controller.
<apex:page controller="screen1">
<apex:form >
<apex:pageBlock >
<apex:pageBlockSection >
<apex:inputField value="{!account.name}"/>
<apex:commandButton value="Save" action="{!save}"/>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:form>
</apex:page>
public class screen1
{
public Account myAccount;
public Account callAccMethod;
public Account getAccount()
{
if(myAccount == null)
{
myAccount = new account();
}
return myAccount;
}
public PageReference save()
{
callAccMethod = getAccount();
if(callAccMethod != null)
{
insert myAccount;
}
PageReference pageRef = new PageReference('/apex/screen2');
pageRef.getParameters().put('id', myAccount.Id);
return pageRef;
}
}
I read some documentation from here: https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_controller_error_handling.htm but I'm still confused how this testing with VF works. Any ideas how can I test my visualforce page and explain how you did it, i want to learn.
<apex:page controller="screen1">
<apex:form >
<apex:pageBlock >
<apex:pageBlockSection >
<apex:inputField value="{!account.name}"/>
<apex:commandButton value="Save" action="{!save}"/>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:form>
</apex:page>
public class screen1
{
public Account myAccount;
public Account callAccMethod;
public Account getAccount()
{
if(myAccount == null)
{
myAccount = new account();
}
return myAccount;
}
public PageReference save()
{
callAccMethod = getAccount();
if(callAccMethod != null)
{
insert myAccount;
}
PageReference pageRef = new PageReference('/apex/screen2');
pageRef.getParameters().put('id', myAccount.Id);
return pageRef;
}
}
I read some documentation from here: https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_controller_error_handling.htm but I'm still confused how this testing with VF works. Any ideas how can I test my visualforce page and explain how you did it, i want to learn.
VF page with Controller means you have write tesst class for your controller.
@isTest
public class screen1Test {
public static testMethod void testController()
{
PageReference pageRef = Page.screen1VSF;
Test.setCurrentPage(pageRef);
screen1 sc = new screen1();
String nextPage = sc.save().getUrl();
}
}
I'm stuck here. I don't know what to test and how can i test my controller. Any ideas, i need to understand how this works.
@isTest
private class ScreenTest {
static testMethod void UnitTest1(){
screen1 scr = new screen1();
Account acc = scr.getAccount();
acc.name = 'TestAcc';
scr.save();
}
}
Happy Coding!! :)
Try as below:
@isTest
public class ControllerTestClass
{
static testMethod void testMethod1()
{
Account testAccount = new Account();
testAccount.Name='Test Account' ;
insert testAccount;
Test.StartTest();
PageReference pageRef = Page.screen1VSF ; // Add your VF page Name here
pageRef.getParameters().put('id', String.valueOf(testAccount.Id));
Test.setCurrentPage(pageRef);
screen1 testAccPlan = new screen1();
//testAccPlan.save(); call all your function here
Test.StopTest();
}
I have 100% code coverage!
Nice. So what you wrote in this small code was only to create new object screen1() then you created a variable acc with data type account and this variable calls the getAccount() method and then you created an account with name 'TestAcc' and then call the save() method, right ? Can you explain me please why i have 100% code coverage?
You can refer the below refernces:http://amitsalesforce.blogspot.in/2017/02/salesforce-tutorial-how-to-learn-test.html
http://amitsalesforce.blogspot.in/2015/06/best-practice-for-test-classes-sample.html
Please mark it as best answer which if it resolved your query
public class ControllerTestClass
{
static testMethod void testMethod1()
{
screen1 testAccPlan = new screen1();
Account testAccount = testAccPlan.getAccount();
testAccPlan.save();
PageReference pageRef = Page.screen1VSF ; // Add your VF page Name here
pageRef.getParameters().put('id',testAccount.Id);
Test.setCurrentPage(pageRef);
System.assertEquals(expected, actual);
//screen1 testAccPlan = new screen1();
}
}
I have 100% percent with this code. I covered all methods. Now i need to make assertions. How can i make some assertions on this test method ?
This Error is occured because you haven't inserted any account with the required field 'name' in it. You don't need to create an instance of account record at #line 7 since your test environment already return it after calling getAccount() method. so just provide name for account after getting the instance returned by test method and then call save method with same instance that you have already created at #line 6 i.e sc.save() please do not create the new instance again for the class at #line 13 So apart from :-
screen1 sc = new screen1();
Account testAccount = new Account();
testAccount.Name='TestAccount' ;
Account acc = sc.getAccount();
Just Write :-
Test.StartTest();
screen1 sc = new screen1();
Account acc = scr.getAccount();
acc.name = 'TestAcc';
sc.save();
Test.StopTest();
}
If this really help you please mark it as best answere. :)