0

I have created a Class called "SalesMarketing" and i am writing the test class called "SalesMarketingTest" although the test class is passing the "SalesMarketing" class has 0% Code Coverage

/*************************************************************************************************************************
Class Name      :   SalesMarketing
Class Desc.     :   Class used for Sales & Marketing Visual Force Page   
Author          :   DM
Description     :   This class is used to populate the Sales & Marketing VF component, displaying  Material Name,Product, 
                    item from Material_Records__c and Quantity from Materials_Junction__c. Records are selected from the 
                    page (using the checkbox) once an quantity is added the user pushes "save" these records will map 
                    back to the "Sales & Marketing" Object with relevant information selected.

***************************************************************************************************************************/


public with sharing class SalesMarketing
{
    public List<Material_Records__c> Materials {get;set;} 
    public List<materialWrapper> materialWrapperList {get;set;} 
    public Materials_Junction__c marketingJunction {get;set;}
    public String searchString {get;set;} 
    public SalesMarketing(ApexPages.StandardController controller)
    //public SalesMarketing()
    {   
        system.debug('++ Inside Constructor ');
        marketingJunction = (Materials_Junction__c) controller.getRecord();
        materialWrapperList = new List<materialWrapper>();
        Materials = [select ID,name,Product__c, Item__c, Active__c from Material_Records__c where Active__c =true limit 10];
        for(Material_Records__c obj : Materials)
        {
            materialWrapper tempObj= new materialWrapper();
            tempObj.recordId = obj.id;
            tempObj.name = obj.name;
            tempObj.product = obj.Product__c;
            tempObj.item = obj.Item__c;
            tempObj.selectB = false;
            materialWrapperList.add(tempObj);
        }
    }

    public Pagereference getProductOrItemData(){
        materialWrapperList.clear();
        system.debug('+++ searchString is '+ searchString); 
        system.debug('+++ Length for searchString is '+ searchString.length());  
        //String searchVal= searchString.trim();
        String searchVal='%'+searchString.trim() +'%';
        system.debug('+++ searchString is '+ searchVal); 

         Materials = [select ID,name,Product__c, Item__c, Active__c from Material_Records__c where Active__c =true and (Product__c like :searchVal OR Item__c like :searchVal)];
        for(Material_Records__c obj : Materials)
        {   
            materialWrapper tempObj= new materialWrapper();
            tempObj.recordId = obj.id;
            tempObj.name = obj.name;
            tempObj.product = obj.Product__c;
            tempObj.item = obj.Item__c;
            tempObj.selectB = false;
            materialWrapperList.add(tempObj);
        }
        system.debug('+++ materialWrapperList is '+ materialWrapperList);
        system.debug('++ materialWrapperList size is '+ materialWrapperList.size());
        return null;
    }

    //save method
    public Pagereference save()
    {
        list<Materials_Junction__c> recordToInsert = new list<Materials_Junction__c>();
        try{
        for(materialWrapper obj : materialWrapperList)
        {
         Materials_Junction__c temp ;
            if(obj.selectB == true)
            {
                temp = new Materials_Junction__c();
                temp.sales_and_marketing__c = marketingJunction.sales_and_marketing__c;
                temp.Material_Records__c= obj.recordId;
                temp.quantity__C = obj.quantity; 
                recordToInsert.add(temp);
            }
            //recordToInsert.add(temp); you are adding element outside the if condition that the reason for save button error
        }
        insert recordToInsert; 
        return new Pagereference('/'+marketingJunction.sales_and_marketing__c);
        }catch(Exception e){
        ApexPages.addMessage(new ApexPages.Message(ApexPages.severity.Error,e.getMessage()));
        return null;
        }       
    }


    public class materialWrapper
    {
        public string recordId {get; set;}
        public string name {get; set;}
        public string product {get; set;}
        public string item {get; set;}
        public Decimal quantity {get; set;}
        public boolean selectB {get; set;}

        public void materialWrapper()
        {
            recordId = '';
            name = '';
            product = '';
            item = '';
            quantity = 0.0;
            selectB = false;
        }
    }
}

