0

I am writing a test class for custom case creation method. My controller code is given below:

public PageReference submitCase() {
        try{
            if(c.suppliedEmail == '' || c.suppliedEmail == null){
                ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Error, 'Email field cannot be left blank'));  
                return null;
            }
            else{
                c.status = 'New';
                c.origin = 'Web';
                INSERT C;
            }

And my test class code is given below:

@isTest(seeAllData =false)
public class WebToCaseCustomContollerTest{
        static testMethod void caseTestData(){
            Contact cntct = new Contact(lastName = 'Testcontact');
            insert cntct;
            WebToCaseCustomContoller webObj = new WebToCaseCustomContoller();
            webObj.submitCase();  }
}

I am unable to cover the else part here. How to pass suppliedEmail field to my case so that I can cover my else part How do I increase my test coverage?

2
  • 1
    setting value to suppliedEmail is depends on the accessibility of c. So need that code snippet as well. BTW, your objective should be to perform unit test and not just to cover the code
    – highfive
    Commented Oct 23, 2015 at 7:02
  • C is coming from the vf page like this: public Case c { get; set; }
    – happy
    Commented Oct 23, 2015 at 7:07

1 Answer 1

3

You can write something like this:

    WebToCaseCustomContoller webObj = new WebToCaseCustomContoller();
            webObj.c.suppliedEmail = '[email protected]';
webObj.submitCase();

Also if your Case not initialized you need to initialize that first:

Case c = new Case(Name='problem');
1
  • Thanks a lot! Learnt a new thing here.
    – happy
    Commented Oct 23, 2015 at 7:38

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .