I have created an Apex Class and VF page for Popup Alert. I am looking for a test class for the same.

Below is my Apex Class:

public class PopUpAlert
public Contact cont;

  public PopUpAlert(ApexPages.StandardController contact)

   Contact cont = (Contact)contact.getRecord();
    cont=[select id,Customer__c from Contact where Id=: cont.ID];


  public void setAlertVal(){
     cont.Customer__c = true;
     update cont;

This is my Test Class:


public class TestPopUpAlert 
    static testMethod void createContact()


        Contact cont = new Contact();
        cont.FirstName = 'Bob';
        cont.LastName  = 'Test';
        cont.MailingStreet = '298 S. Ringo Street';
        cont.MailingCity = 'Little Rock';
        cont.MailingState = 'AR';
        cont.MailingPostalCode = '72201'; 

        insert cont;
        System.debug('created primary contact'); 

          Apexpages.StandardController sc = new Apexpages.StandardController(cont);
          PopUpAlert pua = new PopUpAlert(sc);
          setAlertVal sav = new setAlertVal(sc);

          cont.Customer__c = True;
          update cont;


I have been able to achieve 50% code coverage thus far but am looking for atleast 100% coverage.

Request your help.


closed as too broad by glls, battery.cord, Boris Bachovski, Dave Humm, Himanshu Aug 4 '18 at 16:27

Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • Dear downvoter, It would be helpful if you can post comments about why you downvoted, This basically brings back to salesforce.meta.stackexchange.com/questions/2706/… – Pranay Jaiswal Aug 2 '18 at 12:13
  • 6
    can you edit you question and let us know what problem are you facing, error message/exception you are getting. What lines are not covered? – Pranay Jaiswal Aug 2 '18 at 12:15
  • 1
    @tanzysfdc, it would really help you to search through SFSE and other resources to read about test methodology. You're making a lot of very common mistakes that you can easily fix if you follow best practices. (1) you need to design your tests to cover all functionality, like the answer below says; (2) you need to make assertions to prove behavior; and (3) you need to create your own data, not use seeAllData=true. – David Reed Aug 2 '18 at 13:53
  • 3
    One reason you're probably receiving downvotes is that you don't seem to have looked at the wealth of resources that are already available to you to show you how to do this. – David Reed Aug 2 '18 at 13:54

In order to maximize your test coverage, you need to design test cases that covers every functionality of your class.

Note that the class PopUpAlert has two methods:

  • The class constructor
  • setAlertVal

Your test is covering only the class constructor part, thus you have 50% of code coverage.

I also suggest that you use the AAA pattern while designing your test code. You can read more about it here: http://wiki.c2.com/?ArrangeActAssert.

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