THIS IS THE SalesMarketingTest TEST CLASS

@isTest
private class SalesMarketingTest
{
    static testmethod void  SalesMarketing()
    {

        /**
        ***************************************************************************************** 
        *                     This is test data for Creating an Account
        ***************************************************************************************** 
        */  
            Account acct = new Account
                  (Name='Test');
            insert acct;

        /**
        ***************************************************************************************** 
        *                     Querying RT Object for Contact object
        * Sales_Contact__c on Material Request has an Filter Criteria Applied on lookup
        *Filter = Contact Record Type = Schroders Employee
        ***************************************************************************************** 
        */  

        RecordType rt = [select id,developername,Name from RecordType where SobjectType='Contact' and developername='Employee' Limit 1];

        /**
        ***************************************************************************************** 
        *                     This is test data for Creating an Contact 
        *                         Passing Contact RT Id From Query Above
        ***************************************************************************************** 
        */     
            Contact cont = new Contact (
                   AccountId= acct.id,
                   MailingCountry ='United States',
                   recordTypeId=rt.id,
                   LastName ='Test Name');
            insert cont;

        /**
        ***************************************************************************************** 
        *                     This is test data for Creating an Material Request
        ***************************************************************************************** 
        */          

            sales_and_marketing__c MKTRequest = new sales_and_marketing__c(
                   Name = 'Test Name',
                   Sales_Contact__c = Cont.id,
                         //  Delivery_Date__c =   datatype "Date/Time" 
                   Delivery_Date__c = DateTime.newInstance(2011, 11, 18, 3, 3, 3),
                       //Event_Date__c = datatype "Date" 
                   Event_Date__c = Date.newInstance(2017,2,2)
                       );
            insert MKTRequest;

        /**
        ***************************************************************************************** 
        *                     This is test data for Creating an Material Record
        ***************************************************************************************** 
        */
            Material_Records__c MRecords= new Material_Records__c(
                   Active__c = True,
                   Item__c='United States',
                   Product__c='Test Name');
            insert MRecords;

        /**
        ***************************************************************************************** 
        *                          This is test data for Materials_Junction
        ***************************************************************************************** 
        */
            Materials_Junction__c MJunction= new Materials_Junction__c(
                   Material_Records__c =MRecords.id,
                   sales_and_marketing__c= MKTRequest.id);
            insert MJunction;

        /**
        ***************************************************************************************** 
        *Checking that Materials_Junction__c has a record. 
        ***************************************************************************************** 
        */
        MJunction = [
            Select 
            Id, 
            Name, 
            Material_Records__c, 
            sales_and_marketing__c 
                From Materials_Junction__c 
            WHERE sales_and_marketing__r.id =:MKTRequest.Id 
            AND  Material_Records__r.id =:MRecords.id];
        /**
        ***************************************************************************************** 
        *System Assert Statements. Checking that Record has the correct value from insert statements
        ***************************************************************************************** 
        */
        system.assertEquals(MKTRequest.Id, MJunction.sales_and_marketing__c); //Check that Sales& MKT Id is same as MKTRequest.Id
        system.assertEquals(MRecords.Id, MJunction.Material_Records__c ); //Check that MaterialRecordsID is same as MRecords.Id


        /**
        ***************************************************************************************** 
        *Wrapper
        ***************************************************************************************** 
        */

        //Test.StartTest();

        //materialWrapper obj = new materialWrapper();

        SalesMarketing.materialWrapper wrapper= new SalesMarketing.materialWrapper();
        //Test.StopTest();
    }
}

Notcovered1 Notcovered2

closed as off-topic by Eric, Tushar Sharma, Boris Bachovski, Santanu Boral, Mohith Shrivastava Feb 7 '17 at 20:02

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" – Eric, Tushar Sharma, Boris Bachovski, Santanu Boral, Mohith Shrivastava
If this question can be reworded to fit the rules in the help center, please edit the question.

  • You are not calling SalesMarketing class methods from test class. – Reshma Feb 7 '17 at 9:33
