+ Start a Discussion
Stéphanie ParkStéphanie Park 

Need help with code coverage

Hello, I'm trying to update an apex class created by an external developper. I'm not a developper myself, but I manage to change the class as needed. However I am running into a problem with unit test and code coverage. I have no idea how to update the existing test to make it work. Anybody willing to help me out?
Thank you!!

here is my class:
/**
*   @description controller for MarketingPreferences visualforce page.
*
*   @author dch
*   @copyright PARX
*/
public with sharing class MarketingPreferencesController
{
    public UserWrapper user {get; set;}  
    public String language {get;set;}
    public Boolean isFrLanguage
    {
        get
        {
            return ApexPages.currentPage().getParameters().get('language') == 'fr' ? true : false;
        }
        private set;
    }
    private String userId;
    private Boolean isPersonalLinkAccess;
    public String redirectUrl {get; private set;}
    public Boolean shouldRedirect {public get; private set;}
    public MarketingPreferencesController()
    {
        isPersonalLinkAccess = false;
        userId = Apexpages.currentPage().getParameters().get('userId');
        language = Apexpages.currentPage().getParameters().get('language');
        if (userId != null && userId != '')
        {
            if (userId.startsWith('003'))
            {
                user = new UserWrapper([
                    SELECT LastName, FirstName, Account.Name, Web_Message__c, Private_Email__c, Country_PA__c,
                        NL_Confnew__c, NL_News_of_the_Week__c,
                        NL_Globe_l_ctronique__c, Annual_Report__c, NL_CIES_Newsletter__c, NL_Publi_Newsletter__c,
                        GHP_Newsletter__c, NL_ADH_Newsletter__c, NL_PGGC_Newsletter__c, NL_FC_Training_Updates__c, NL_HDC_Newsletter__c, NL_CFD_Newsletter__c, NL_PSIG_Newsletter__c, Jobtitle__c
                    FROM Contact
                    WHERE id = :userId]);
                    
                isPersonalLinkAccess = true;
            } else
            {
                user = new UserWrapper([
                    SELECT LastName, FirstName, Company, Private_Email__c, Country_PA__c,
                        NL_Confnew__c, NL_News_of_the_Week__c,
                        Nl_Globe_electronique__c, Annual_Report__c, NL_CIES_Newsletter__c, NL_Publi_Newsletter__c,
                        GHP_Newsletter__c, NL_ADH_Newsletter__c, NL_PGGC_Newsletter__c, NL_FC_Training_Updates__c, Position__c,NL_HDC_Newsletter__c,NL_CFD_Newsletter__c, NL_PSIG_Newsletter__c
                    FROM Lead
                    WHERE id = :userId]);
                    
                isPersonalLinkAccess = true;
            }
        } else
        {
            user = new UserWrapper();
        }
    }
    
    public class UserWrapper
    {
        public String firstName {get; set;}
        public String lastName {get; set;}
        public String organization {get; set;}
        public String email {get; set;}
        public String countryOfResidence {get; set;}
        public Boolean publicEvents {get; set;}
        public Boolean newsOfTheWeek {get; set;}
        public Boolean globe {get; set;}
        public List<selectOption> countries {get; set;}
        public String selectedCountry {get; set;}
        public List<String> selectedAreasOfInterest {get; set;}
        public Boolean annualReport {get; set;}
        public Boolean publicNewsletter {get; set;}
        public Boolean adhNewsletter {get; set;}
        public Boolean ciesNewsletter {get; set;}
        public Boolean pggcNewsletter {get; set;}
        public Boolean ghpNewsletter {get; set;}
        public Boolean hdcNewsletter {get; set;}
        public Boolean cfdNewsletter {get; set;}
        public Boolean ggcNewsletter {get; set;}
        public Boolean fcuPdates {get; set;}
        public String jobtitle {get; set;}
        
        public UserWrapper(sObject user)
        {
            Boolean isLead =  (String.valueof(user.Id.getSObjectType()) == 'Lead' ? true : false);
            this.countries = SiteUtils.getPicklistValues(new Lead(), 'Country_PA__c');
            this.firstName = String.valueof(user.get('FirstName'));
            this.lastName = String.valueof(user.get('LastName'));
            this.jobtitle = isLead ? String.valueof(user.get('Position__c')) : String.valueof(user.get('Jobtitle__c'));
            this.organization = isLead ? String.valueof(user.get('Company')) : ((Contact)user).Account.Name;
            this.email = String.valueof(user.get('Private_Email__c'));
            this.selectedCountry = String.valueof(user.get('Country_PA__c'));
            
            this.publicEvents = String.valueof(user.get('NL_Confnew__c')) == 'Subscribed' ?
                    true  : false;
            this.NewsOfTheWeek = (String.valueof(user.get('NL_News_of_the_Week__c')) == 'Subscribed' ? true  : false);
            this.globe = isLead ?
                (String.valueof(user.get('NL_Globe_electronique__c')) == 'Subscribed' ?
                    true  :
                    false) :
                (String.valueof(user.get('NL_Globe_l_ctronique__c')) == 'Subscribed' ?
                    true  :
                    false);
            this.annualReport = (String.valueof(user.get('Annual_Report__c')) == 'Subscribed' ? true  : false);
            this.publicNewsletter = (String.valueof(user.get('NL_Publi_Newsletter__c')) == 'Subscribed' ? true  : false);
            this.ghpNewsletter = (String.valueof(user.get('GHP_Newsletter__c')) == 'Subscribed' ? true  : false);
            this.adhNewsletter = (String.valueof(user.get('NL_ADH_Newsletter__c')) == 'Subscribed' ? true  : false);
            this.pggcNewsletter = (String.valueof(user.get('NL_PGGC_Newsletter__c')) == 'Subscribed' ? true  : false);
            this.ciesNewsletter = (String.valueof(user.get('NL_CIES_Newsletter__c')) == 'Subscribed' ? true  : false);
            this.fcuPdates = (String.valueof(user.get('NL_FC_Training_Updates__c')) == 'Subscribed' ? true  : false);
            this.hdcNewsletter = (String.valueof(user.get('NL_HDC_Newsletter__c')) == 'Subscribed' ? true  : false);
            this.cfdNewsletter = (String.valueof(user.get('NL_CFD_Newsletter__c')) == 'Subscribed' ? true  : false);
            this.ggcNewsletter = (String.valueof(user.get('NL_PSIG_Newsletter__c')) == 'Subscribed' ? true  : false);     
        }
        
        public UserWrapper()
        {
            this.countries = SiteUtils.getPicklistValues(new Lead(), 'Country_PA__c');
            this.publicEvents = false;
            this.NewsOfTheWeek = false;
            this.globe = false;
            this.annualReport = false;
            this.publicNewsletter = false;
            this.ghpNewsletter = false;
            this.pggcNewsletter = false;
            this.adhNewsletter = false;
            this.ciesNewsletter = false;
            this.fcuPdates = false;
            this.hdcNewsletter = false;
            this.cfdNewsletter = false;
            this.ggcNewsletter = false;
        }
    }
    
    public PageReference submit()
    {
        Boolean errorMsg = false;
        if (user.lastName == null || user.lastName.trim() == '')
        {
            SiteUtils.addError('> Last Name: You must enter a value');
            errorMsg = true;
            
        }
        if (user.firstName == null || user.firstName.trim() == '')
        {
            SiteUtils.addError('> First Name: You must enter a value');
            errorMsg = true;
        }
        if (user.email == null || user.email.trim() == '')
        {
            SiteUtils.addError('> Email Address: You must enter a value');
            errorMsg = true;
        }
        if (user.organization == null || user.organization.trim() == '')
        {
            user.organization = 'No Account';
        }
        if (!errorMsg)
        {
            List<Contact> contact = [
                    SELECT Account.Name, NL_Confnew__c, NL_News_of_the_Week__c, NL_Globe_l_ctronique__c,
                            Annual_Report__c, NL_Publi_Newsletter__c, GHP_Newsletter__c, NL_CIES_Newsletter__c,
                            NL_FC_Training_Updates__c, NL_PGGC_Newsletter__c, NL_ADH_Newsletter__c, NL_HDC_Newsletter__c,
NL_CFD_Newsletter__c, NL_PSIG_Newsletter__c, Jobtitle__c, Country_PA__c
                    FROM Contact
                    WHERE LastName = :user.lastName
                      AND (Business_email__c = :user.email OR Private_Email__c = :user.email OR IHEID_email__c = :user.email OR Assistant_email__c = :user.email)
                      Limit 1];
                      
            if (!contact.isEmpty())
            {
                
                if (contact[0].Country_PA__c != user.selectedCountry)
                {
                    contact[0].Web_Updated_Country__c= 'The Country have changed form the web to ' + user.selectedCountry;
                }
                
                if (contact[0].Account.Name != user.organization)
                {
                    contact[0].Web_Message__c = 'The entreprise have changed form the web to ' + user.organization;
                }
                
                contact[0].NL_Confnew__c = (isPersonalLinkAccess ? (
                    user.publicEvents ?
                        'Subscribed' : contact[0].NL_Confnew__c != null ? 'Unsubscribed' : '') : (
                    user.publicEvents || contact[0].NL_Confnew__c == 'Subscribed' ?
                        'Subscribed' : ''));
                
                contact[0].NL_News_of_the_Week__c = (isPersonalLinkAccess ? (
                    user.newsOfTheWeek ?
                        'Subscribed' : contact[0].NL_News_of_the_Week__c != null ? 'Unsubscribed' : '') : (
                    user.newsOfTheWeek || contact[0].NL_News_of_the_Week__c == 'Subscribed' ?
                        'Subscribed' : ''));
                        
                contact[0].NL_Globe_l_ctronique__c = (isPersonalLinkAccess ? (
                    user.globe ?
                        'Subscribed' : contact[0].NL_Globe_l_ctronique__c != null ? 'Unsubscribed' : '') : (
                    user.globe || contact[0].NL_Globe_l_ctronique__c == 'Subscribed' ?
                        'Subscribed' : ''));
                
                contact[0].Annual_Report__c = (isPersonalLinkAccess ? (
                    user.annualReport ?
                        'Subscribed' : contact[0].Annual_Report__c != null ? 'Unsubscribed' : '') : (
                    user.annualReport || contact[0].Annual_Report__c == 'Subscribed' ?
                        'Subscribed' : ''));
                
                contact[0].NL_Publi_Newsletter__c = (isPersonalLinkAccess ? (
                    user.publicNewsletter ?
                        'Subscribed' : contact[0].NL_Publi_Newsletter__c != null ? 'Unsubscribed' : '') : (
                    user.publicNewsletter || contact[0].NL_Publi_Newsletter__c == 'Subscribed' ?
                        'Subscribed' : ''));
                
                contact[0].GHP_Newsletter__c = (isPersonalLinkAccess ? (
                    user.ghpNewsletter ?
                        'Subscribed' : contact[0].GHP_Newsletter__c != null ? 'Unsubscribed' : '') : (
                    user.ghpNewsletter || contact[0].GHP_Newsletter__c == 'Subscribed' ?
                        'Subscribed' : ''));
                
                contact[0].NL_CIES_Newsletter__c = (isPersonalLinkAccess ? (
                    user.ciesNewsletter ?
                        'Subscribed' : contact[0].NL_CIES_Newsletter__c != null ? 'Unsubscribed' : '') : (
                    user.ciesNewsletter || contact[0].NL_CIES_Newsletter__c == 'Subscribed' ?
                        'Subscribed' : ''));
                        
                contact[0].NL_FC_Training_Updates__c = (isPersonalLinkAccess ? (
                    user.fcuPdates ?
                        'Subscribed' : contact[0].NL_FC_Training_Updates__c != null ? 'Unsubscribed' : '') : (
                    user.fcuPdates || contact[0].NL_FC_Training_Updates__c == 'Subscribed' ?
                        'Subscribed' : ''));
                        
                contact[0].NL_PGGC_Newsletter__c = (isPersonalLinkAccess ? (
                    user.pggcNewsletter ?
                        'Subscribed' : contact[0].NL_PGGC_Newsletter__c != null ? 'Unsubscribed' : '') : (
                    user.pggcNewsletter || contact[0].NL_PGGC_Newsletter__c == 'Subscribed' ?
                        'Subscribed' : ''));
                
                contact[0].NL_ADH_Newsletter__c = (isPersonalLinkAccess ? (
                    user.ADHNewsletter ?
                        'Subscribed' : contact[0].NL_ADH_Newsletter__c != null ? 'Unsubscribed' : '') : (
                    user.ADHNewsletter || contact[0].NL_ADH_Newsletter__c == 'Subscribed' ?
                        'Subscribed' : ''));
                        
                 contact[0].NL_HDC_Newsletter__c = (isPersonalLinkAccess ? (
                    user.hdcNewsletter ?
                        'Subscribed' : contact[0].NL_HDC_Newsletter__c != null ? 'Unsubscribed' : '') : (
                    user.hdcNewsletter || contact[0].NL_HDC_Newsletter__c == 'Subscribed' ?
                        'Subscribed' : ''));
                  
                  contact[0].NL_CFD_Newsletter__c = (isPersonalLinkAccess ? (
                    user.cfdNewsletter ?
                        'Subscribed' : contact[0].NL_CFD_Newsletter__c != null ? 'Unsubscribed' : '') : (
                    user.cfdNewsletter || contact[0].NL_CFD_Newsletter__c == 'Subscribed' ?
                        'Subscribed' : ''));
                  
                  contact[0].NL_PSIG_Newsletter__c = (isPersonalLinkAccess ? (
                    user.ggcNewsletter ?
                        'Subscribed' : contact[0].NL_PSIG_Newsletter__c != null ? 'Unsubscribed' : '') : (
                    user.ggcNewsletter || contact[0].NL_PSIG_Newsletter__c == 'Subscribed' ?
                        'Subscribed' : ''));
                        
                 //  contact[0].NL_GMC_Newsletter_Form__c = (isPersonalLinkAccess ? (
                 //   user.gmcNewsletter ?
                 //       'Subscribed' : contact[0].NL_GMC_Newsletter__c != null ? 'Unsubscribed' : '') : (
                 //   user.gmcNewsletter || contact[0].NL_GMC_Newsletter__c == 'Subscribed' ?
                 //       'Subscribed' : ''));
                
                //contact[0].Preferred_E_mail__c = 'Private Email';
                //contact[0].Private_Email__c = user.email;
                contact[0].Jobtitle__c = user.jobtitle;
                
                update contact[0];
            } else
            {
                List<Lead> lead = [
                    SELECT Company, Country_PA__c, NL_Confnew__c, NL_News_of_the_Week__c,  
                        NL_Globe_electronique__c, Annual_Report__c,
                        NL_Publi_Newsletter__c, GHP_Newsletter__c, NL_CIES_Newsletter__c, Preferred_E_mail__c,
                        NL_FC_Training_Updates__c, NL_PGGC_Newsletter__c, NL_ADH_Newsletter__c, NL_HDC_Newsletter__c, NL_CFD_Newsletter__c, NL_PSIG_Newsletter__c, Position__c
                    FROM Lead
                    WHERE LastName = :user.lastName
                      AND (Business_Email__c = :user.email OR Private_Email__c = :user.email OR IHEID_Email__c = :user.email)
                      AND isConverted = FALSE
                      Limit 1];
                      
                if (lead.isEmpty())
                {
                    User newLeadOwner = [SELECT ID FROM User WHERE Name = 'Stéphanie Park' AND IsActive = true limit 1];
                    
                     Lead newLead = new Lead();
                        newLead.Country_PA__c = user.selectedCountry;
                        newLead.LastName = user.lastName;
                        newLead.FirstName = user.firstName;
                        newLead.Company = user.organization;
                        newLead.Private_Email__c = user.email;
                        newLead.NL_Confnew__c = (user.publicEvents ? 'Subscribed' : '');
                        newLead.NL_News_of_the_Week__c = (user.NewsOfTheWeek ? 'Subscribed' : '');
                        newLead.NL_Globe_electronique__c = (user.globe ? 'Subscribed' : '');
                        newLead.Annual_Report__c = (user.annualReport ? 'Subscribed' : '');
                        newLead.NL_Publi_Newsletter__c = (user.publicNewsletter ? 'Subscribed' : '');
                        newLead.GHP_Newsletter__c = (user.GHPNewsletter ? 'Subscribed' : '');
                        newLead.NL_PGGC_Newsletter__c = (user.pggcNewsletter ? 'Subscribed' : '');
                        newLead.NL_ADH_Newsletter__c = (user.ADHNewsletter ? 'Subscribed' : '');
                        newLead.NL_CIES_Newsletter__c = (user.CIESNewsletter ? 'Subscribed' : '');
                        newLead.NL_HDC_Newsletter__c = (user.hdcNewsletter ? 'Subscribed' : '');
                        newLead.NL_CFD_Newsletter__c = (user.cfdNewsletter ? 'Subscribed' : '');
                        newLead.NL_PSIG_Newsletter__c = (user.ggcNewsletter ? 'Subscribed' : '');         
                        newLead.Preferred_E_mail__c = 'Private Email';
                        newLead.NL_FC_Training_Updates__c = (user.FCUpdates ? 'Subscribed' : '');
                        newLead.Jobtitle__c = user.jobtitle;
                        newLead.LeadSource = 'Web';
                        newLead.LeadSourceSetail__c= 'Communication Forms';
                        newLead.RecordTypeId = SiteUtils.recordTypes.get('Lead:Generic_Prospect');
                        newLead.OwnerId = newLead.OwnerId = newLeadOwner.Id;
                        insert newLead;
                } else
                {
                    lead[0].Company = user.organization;
                    lead[0].Country_PA__c = user.selectedCountry;
                    
                    System.debug('::::::::::::::lead[0].NL_Confnew__c' + lead[0].NL_Confnew__c);
                    lead[0].NL_Confnew__c = (isPersonalLinkAccess ? (
                        user.publicEvents ?
                            'Subscribed' :
                            lead[0].NL_Confnew__c != null ? 'Unsubscribed' : '') : (
                        user.publicEvents || lead[0].NL_Confnew__c == 'Subscribed' ?
                            'Subscribed' : ''));
                
                    lead[0].NL_News_of_the_Week__c = (isPersonalLinkAccess ? (
                        user.newsOfTheWeek ?
                            'Subscribed' :
                                lead[0].NL_News_of_the_Week__c != null ?
                                'Unsubscribed' : '') : (
                        user.newsOfTheWeek || lead[0].NL_News_of_the_Week__c == 'Subscribed' ?
                            'Subscribed' : ''));
                            
                    lead[0].NL_Globe_electronique__c = (isPersonalLinkAccess ? (
                        user.globe ?
                            'Subscribed' : lead[0].NL_Globe_electronique__c != null ? 'Unsubscribed' : '') : (
                        user.globe || lead[0].NL_Globe_electronique__c == 'Subscribed' ?
                            'Subscribed' : ''));
                    
                    lead[0].Annual_Report__c = (isPersonalLinkAccess ? (
                        user.annualReport ?
                            'Subscribed' : lead[0].Annual_Report__c != null ? 'Unsubscribed' : '') : (
                        user.annualReport || lead[0].Annual_Report__c == 'Subscribed' ?
                            'Subscribed' : ''));
                    
                    lead[0].NL_Publi_Newsletter__c = (isPersonalLinkAccess ? (
                        user.publicNewsletter ?
                            'Subscribed' : lead[0].NL_Publi_Newsletter__c != null ? 'Unsubscribed' : '') : (
                        user.publicNewsletter || lead[0].NL_Publi_Newsletter__c == 'Subscribed' ?
                            'Subscribed' : ''));
                    
                    lead[0].GHP_Newsletter__c = (isPersonalLinkAccess ? (
                        user.ghpNewsletter ?
                            'Subscribed' : lead[0].GHP_Newsletter__c != null ? 'Unsubscribed' :  '') : (
                        user.ghpNewsletter || lead[0].GHP_Newsletter__c == 'Subscribed' ?
                            'Subscribed' : ''));
                    
                    lead[0].NL_CIES_Newsletter__c = (isPersonalLinkAccess ? (
                        user.ciesNewsletter ?
                            'Subscribed' : lead[0].NL_CIES_Newsletter__c != null ? 'Unsubscribed' : '') : (
                        user.ciesNewsletter || lead[0].NL_CIES_Newsletter__c == 'Subscribed' ?
                            'Subscribed' : ''));
                            
                    lead[0].NL_PGGC_Newsletter__c = (isPersonalLinkAccess ? (
                        user.pggcNewsletter ?
                            'Subscribed' : lead[0].NL_PGGC_Newsletter__c != null ? 'Unsubscribed' : '') : (
                        user.pggcNewsletter || lead[0].NL_PGGC_Newsletter__c == 'Subscribed' ?
                            'Subscribed' : ''));
                                    
                    lead[0].NL_FC_Training_Updates__c = (isPersonalLinkAccess ? (
                        user.fcuPdates ?
                            'Subscribed' : lead[0].NL_FC_Training_Updates__c != null ? 'Unsubscribed' : '') : (
                        user.fcuPdates || lead[0].NL_FC_Training_Updates__c == 'Subscribed' ?
                            'Subscribed' : ''));                                
                    //lead[0].Preferred_E_mail__c = 'Private Email';
                    //lead[0].Private_Email__c = user.email;
                    lead[0].Jobtitle__c = user.jobtitle;
                    update lead[0];
                }
            }
        }else
        {
            return null;
        }
        
        if (isFrLanguage)
        {
            shouldRedirect = true;
            redirectUrl = 'http://graduateinstitute.ch/fr/home/about-us/newsletters/NewsletterSubscriptionConfirmation.html';
        }else
        {
            shouldRedirect = true;
            redirectUrl = 'http://graduateinstitute.ch/home/about-us/newsletters/NewsletterSubscriptionConfirmation.html';
        }
        return null;
    }
}




