-1

Hey any help how to create a test class for codes

public class MycustomSettingController {

    public Org_Info__c orgInfo {get; set;}

    public MycustomSettingController(){
        orgInfo = new Org_Info__c();
    }

public PageReference save(){
    try{
        upsert orgInfo;
        ApexPages.addmessage(new ApexPages.message(ApexPages.severity.CONFIRM,'Created successfully'));
    }
    catch(System.DMLException e){
          ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Invalid Credentials  '));
        return null;
    }

        orgInfo = null;
        ScheduledOrgInfoFetch.ScheduledOrgInfoFetch();
        return null;  
}

this is my test class

@isTest
public class Test {
 @isTest public static void testVFcontroller(){ 

        Org_Info__c orgI = new Org_Info__c();
        orgI.Client_ID__c = 'clID';
        orgI.Client_Secret__c = 'sec';
        orgI.Username__c = 'us';
        orgI.Password__c = 'pw';
        orgI.Sandbox__c = false;

        System.Test.startTest();

        MycustomSettingController controllerRes = new MycustomSettingController();
        controllerRes.orgInfo = orgI;
        controllerRes.save();
          //try{
                 //insert to;
            //}
            //catch(System.DMLException e){

            //System.assert(e.getMessage().contains('Insertion failed'));
           //}
        System.assert(ApexPages.hasMessages(ApexPages.SEVERITY.ERROR));
        System.Test.stopTest();

        Org_Info__c oi = Org_Info__c.getOrgDefaults();
        System.assertEquals('us', oi.Username__c);

    }
}
  • If you're new to unit testing, the trailhead module on Apex testing is a good place to start. – Derek F Sep 3 '18 at 3:16
  • i already created a test class but this part catch(System.DMLException e){ ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Invalid Credentials ')); return null; } got red color – jan Sep 3 '18 at 3:43
  • If you already tried writing unit test and facing any issues, post them so that community can help you on resolving. If you need some guidance on how to write unit test, @DerekF given link will be a really good place to start – highfive Sep 3 '18 at 3:44
  • i already put my test class – jan Sep 3 '18 at 3:49
0

For covering all the test cases in your Class, You need to create test data accordingly. Here, I have Called your method two times, one with correct data, one with missing required field. The first call will cover your try part in the controller and next call will cover catch part with DML Exception.

DmlException:- Any problem with a DML statement, such as an insert statement missing a required field on a record.

Better to create one user in your test data and run the whole context as that user. Here is the overview:-

@isTest
public class Test {
static testMethod void testVFcontroller(){ 

        Org_Info__c orgI = new Org_Info__c();
        orgI.Name='test';
        orgI.sa_nket__Test__c='test';
        Org_Info__c org2 = new Org_Info__c();

        System.Test.startTest();

        MycustomSettingController controllerRes = new MycustomSettingController();
        controllerRes.orgInfo = orgI;
        controllerRes.save();

        controllerRes.orgInfo = org2;
        controllerRes.save();

        System.assert(ApexPages.hasMessages(ApexPages.SEVERITY.ERROR));

        System.Test.stopTest();
    }
}

Testing Best Practices

  1. Make calls to methods using both valid and invalid inputs.
  2. Complete successfully without throwing any exceptions, unless those errors are expected and caught in a try…catch block.
  3. Always handle all exceptions that are caught, instead of merely catching the exceptions.
  4. Use System.assert methods to prove that code behaves properly.
  5. Use the runAs method to test your application in different user contexts.
  6. Exercise bulk trigger functionality—use at least 20 records in your tests.
  7. Use the ORDER BY keywords to ensure that the records are returned in the expected order.
  8. Not assume that record IDs are in sequential order.
  9. Set up test data:
    • Create the necessary data in test classes, so the rest have to rely on data in a particular organization.
    • Create all test data before calling the Test.startTest method.
    • Since tests don't commit, you won't need to delete any data.
  10. Write comments stating not only what is supposed to be tested, but the assumptions the tester made about the data, the expected outcome, and so on.
  11. Test the classes in your application individually. Never test your entire application in a single test.

Reference:- https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_testing_best_practices.htm

|improve this answer|||||
  • it's working now thank you for the help. My test class are working well – jan Sep 3 '18 at 5:25

Not the answer you're looking for? Browse other questions tagged or ask your own question.