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
LoneStar69LoneStar69 

test class for simple save

Guys, i am trying to write a test class for a simple save method.
This might be simple, but i cannot get this correct!!!

Apex Class -

public with sharing class ExtUpdated {
ApexPages.StandardController stdcontroller ;
    public ExtUpdated(ApexPages.StandardController controller) {
    stdcontroller = controller;    }
public PageReference todo(){
stdcontroller.save();
return (new pagereference('/apex/confirm').setredirect(true));}}

Test Class -

@isTest
public class TestExtUpdated {
    static testMethod void saveNewCase() {
Case co = new Case();
        insert co;
        Test.startTest();
PageReference pageRef = Page.MyVFPage; // Add your VF page Name here
Test.setCurrentPage(pageRef);
ApexPages.StandardController sc = new ApexPages.StandardController(co);
sc.save();
Test.stopTest();
}}

No errors, but the test coverage is 0%.

Thanks for your help!!!
Best Answer chosen by LoneStar69
Swayam@SalesforceGuySwayam@SalesforceGuy
Hey,

You need to instantaniate and call todo method of your class from test class, 
 
@isTest
public class TestExtUpdated 
	{
		static testMethod void saveNewCase() 
		{
		Case co = new Case();
		insert co;
		
		Test.startTest();
		
		ApexPages.StandardController sc = new ApexPages.StandardController(co);
		ExtUpdated  extUpdatedObj = new ExtUpdated (sc);
		extUpdatedObj.todo();
		PageReference pageRef = Page.MyVFPage; // Add your VF page Name here
		Test.setCurrentPage(pageRef);
		
		Test.stopTest();
		}
	}


Hope this help

--
Thanks
Swayam
@salesforceguy
 

All Answers

Swayam@SalesforceGuySwayam@SalesforceGuy
Hey,

You need to instantaniate and call todo method of your class from test class, 
 
@isTest
public class TestExtUpdated 
	{
		static testMethod void saveNewCase() 
		{
		Case co = new Case();
		insert co;
		
		Test.startTest();
		
		ApexPages.StandardController sc = new ApexPages.StandardController(co);
		ExtUpdated  extUpdatedObj = new ExtUpdated (sc);
		extUpdatedObj.todo();
		PageReference pageRef = Page.MyVFPage; // Add your VF page Name here
		Test.setCurrentPage(pageRef);
		
		Test.stopTest();
		}
	}


Hope this help

--
Thanks
Swayam
@salesforceguy
 
This was selected as the best answer
LoneStar69LoneStar69
Hi Swayam,

Thanks for your response, but the coverage still shows 0%.
ericmonteericmonte
Try hitting run all test. Sometimes I have the same issue as well and the code coverage doesn't work to well using dev console. Also you can check with run test using eclipse to see where you stand the class.
LoneStar69LoneStar69
Thanks for the info Ericmonte, was able to deploy fine though.