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
SFDCmack08180939826349907SFDCmack08180939826349907 

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);

    }

}
Best Answer chosen by SFDCmack08180939826349907
pconpcon
Since writing a test class to cover all of the facets of this class is not something that anyone on here will do for you, I can give you some pointers and hopefully get you started.  I would recommend that you do some reading on testing [1] [2] [3] [4] to get a better understanding.  Each of your individual tests should only tests one specific portion of you class (ie a constructor test, sendEmail test, contactSelected test, etc).  You should also have both a postitive (everything works perfectly) and a negative (things are not right) test.

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.
 
public class CaseOwnerOaeController {
    @testVisible private final Case o;

    public CaseOwnerOaeController(ApexPages.StandardController stdController) {
        this.o = (Case) stdController.getRecord();
    }

    public PageReference autoRun() {
        if (this.o == null) {
            return null;
        }

        this.o.OwnerId = '00Gd0000001hJXH';
        update this.o;

        PageReference pageRef = new PageReference('/' + this.o.Id);
        pageRef.setRedirect(true);
        return pageRef;

    }
}
 
public class CaseOwnerOaeControllerTest {
    private static void testMethod 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();

        System.assertEquals('/' + testCase.Id, result.getUrl(), 'Did not get the right URL back');

        testCase = [select OwnerId from Case where Id = :testCase.Id];
        System.assertEquals( '00Gd0000001hJXH', testCase.OwnerId, 'The owner was not updated');
    }
}




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

ManojjenaManojjena
Hi SFDCmock,

Try with below code and don't forget to add necessary fields to insert a case record .
@isTest
public class CaseOwnerOaeControllerTest {
    private static testMethod void unitTest (){
    case testObj=new case();
      testObj.Origin='Phone';
      testObj.Status='New';
       //Add mandatory fields to insert 
	insert testObj;
	 
     ApexPages.currentPage().getParameters().get('id',testObj.Id);
	 ApexPages.standradSetController stdcon=ApexPages.StandradSetController(testObj);
     CaseOwnerOaeController controller = new MyController(stdcon);
     controller.autoRun();
   }
}

Let me know if it helps !
Thanks 
Manoj
 
pconpcon
Since writing a test class to cover all of the facets of this class is not something that anyone on here will do for you, I can give you some pointers and hopefully get you started.  I would recommend that you do some reading on testing [1] [2] [3] [4] to get a better understanding.  Each of your individual tests should only tests one specific portion of you class (ie a constructor test, sendEmail test, contactSelected test, etc).  You should also have both a postitive (everything works perfectly) and a negative (things are not right) test.

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.
 
public class CaseOwnerOaeController {
    @testVisible private final Case o;

    public CaseOwnerOaeController(ApexPages.StandardController stdController) {
        this.o = (Case) stdController.getRecord();
    }

    public PageReference autoRun() {
        if (this.o == null) {
            return null;
        }

        this.o.OwnerId = '00Gd0000001hJXH';
        update this.o;

        PageReference pageRef = new PageReference('/' + this.o.Id);
        pageRef.setRedirect(true);
        return pageRef;

    }
}
 
public class CaseOwnerOaeControllerTest {
    private static void testMethod 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();

        System.assertEquals('/' + testCase.Id, result.getUrl(), 'Did not get the right URL back');

        testCase = [select OwnerId from Case where Id = :testCase.Id];
        System.assertEquals( '00Gd0000001hJXH', testCase.OwnerId, 'The owner was not updated');
    }
}




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/
This was selected as the best answer
SFDCmack08180939826349907SFDCmack08180939826349907
Hi  pcon

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();
       
          }
    }
SFDCmack08180939826349907SFDCmack08180939826349907
Hi  Manoj,
I got an Error: Compile Error: Invalid type: MyController at line 12 column 46
pconpcon
Sorry, line 13 of the test should be PageReference not PageResult
ManojjenaManojjena
Hi try with relace with below line 

CaseOwnerOaeController controller = new CaseOwnerOaeController (stdcon);
SFDCmack08180939826349907SFDCmack08180939826349907
Thanks so much guys