You need to sign in to do that
Don't have an account?
SFDCmack08180939826349907
I am lost creating a test class for my apex controller to deploy code to production
Code:
public class CaseOwnerOaeController {
// Constructor - this only really matters if the autoRun function doesn't work right
private final Case o;
public CaseOwnerOaeController(ApexPages.StandardController stdController) {
this.o = (Case)stdController.getRecord();
}
// Code we will invoke on page load.
public PageReference autoRun() {
String theId = ApexPages.currentPage().getParameters().get('id');
if (theId == null) {
// Display the Visualforce page's content if no Id is passed over
return null;
}
for (Case o:[select id,ownerId from Case where id =:theId]) {
o.ownerId='00Gd0000001hJXH';
update o;
}
// Redirect the user back to the original page
PageReference pageRef = new PageReference('/' + theId);
pageRef.setRedirect(true);
return pageRef;
}
}
Test class:
public class CaseOwnerOaeControllerTest {
private static void testMethod test (){
case testObj=new mycase();
// set values in testObj appropriately
CaseOwnerOaeController controller = new MyController();
controller.o=testObj;
controller.autoRun();
System.assertNotEquals(null, testObj.id);
}
}
public class CaseOwnerOaeController {
// Constructor - this only really matters if the autoRun function doesn't work right
private final Case o;
public CaseOwnerOaeController(ApexPages.StandardController stdController) {
this.o = (Case)stdController.getRecord();
}
// Code we will invoke on page load.
public PageReference autoRun() {
String theId = ApexPages.currentPage().getParameters().get('id');
if (theId == null) {
// Display the Visualforce page's content if no Id is passed over
return null;
}
for (Case o:[select id,ownerId from Case where id =:theId]) {
o.ownerId='00Gd0000001hJXH';
update o;
}
// Redirect the user back to the original page
PageReference pageRef = new PageReference('/' + theId);
pageRef.setRedirect(true);
return pageRef;
}
}
Test class:
public class CaseOwnerOaeControllerTest {
private static void testMethod test (){
case testObj=new mycase();
// set values in testObj appropriately
CaseOwnerOaeController controller = new MyController();
controller.o=testObj;
controller.autoRun();
System.assertNotEquals(null, testObj.id);
}
}
I've re-written your controller to better fit how it should be used. There is also a single test. This test will not give you 100% coverage so you will need to write one where the case is not set.
Each test should follow the following structure:
- Setup of test data. This includes creation of any data needed by your class. Account, Contacts etc
- Starting the test. This is calling Test.startTest() to reset the governor limits
- Calling your class / method
- Stopping the test.This is calling Test.stopTest() to reset the governor limits and allow for any async jobs to finish
- Asserting that your changes have worked
- If you have inserted/updated/deleted data, you need to query for the updates
- Run System.assert, System.assertEquals, System.assertNotEquals to verify that you got the correct data back
If you have any specific problems with your tests, feel free to create a new post with the part of the class you are trying to test and your current test method, and you will more likely get a better response then asking for someone to essentially write an entire test class for you.[1] http://www.sfdc99.com/2013/05/14/how-to-write-a-test-class/
[2] http://pcon.github.io/presentations/testing/
[3] http://blog.deadlypenguin.com/blog/2014/07/23/intro-to-apex-auto-converting-leads-in-a-trigger/
[4] http://blog.deadlypenguin.com/blog/testing/strategies/
All Answers
Try with below code and don't forget to add necessary fields to insert a case record .
Let me know if it helps !
Thanks
Manoj
I've re-written your controller to better fit how it should be used. There is also a single test. This test will not give you 100% coverage so you will need to write one where the case is not set.
Each test should follow the following structure:
- Setup of test data. This includes creation of any data needed by your class. Account, Contacts etc
- Starting the test. This is calling Test.startTest() to reset the governor limits
- Calling your class / method
- Stopping the test.This is calling Test.stopTest() to reset the governor limits and allow for any async jobs to finish
- Asserting that your changes have worked
- If you have inserted/updated/deleted data, you need to query for the updates
- Run System.assert, System.assertEquals, System.assertNotEquals to verify that you got the correct data back
If you have any specific problems with your tests, feel free to create a new post with the part of the class you are trying to test and your current test method, and you will more likely get a better response then asking for someone to essentially write an entire test class for you.[1] http://www.sfdc99.com/2013/05/14/how-to-write-a-test-class/
[2] http://pcon.github.io/presentations/testing/
[3] http://blog.deadlypenguin.com/blog/2014/07/23/intro-to-apex-auto-converting-leads-in-a-trigger/
[4] http://blog.deadlypenguin.com/blog/testing/strategies/
I got the error trying to test the method: Error: Compile Error: Invalid type: PageResult at line 10 column 10
public class CaseOwnerOaeControllerTest{
private static testMethod void test(){
Case testCase = new Case(
Subject = 'Test Subject');
insert testCase;
ApexPages.StandardController stdController = new ApexPages.StandardController(testCase);
Test.startTest();
CaseOwnerOaeController controller = new CaseOwnerOaeController(stdController);
PageResult result = controller.autoRun();
Test.stopTest();
}
}
I got an Error: Compile Error: Invalid type: MyController at line 12 column 46
CaseOwnerOaeController controller = new CaseOwnerOaeController (stdcon);