2

need to cover try and catch blocks

   
public class InsertDynamicSObject {


        public String RecordName { get; set; }

        public String ObjectName { get; set; }

        public InsertDynamicSObject (){

            RecordName = '' ;
            ObjectName = '' ;
        }

        public PageReference save() {

            map allObjs = Schema.getGlobalDescribe();
                //system.debug('--------keys---------->'+allObjs.keyset());
                //system.debug('--------values---------->'+allObjs.values());
                //system.debug('--------values---------->'+allObjs.size());

            set Objkeys = allObjs.keyset();

            if(Objkeys.contains(ObjectName.toLowerCase())){

                try{
                    SObject SObj = Schema.getGlobalDescribe().get(ObjectName).newSObject();
                    SObj.put('Name' , RecordName);
                    //SObj.put('LastName' , RecordName);

                    insert Sobj ;

                    pageReference pg1 = new PageReference('/'+SObj.id);
                    return pg1;
                }

                catch(Exception e){

                    ApexPages.addMessages(e);
                    return null;
                }
            }

            else {

                apexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR, 'Object API name is invalid..'));
                //ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.ERROR,'Error: Invalid Input.');
                //ApexPages.addMessage(myMsg);
            }

            return null;    
        }

    }

test class for the above code covers only 59% of code

@istest
public class InsertDynamicSObject_test {

    static testMethod void PositiveTest1(){
        InsertDynamicSObject IDS = new InsertDynamicSObject();

        string ObjectName = 'Account';
        string RecordName = 'Raju';

        //sObject sObj = Schema.getGlobalDescribe().get(ObjectName).newSObject();
        //sobj.put('name', RecordName);
        //insert sObj;

        //SObject SObj = new account();
        //sObj.put('name', RecordName);
        //try{
        //insert sobj;
        //}
        //catch(){

        //}


        IDS.save();

        PageReference pageRef = Page.InsertDynamicSObject  ; 
        Test.setCurrentPage(pageRef);

    }
}

closed as off-topic by Chris Duncombe, crmprogdev, Adrian Larson, superfell, Mark Pond Sep 15 '15 at 18:25

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions on problems in code you've written must describe the specific problem and include valid code to reproduce it. For help writing short, self-contained syntactically-valid examples, see: SSCCE.org" – crmprogdev, Adrian Larson, Mark Pond
If this question can be reworded to fit the rules in the help center, please edit the question.

  • Have you written a test class for this? – Doug B Sep 14 '15 at 8:21
  • yes i have written please have a look at my code – RaJu Sep 14 '15 at 8:22
  • 2
    You'll find that people will be more willing to spend time helping you with these kinds of problems if you tell them which lines of code aren't covered by your test class (hint: knowing the line number isn't helpful since we can't tell the number when viewing it here, so mark you code sections). – crmprogdev Sep 14 '15 at 14:35
0

you have declared the ObjectName and RecordName variables locally in your test class which is not required.

You need to assign the ObjectName and RecordName properties of the InsertDynamicSObject class in your test class.

InsertDynamicSObject IDS = new InsertDynamicSObject();
IDS.ObjectName = 'Account';
IDS.RecordName = 'Raju';
  • And one more how to cover catch block and for error message – RaJu Sep 14 '15 at 9:01

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