Here is my test
@isTest
private class Test_MarketingPreferencesController {

    private static testMethod void testnewUserRegistration() 
    {
        Pagereference pg = Page.MarketingPreferences;
        pg.getParameters().put('language', 'en');
        MarketingPreferencesController controller = new MarketingPreferencesController();
        
        Test.setCurrentPage(pg);
        controller.user.firstName = 'test user';
        controller.user.lastname = 'test user';
        controller.user.email = 'test@gmail.com';
        controller.user.publicEvents = true;
        
        test.startTest();
        controller.submit();
        test.stopTest();
        
        List<Lead> leads = [
                SELECT Preferred_E_mail__c, Private_Email__c, NL_Confnew__c,NL_HDC_Newsletter__c,NL_CCDP_Newsletter__c,NL_CFD_Newsletter__c,NL_PGGC_Newsletter__c,NL_CIES_Newsletter__c,NL_CTEI_Newsletter__c,NL_Academic_News__c,NL_News_of_the_Week__c 
                FROM Lead 
                WHERE FirstName = 'test user'
                  AND Lastname = 'test user'
                  AND email = 'test@gmail.com'];
        
        System.assertEquals(1, leads.size());
        System.assertEquals('Private Email', leads[0].Preferred_E_mail__c);
        System.assert(leads[0].NL_Confnew__c == 'Subscribed');
        System.assert(leads[0].NL_HDC_Newsletter__c == 'Subscribed');
        System.assert(leads[0].NL_CCDP_Newsletter__c == 'Subscribed');
        System.assert(leads[0].NL_CFD_Newsletter__c == 'Subscribed');
        System.assert(leads[0].NL_PGGC_Newsletter__c == 'Subscribed');
        System.assert(leads[0].NL_CIES_Newsletter__c == 'Subscribed');
        System.assert(leads[0].NL_CTEI_Newsletter__c == 'Subscribed');
        System.assert(leads[0].NL_CCDP_Newsletter__c == 'Subscribed');
        System.assert(leads[0].NL_Academic_News__c == 'Subscribed');
        System.assert(leads[0].NL_News_of_the_Week__c == 'Subscribed');
    
    }
    
