+ Start a Discussion
manjirimanjiri 

How to write test classes for apex controller method

Hi,

Currently I am new, in writing testcases for apex classes.
I want to test the method for apex controller for visualforce page
e.g.public PageReference next()
how can I write test method for this?
Can anybody share the example?

Thanks
aalbertaalbert
There is an example in the Visualforce Developer's Guide:

1. http://www.salesforce.com/us/developer/docs/pages/index.htm
2. Go to Visualforce Controllers -> Custom Controllers and Controller Extensions -> Testing Custom Controllers and Extensions

In short, to write test methods for controllers, you want to write test scenarios to validate your controller's behavior.
You can instantiate the controller class just like any other class or object. Then you can 'set' and 'get the property values to start testing the
test scenarios. And you can also execute the action methods which return a PageReference and check to see if the proper Page or URL is returned.
I know this is just a start, but I hope it helps.

gv007gv007

Check Apex language reference manual available in the wiki.

Try to implement all the test methods in a singe class based in the situation decided how many test class u want.

manjirimanjiri
Thanks for the reply.
manjirimanjiri
Hi Aalbert,

Thanks,
Currently I want to do test coverage for triggers, I am searching for that. If you know any solution example please share that.

Thanks

shillyershillyer

Here's a great example from one of our partners on bulkifying a trigger and how to write the correlating test class with 100% test coverage.

Best,

Sati

manjirimanjiri
Good Example, Thanks for the reply.