0

I have a use-case where 3 custom objects are used.

Event (One) -> Meeting (Many) (Master detail)

Meeting (One) - > Attendee (Many) (Lookup)

I have built an Apex class for this.

public class EventManagement{

public string selected{get;set;}
public List<Selectoption> options{get;set;}
public List<Event__c> event{get;set;}
public string meets{get;set;}
public List<selectoption> opt{get;set;}
public List<Meeting__c> meeting{get;set;}
public boolean Test{get;set;}
public boolean renderTable{get;set;}
public boolean renderNew{get;set;}
public boolean imp{get;set;}
public List<AttendeesWrapper> attndWrap{get;set;}
public List<AttendeesWrapper> attndWrap2{get;set;}
public List<AttendeesWrapper> mapWrap{get;set;}
public List<Attendee__c> attList{get;set;}
public Meeting__c meetn {get;set;}
public List<Attendee__c> attList1{get;set;}

public EventManagement(){
    options=new List<selectoption>();
    event=new List<Event__c>();
    event=[select ID,Name from Event__c where   Event_Status__c = :'Approved' ];
    options.add(new selectoption('','----SELECT----'));
        for(Event__c eveList : event){
        options.add(new selectoption(eveList.ID,eveList.Name));
       }
    }


    public void mymeetings(){
    Test=true;
    opt=new List<selectoption>();
    meeting=new List<Meeting__c>();
    meeting=[select ID,Name from Meeting__c where Event__c=:selected];

    opt.add(new selectoption('','----SELECT----'));  
    for(Meeting__c mee : meeting){

        opt.add(new selectoption(mee.ID,mee.Name));
                }
    }


    public void myAttendees(){
            for(Meeting__c mt : meeting){  // Line no 52
                if(mt.name==meets)
                    meetn=mt;
            }

        attList=new List<Attendee__c>();
        attList=[Select Id,Name,Attendee_Email__c,Country__c,Status__c,Attendee__c.Meeting__c from Attendee__c where  Meeting__c=:meets];

         attndWrap = new List<AttendeesWrapper>();

         for(Attendee__c attnd : attList){
             attndWrap.add(new AttendeesWrapper(attnd));
         }

        if(attndWrap.size()>0){
             renderTable = true;


    } 
   }


   public void updateAttendee() {
        List<Attendee__c> atv = new List<Attendee__c>();

        for(AttendeesWrapper aw1 : attndWrap){
            if(aw1.selected1 == true){

            atv.add(aw1.attn1);

           }
        }

        if(atv.size()>0){
            update atv;
        }

        myAttendees();
    }


        public void newAttendee(){
         Attendee__c newA= new Attendee__c();
         attndWrap.add(new AttendeesWrapper(newA));

     }

     public void upsertAttendee(){
          List<Attendee__c> l1 = new List<Attendee__c>();
          Attendee__c atnew = attList[0];
        for(AttendeesWrapper aws: attndWrap){
            if(aws.selected1== true){
            aws.attn1.Meeting__c = atnew.Meeting__c;
                l1.add(aws.attn1);
            }
        }

        if(l1.size()>0){
            upsert l1;

        }
        myAttendees();
     }

     public void deleteAttendee(){
         List<Attendee__c> at = new List<Attendee__c>();

        for(AttendeesWrapper aw2 : attndWrap){
            if(aw2.selected1 == true){

            at.add(aw2.attn1);

           }
        }

        if(at.size()>0){
            delete at;
        }

        myAttendees();
     }

    public void importAttendee(){
        imp=true;
        List<Attendee__c> attList1 = new List<Attendee__c>();
        attList1=[select id,name,Mobile__c,Country__c,Attendee_Email__c, Attendee__c.Meeting__c from Attendee__c where Meeting__c=null];
        attndWrap2 = new List<AttendeesWrapper>();
        for(Attendee__c wa : attList1){
          attndWrap2.add(new AttendeesWrapper(wa));
        }
      } 

      public void mapAttendee(){
           List<Attendee__c> atnds = new List<Attendee__c>();
           if(meets==null){
               renderNew= false;
           }
           Attendee__c atnew = attList[0];
           for(AttendeesWrapper atL : attndWrap2){

                 if(atL.selected1 == true){
                    Attendee__c atn4 = atL.attn1;
                    atn4.Meeting__c=atnew.Meeting__c ;
                    atnds.add(atn4);
                 }
          }
          upsert atnds;
          myAttendees();
          imp=false;
        }




    /* Wrapper Class */