    private static testMethod void testExistingUserRegistration() 
    {
        Lead lead = new Lead();
        lead.FirstName = 'Test';
        lead.LastName = 'Test';
        lead.Email = 'test@gmail.com';
        lead.Private_Email__c = 'test@gmail.com';
        lead.Preferred_E_mail__c = 'Private Email';
        lead.CountryOfResidence__c = 'Belarus';
        lead.Company = 'Test Company';
        lead.NL_Confnew__c = 'Subscribed';
        lead.NL_HDC_Newsletter__c = 'Subscribed';
        lead.NL_CCDP_Newsletter__c = 'Subscribed';
        lead.NL_CFD_Newsletter__c = 'Subscribed';
        lead.NL_PGGC_Newsletter__c = 'Subscribed';
        lead.NL_CIES_Newsletter__c = 'Subscribed';
        lead.NL_CTEI_Newsletter__c = 'Subscribed';
        lead.NL_CCDP_Newsletter__c = 'Subscribed';
        lead.NL_Academic_News__c = 'Subscribed';
        lead.NL_News_of_the_Week__c = 'Subscribed';
        insert lead;
        
        Pagereference pg = Page.MarketingPreferences;
        pg.getParameters().put('language', 'fr');
        pg.getParameters().put('userId', lead.Id);
        Test.setCurrentPage(pg);
        MarketingPreferencesController controller = new MarketingPreferencesController();
        
        controller.user.firstName = lead.firstName;
        controller.user.lastname = lead.lastname;
        controller.user.email = lead.Email;
        controller.user.publicEvents = true;
        controller.user.organization = 'New Company';
        
        test.startTest();
        controller.submit();
        test.stopTest();
        
        List<Lead> leads = [
                SELECT Preferred_E_mail__c, Private_Email__c, NL_Confnew__c, Company 
                FROM Lead 
                WHERE Id = :lead.Id];
        
        System.assert(leads[0].NL_Confnew__c == 'Subscribed');
        System.assert(leads[0].NL_HDC_Newsletter__c == 'Subscribed');
        System.assert(leads[0].NL_CCDP_Newsletter__c == 'Subscribed');
        System.assert(leads[0].NL_CFD_Newsletter__c == 'Subscribed');
        System.assert(leads[0].NL_PGGC_Newsletter__c == 'Subscribed');
        System.assert(leads[0].NL_CIES_Newsletter__c == 'Subscribed');
        System.assert(leads[0].NL_CTEI_Newsletter__c == 'Subscribed');
        System.assert(leads[0].NL_CCDP_Newsletter__c == 'Subscribed');
        System.assert(leads[0].NL_Academic_News__c == 'Subscribed');
        System.assert(leads[0].NL_News_of_the_Week__c == 'Subscribed');
        System.assert('New Company' == leads[0].Company);
    }
}


 
Abhishek BansalAbhishek Bansal
Hi Stephanie,

Since your apex class consists of a code that is executing based on the various conditions so it is not possible to increase the code coverage just by having a look at it. We need to run the test class in the org and then in the developer console need to find out which lines are not being covered. Based on that test class will be modified again and code coverage can be increased. If you need me to help you with this test class by running it in your org then you can contact me via gmail or skype:
Gmail: abhibansal2790@gmail.com
Skype: abhishek.bansal2790

Thanks,
Abhishek Bansal.