2

I have 69% coverage on my controller, everything is covered accept the pagination. I need some guidance as I'm pretty new to Apex this is my second controller I've ever written. I will include the code that is related to want isn't getting covered. If someone could help me understand how to get the Next and prev page related code covered in a test class I would greatly appreciate it.

Thanks, crmprogdev, for taking time to assist me. My apologies for failing to submit a good question. I have a VF page designed to be a simplified case submission form that I will use in my communities site. It's along the lines of what is included with the Kokua template from a functionality standpoint; in that as the subject is filled it returns a listing of related possible solution articles. This functionality seem to be working but I'm having issues figuring out how to get test coverage for the related articles listing. also, I don't know if I should actually add articles as a part of my test class or not.

Like I mentioned I'm new to this and I apologize if my questions don't seem very informed. I've gone through the Salesforce Knowledge Developer Guide which was instrumental in getting the article listing on my case submission page. I've also gone through the Online Dev 401 course. I've searched the internet and found things that seem related but I think I'm missing some key concept to pull all the information together. Below is my controller and test class.

Controller:

public with sharing class submitcasecd1controller {

  private Static Final Integer PAGE_NUMBER = 10;

  public Case c { get; set; }

  public String acctNum { get; set; }

  public submitcasecd1controller() {
  String qryString = 'SELECT Id, title, UrlName, 
    LastPublishedDate,LastModifiedById FROM KnowledgeArticleVersion 
       WHERE (PublishStatus = \'online\' and Language = \'en_US\')';
      List<KnowledgeArticleVersion> articleList= 
        Database.query(qryString);
      maxSize = articleList.size() ;

      c = new Case();
      c.Status = 'New';
      c.Priority = 'Medium';
      c.Origin = 'Web';

  }

  Integer currentPage = 1;
  Integer maxSize = 1;

  public boolean getPrevRequired() {
    return currentPage > 1;
  }

  public boolean getNextRequired() {
    return currentPage * PAGE_NUMBER < maxSize;
  }

  public Decimal getCurrentPageNumber() {
    return this.currentPage;
  }

  public PageReference next() {
    if(maxSize > this.currentPage * PAGE_NUMBER) {
      this.currentPage = this.currentPage + 1;
    }
    return null;
  }

  public PageReference previous() {
    if(this.currentPage > 1)
        this.currentPage = this.currentPage - 1;
    return null;
  }

public PageReference submitCase() {
    List<Account> accts = [SELECT Id FROM Account WHERE AccountNumber = :acctNum];
    if (accts.size() != 1) {
        ApexPages.Message msg = new ApexPages.Message(ApexPages.Severity.FATAL, 'Invalid account number');
        ApexPages.addMessage(msg);
        return null;
    } else {
        try {
            c.AccountId = accts.get(0).Id;

            // now look for an associated contact with the same email
            Contact cnt = [SELECT Id FROM Contact WHERE AccountId = :c.AccountId AND Email = :c.SuppliedEmail LIMIT 1];
            if (cnt != null)
                c.ContactId = cnt.Id;

            // Specify DML options to ensure the assignment rules are executed
            Database.DMLOptions dmlOpts = new Database.DMLOptions();
            dmlOpts.assignmentRuleHeader.useDefaultRule = true;
            c.setOptions(dmlOpts);

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

Test Class:

@isTest

public class submitcasecd1contollerTest{

  Public static testMethod void submitcasecd1contollerTest() {

  System.debug('Inserting Account');

        Account tstacct       = new Account();
        tstacct.Name          = 'Acme Test Account';
        tstacct.AccountNumber = '0123456';
        insert tstacct;

  System.debug('Checking for successful Account insert.');
        Account[] ca = [SELECT Id, AccountNumber FROM Account WHERE AccountNumber = '0123456'];


  System.debug('Inserting Contact');

        Contact tstcnt        = new Contact();
        tstcnt.FirstName      = 'Fred';
        tstcnt.LastName       = 'Jones';
        tstcnt.Email          = '[email protected]';
        List<Account> accts1 = [SELECT Id FROM Account WHERE AccountNumber = '0123456'];
        tstcnt.AccountId      = accts1.get(0).Id;
        insert tstcnt;     

  System.debug('Checking for successful Account insert.');
        Contact[] cont = [SELECT Id, AccountID FROM Contact WHERE Email = '[email protected]'];


    PageReference pageRef = page.submitCasecd1;
    Test.setCurrentPage(pageRef);

  System.debug('Positive Results Test');    
    submitcasecd1controller controller = new submitcasecd1controller();

        controller.c.Status              = 'New';
        controller.c.Priority            = 'Medium';
        controller.c.Origin              = 'Web'; 
        controller.c.SuppliedName        = 'Fred Jones';
        controller.c.SuppliedEmail       = '[email protected]';
        controller.c.Subject             = 'Test Case';
        controller.c.Description         = 'This is the description.';  
        controller.acctnum               = '0123456';

        controller.submitCase();

  System.debug('Test the Positive Results');
  case newrec = [SELECT Id FROM Case WHERE Subject = 'Test Case'];
  system.assertNotEquals(null, newrec);

  System.debug('Negative Results Test');       
    submitcasecd1controller controllerneg = new submitcasecd1controller();

        controllerneg.c.Status              = 'New';
        controllerneg.c.Priority            = 'Medium';
        controllerneg.c.Origin              = 'Web'; 
        controllerneg.c.SuppliedName        = 'Fred Jones';
        controllerneg.c.SuppliedEmail       = '[email protected]';
        controllerneg.c.Subject             = 'Test Case';
        controllerneg.c.Description         = 'This is the description.';  
        controllerneg.acctnum               = '0123445'; //This acct Number doesn't exist triggers negative path.

         controllerneg.submitCase();

  System.debug('Negative Results Test, Triggers the catch');    
    submitcasecd1controller controllercatch = new submitcasecd1controller();

        controllercatch.c.Status              = 'New';
        controllercatch.c.Priority            = 'Medium';
        controllercatch.c.Origin              = 'Web'; 
        controllercatch.c.SuppliedName        = 'Fred Jones';
        controllercatch.c.SuppliedEmail       = '[email protected]';//This incorrect email triggers the catch
        controllercatch.c.Subject             = 'Test Case';
        controllercatch.c.Description         = 'This is the description.';  
        controllercatch.acctnum               = '0123456'; 

        controllercatch.submitCase();
    }
}
5
  • 2
    We'll need to see the test class too.
    – Jagular
    Apr 20, 2015 at 14:01
  • 1
    Please take a look at How do I ask a good question. Without your test class to know what you've already done, its not realistic to expect anyone to be able to help you. Please click on edit at the bottom left to update your post with your test class code.
    – crmprogdev
    Apr 20, 2015 at 14:06
  • I did my best to provide useful information, any help is greatly appreciated. @crmprogdev
    – sjc
    Apr 20, 2015 at 15:33
  • Update: I've gotten my test coverage up to 80% so at least I can migrate the code. However, I'd like to achieve 100%. If anyone is willing to point me in the right direction, it would be appreciated.
    – sjc
    Apr 20, 2015 at 20:05
  • 1
    @sjc -- Your testMethod needs more system.asserts, for example after controllercatch.submitCase() - don't be blinded to the 100% code coverage goal - instead, write testmethods that verify your code does what you want so when maintenance is done over the months to follow, your testmethods will provide a regression test basis and your confidence in each deployment goes way up. Good functional testing should lead code coverage. Then, you can look to see where you are missing functional testing
    – cropredy
    Jul 16, 2015 at 22:40

1 Answer 1

1

For the pageReference buttons, you can just call them in your testMethod. For example (using the test controller 'controller' that you set up in your test) controller.next(); will cover the next button with nothing further needed. For testing getPrevRequired & getNextRequired you can used assertEqual and check against whether you test controller should be showing other pages available or not. This will vary based on the query results you check against, but I'm guessing Prev would have to be false and Next would be true in the case of your specific test.

You must log in to answer this question.

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