       public class AttendeesWrapper {
         public Attendee__c attn1 {get;set;}
         public boolean selected1{get;set;}

         public AttendeesWrapper(Attendee__c ab) {
             this.attn1=ab;
             this.selected1=false;

         }
       }  
}

This is my test class:

    @isTest
public class TestProject{

     static testmethod void test_Eventcontroller(){

        /* Creating Event Records */


        Event__c evnt = new Event__c(name='TestEvent', Event_Status__c='Approved');
        insert evnt;
        Event__c evnt1 = new Event__c(name='TestEvent1', Event_Status__c='Approved');
        insert evnt1;
        EventManagement aoc= new EventManagement();
        System.assertEquals(2,aoc.event.size());

    }

    static testmethod void test_Meeting(){

       EventManagement aoc= new EventManagement();
     Event__c event = new Event__c(name='TestEvent', Event_Status__c='Approved');
        insert event;

       Event__c evnt1 = new Event__c(name='TestEvent1', Event_Status__c='Approved');
        insert evnt1;

    Meeting__c mtn = new Meeting__c(name='TestMeeting',Event__c=evnt1.id);
        insert mtn;

        aoc.selected = evnt1.id; //the line to add

        aoc.mymeetings();
        //Test.StartTest(); 
        System.assertEquals(1,aoc.meeting.size());
        //Test.StopTest();
    }

    static testmethod void test_Attendees(){
         EventManagement aoc= new EventManagement();
        Event__c evnt1 = new Event__c(name='TestEvent1', Event_Status__c='Approved');
        insert evnt1;
        Meeting__c mtn = new Meeting__c(name='TestMeeting',Event__c=evnt1.id);
        insert mtn;
        Attendee__c att = new Attendee__c(name='Test Attendee',Meeting__c=mtn.id);
        insert att;
        System.debug('mtnmmmmmmmmmmm :'+mtn);
        System.debug('aoccccccccccc :'+aoc);
         aoc.meets = mtn.id; //the line to add
         aoc.mymeetings();
         aoc.myAttendees(); // Line no 56
        //Test.StartTest();
        System.assertEquals(1,aoc.attndWrap.size());
        //Test.StopTest();
    }   
}

Attendees assertion is only failing after modification.

Expected 1. Found 0

1 Answer 1

1

For your test_Meeting method, you forgot to set the selected variable on your controller before calling the mymeetings method.

Here's the right code:

static testmethod void test_Meeting(){

       EventManagement aoc= new EventManagement();
     Event__c event = new Event__c(name='TestEvent', Event_Status__c='Approved');
        insert event;

       Event__c evnt1 = new Event__c(name='TestEvent1', Event_Status__c='Approved');
        insert evnt1;

    Meeting__c mtn = new Meeting__c(name='TestMeeting',Event__c=evnt1.id);
        insert mtn;

        aoc.selected = evnt1.id; //the line to add

        aoc.mymeetings();
        //Test.StartTest(); 
        System.assertEquals(1,aoc.meeting.size());
        //Test.StopTest();
    }

For your third method, you have to insert your records before calling the controller methods. You have also to set the selected and meets variables of the controller.

Here's the correction:

static testmethod void test_Attendees(){
         EventManagement aoc= new EventManagement();
        Event__c evnt1 = new Event__c(name='TestEvent1', Event_Status__c='Approved');
        insert evnt1;
        Meeting__c mtn = new Meeting__c(name='TestMeeting',Event__c=evnt1.id);
        insert mtn;
        Attendee__c att = new Attendee__c(name='Test Attendee',Meeting__c=mtn.id);
        insert att;

         aoc.meets = mtn.id; //the line to add

         aoc.myAttendees();
        //Test.StartTest();
        System.assertEquals(1,aoc.attndWrap.size());
        //Test.StopTest();
    }   
7
  • Thankyou, test_Meeting() is working now, but test_Attendees() is still failing. Assertion error. Expected 1. Found 0 Commented Feb 16, 2017 at 9:33
  • Are you sure you added the line: aoc.meets = mtn.id; before calling the myAttendees method? Commented Feb 16, 2017 at 9:39
  • System.NullPointerException: Attempt to de-reference a null object at this line: aoc.meets = mtn.id; //the line to add Commented Feb 16, 2017 at 9:50
  • Did you copy my whole test method for test_Attendees? Commented Feb 16, 2017 at 9:53
  • Updated the question with latest code Commented Feb 16, 2017 at 9:53

You must log in to answer this question.

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