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
Saru VadlamudiSaru Vadlamudi 

Test class-Soql Code coverage for custom controller

i have test class for vf controller where as soql which is not not covering in code coverage even after inserting one test opp record.Please suggest me that how to get cover this soql.Here oppId is currentpage parameter.
In debug logs opppDeatils list value is coming as 0 but test opp record is inserted in test class.

List<Opportunity> oppDetails =[Select Name
From Opportunity
Where Id =:ApexPages.currentPage().getParameters().get('oppID')];
Best Answer chosen by Saru Vadlamudi
Chandra Sekhar CH N VChandra Sekhar CH N V
@karuturi, I would suggest you to include two different test methods one for positive test case and another for negative. This should cover both you if and else parts of the code.

All Answers

srlawr uksrlawr uk
Well, we could probably use a slightly wider set of your code here... at a query level I think you are probably alright. The first thing I would check is how are you setting oppID onto the page parameters in your test class?

I would use system.debug right before the query to establish that this
 
ApexPages.currentPage().getParameters().get('oppID')

is working properly. If you are absolutely sure that the opportunity is being inserted into the database (within the scope of this current test method) then that really is the only bit that can be causing the SOQL query to return no results.


 
Amit Chaudhary 8Amit Chaudhary 8
Please check below post. I hope that will help you
http://amitsalesforce.blogspot.in/2015/06/best-practice-for-test-classes-sample.html

Please try below test class. I hope that will help you
@isTest 
public class ExtensionTestClass 
{
	static testMethod void testMethod1() 
	{
		Account testAccount = new Account();
		testAccount.Name='Test Account' ;
		insert testAccount;
		
		opportunity oppr = new opportunity( Name='testing DIE 4/6/2015' ,  AccountId= testAccount.Id, StageName = 'Prospecting', 
										   CloseDate = closeDt );
		insert oppr;

		Test.StartTest(); 
			ApexPages.StandardController sc = new ApexPages.StandardController(testAccount);
			myControllerExtension testAccPlan = new myControllerExtension(sc);

			PageReference pageRef = Page.AccountPlan; // Add your VF page Name here
			pageRef.getParameters().put('oppID', String.valueOf(oppr.Id));
			Test.setCurrentPage(pageRef);

			//testAccPlan.save(); call all your function here
		Test.StopTest();
	}
}

 
Mike ArthurMike Arthur
Also, check your code coverage from UI - go to Setup - Develop - Apex Test Coverage and check the coverage from there.  I have recently found the Dev Console to show 0% coverage when the (real) coverage as shown in UI is 100%.
Saru VadlamudiSaru Vadlamudi
Thank you all ,issues is fixed.previously i have passed the id instead of oppID.
 ApexPages.currentPage().getParameters().put('oppID',Opp.Id);
Saru VadlamudiSaru Vadlamudi
Please help me out how to cover the negative scenarios still my code coverage is less .
I have if-else condition and able to cover only one scenario either positive and negative.
suggest me that how to cover the negative scenarios in test class.
Chandra Sekhar CH N VChandra Sekhar CH N V
@karuturi, I would suggest you to include two different test methods one for positive test case and another for negative. This should cover both you if and else parts of the code.
This was selected as the best answer
Saru VadlamudiSaru Vadlamudi
Hi Sekhar,
I have created two diffrent methods for postive and negative but not working as expected and only negative scenario is excuting.
Please let me know what are can be the possibilities for this failues.
Saru VadlamudiSaru Vadlamudi
​Thakns Sekhar,everything is looks fine now.
Sorry i have done silly mistake while passing the data for positive scenario.