0

You were not calling SalesMarketing class methods from within your test class. Try this test class.

@isTest
private class SalesMarketingTest
{
    static testmethod void  SalesMarketing()
    {

        /**
        ***************************************************************************************** 
        *                     This is test data for Creating an Account
        ***************************************************************************************** 
        */  
        Account acct = new Account
              (Name='Test');
        insert acct;

        /**
        ***************************************************************************************** 
        *                     Querying RT Object for Contact object
        * Sales_Contact__c on Material Request has an Filter Criteria Applied on lookup
        *Filter = Contact Record Type = Schroders Employee
        ***************************************************************************************** 
        */  

        RecordType rt = [select id,developername,Name from RecordType where SobjectType='Contact' and developername='Employee' Limit 1];

        /**
        ***************************************************************************************** 
        *                     This is test data for Creating an Contact 
        *                         Passing Contact RT Id From Query Above
        ***************************************************************************************** 
        */     
        Contact cont = new Contact (
               AccountId= acct.id,
               MailingCountry ='United States',
               recordTypeId=rt.id,
               LastName ='Test Name');
        insert cont;

        /**
        ***************************************************************************************** 
        *                     This is test data for Creating an Material Request
        ***************************************************************************************** 
        */          

        sales_and_marketing__c MKTRequest = new sales_and_marketing__c(
               Name = 'Test Name',
               Sales_Contact__c = Cont.id,
                     //  Delivery_Date__c =   datatype "Date/Time" 
               Delivery_Date__c = DateTime.newInstance(2011, 11, 18, 3, 3, 3),
                   //Event_Date__c = datatype "Date" 
               Event_Date__c = Date.newInstance(2017,2,2)
                   );
        insert MKTRequest;

        /**
        ***************************************************************************************** 
        *                     This is test data for Creating an Material Record
        ***************************************************************************************** 
        */
        Material_Records__c MRecords= new Material_Records__c(
               Active__c = True,
               Item__c='United States',
               Product__c='Test Name');
        insert MRecords;

        /**
        ***************************************************************************************** 
        *                          This is test data for Materials_Junction
        ***************************************************************************************** 
        */
        Materials_Junction__c MJunction= new Materials_Junction__c(
               Material_Records__c =MRecords.id,
               sales_and_marketing__c= MKTRequest.id);
        insert MJunction;

        /**
        ***************************************************************************************** 
        *Checking that Materials_Junction__c has a record. 
        ***************************************************************************************** 
        */
        MJunction = [
            Select 
            Id, 
            Name, 
            Material_Records__c, 
            sales_and_marketing__c 
                From Materials_Junction__c 
            WHERE sales_and_marketing__r.id =:MKTRequest.Id 
            AND  Material_Records__r.id =:MRecords.id];
        /**
        ***************************************************************************************** 
        *System Assert Statements. Checking that Record has the correct value from insert statements
        ***************************************************************************************** 
        */
        system.assertEquals(MKTRequest.Id, MJunction.sales_and_marketing__c); //Check that Sales& MKT Id is same as MKTRequest.Id
        system.assertEquals(MRecords.Id, MJunction.Material_Records__c ); //Check that MaterialRecordsID is same as MRecords.Id


        /**
        ***************************************************************************************** 
        *Wrapper
        ***************************************************************************************** 
        */

        Test.StartTest();
        ApexPages.StandardController sc = new ApexPages.StandardController(MJunction);

        SalesMarketing controller = new SalesMarketing(sc);
        controller.searchString = 'Test Name';
        controller.getProductOrItemData();
        System.assertEquals(controller.save().getUrl(), '/'+MKTRequest.id);
        SalesMarketing.materialWrapper wrapper= new SalesMarketing.materialWrapper();
        Test.StopTest();
    }
}

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