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
ShowerHammerShowerHammer 

Need Help with Apex Test Class

I wrote a basic APEX Class that helps create a case from a VisualForce page and need to write a test class for it. I am far from being a developer and managed to get the class uop and running but I do not have the foggiest idea where to start for a test class

public class SubmitCaseController {
    public Case c { get; set; }
    public SubmitCaseController() {
        c = new Case();
    }
    public PageReference submitCase() {
            try {   
                        
                // Specify DML options to ensure the assignment rules are executed
                Database.DMLOptions dmlOpts = new Database.DMLOptions();
                c.Created_By_User__c = UserInfo.getUserId();
                c.OwnerId = '00G30000003El5v';
                c.RecordTypeId = '012n00000004I1r';
                c.Origin = 'Internal Ticketing';
                c.setOptions(dmlOpts);

                // Insert the case
                INSERT c;
                return new PageReference('/apex/thank_you_case_submission');
            } catch (Exception e) {
                ApexPages.addMessages(e);
                return null;
            }
        
    }
 }

 

Best Answer chosen by Admin (Salesforce Developers) 
Abhi_TripathiAbhi_Tripathi

Hey ,

 

Go for this post it will definetly help in your code and as well clear your doubts

 

http://abhithetechknight.blogspot.in/2013/10/salesforce-test-class-basics.html

All Answers

HsethHseth

Checkout this link and let me know if you still have any questions.  Writing a tst class is not a diffcult thing as you already know how to write an apex class.

Apex Test Class

 

Niket SFNiket SF

 

@IsTest(SeeAllData=true)
static testmethod void testNegativeChangecaseOwnerController()
{
test.startTest();
SubmitCaseController obj = new SubmitCaseController();
obj.submitCase();
test.stopTest();
}

 

If it helps you please mark as solved :)

 

 

asish1989asish1989

IsTest(SeeAllData=true)
static testmethod void testNegativeChangecaseOwnerController()
{
test.startTest();
Case testCase = new Case();
testCase.Status = 'New';

testCase.OwnerId = '00G30000003El5v';
testCase.RecordTypeId = '012n00000004I1r';
testCase.Origin = 'Internal Ticketing';
insert testCase;
SubmitCaseController obj = new SubmitCaseController();
obj.submitCase();
test.stopTest();
}

Abhi_TripathiAbhi_Tripathi

Hey ,

 

Go for this post it will definetly help in your code and as well clear your doubts

 

http://abhithetechknight.blogspot.in/2013/10/salesforce-test-class-basics.html

This was selected as the best answer
ShowerHammerShowerHammer

Thank you eveyone for the suggestions, I have marked Abhi's as the bet as it did a great job of explaining how to create future test classes.