function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Laytro80Laytro80 

Test Method Help

I got a lot of help from people on this one a few months back so thanks to all.  Testing has gone well, I have made some changes and I am ready to push into live.

 

I now need to write a test script and I have no idea how to begin.  Any code snippets / pointers would be helpful. I rarely have to write APEX code but had not choice on this occasion.  Thanks again

 

 

public class Roommanager {
    public void search() {
        // Provide default values in the event that no dates are selected
        if(room.Search_Start_Date__c==null)
            room.Search_Start_Date__c=System.today().toStartOfMonth();        
        if(room.Search_End_Date__c==null)
            room.Search_End_Date__c=System.today().toStartOfMonth();    
        
        // Swap the dates if start is before end
        if(room.Search_Start_Date__c>room.Search_End_Date__c) {
            Date tempDate = room.Search_Start_Date__c;
            room.Search_Start_Date__c = room.Search_End_Date__c;
            room.Search_End_Date__c = tempDate;
        }
        
        // Remove previous search results
        rooms.clear();
        availableRooms.clear();
        
        // Find all the room allocations, add them to the list
        rooms.addAll(
            [SELECT
                Id, Name, Start_Date__c, End_Date__c, Nights__c, Room__c, Room__r.Room_No__c, Room__r.Lodge__c, Reservation__c, Reservation__r.Reservation_Contact__c
            FROM
                Room_Allocation__c r     
            WHERE (
                (Start_Date__c >= :room.Search_Start_Date__c  AND Start_Date__c <= :room.Search_End_Date__c) OR
                (End_Date__c  >= :room.Search_Start_Date__c  AND End_Date__c  <= :room.Search_End_Date__c) OR
                (Start_Date__c <= :room.Search_Start_Date__c  AND End_Date__c  >= :room.Search_End_Date__c))  
            ORDER BY
                Room__r.Lodge__c ASC,Room__r.Room_No__c ASC, Room__r.Lodge__c ASC]);
        
        // Build a list of allocated rooms
        Set<Id> allocatedRooms = new Set<Id>();
        for(Room_Allocation__c roomAlloc:rooms)
            allocatedRooms.add(roomAlloc.Room__c);
        
        // Add the rooms that are still available
        availableRooms.addAll(
            [SELECT
                Id, Name, Room_No__c, Lodge__c, Doubles__c, Singles__c, Style__c, Normal_PAX__c,
                (SELECT
                    Id, Name, Start_Date__c, End_Date__c, Room__c
                FROM
                    Room_Allocations__r)
            FROM
                Room__c
            WHERE
                Id NOT IN :allocatedRooms
            ORDER BY
                Lodge__c ASC, Room_No__c ASC]
        );
    }

    public Room_Allocation__c room;
    
    public List<Room_Allocation__c> rooms { get; set; }
    public List<Room__c> availableRooms { get; set; }
    
    // Constructor initialize memory variables
    public Roommanager(ApexPages.StandardController controller) {
        room=(Room_Allocation__c)controller.getRecord();
        rooms=new List<Room_Allocation__c>();
        availableRooms = new List<Room__c>();
        // Default search.
        search();           
    }
}

 

 

Best Answer chosen by Admin (Salesforce Developers) 
Shashikant SharmaShashikant Sharma

Follow these instructions to writing test class for your code

 

 

@isTest
private class TestRoommanager
    {
       
        public static testmethod void testRoomMagerSerch()
            {
//do data insertion                
//Create instance of Room object and insert it
                //Create instance of Room_Allocation__c and assign all  
values before inserting it
                 Room_Allocation__c roomAllocation = new   
                 Room_Allocation__c();

                insert roomAllocation; 

                //Insert any other object that you used in this class       
	        //Create a new instance of standard controller
                ApexPages.StandardController cntr = new       ApexPages.StandardController(roomAllocation);
 
                 test.startTest();
                 //Create instance of roommanager class
                 Roommanager objClsRoommanager =  new Roommanager(cntr);
objClsRoommanager.search();

//Assert your results
                 test.stopTest();

                       
            }
    } 

 

 

All Answers

Shashikant SharmaShashikant Sharma

Follow these instructions to writing test class for your code

 

 

@isTest
private class TestRoommanager
    {
       
        public static testmethod void testRoomMagerSerch()
            {
//do data insertion                
//Create instance of Room object and insert it
                //Create instance of Room_Allocation__c and assign all  
values before inserting it
                 Room_Allocation__c roomAllocation = new   
                 Room_Allocation__c();

                insert roomAllocation; 

                //Insert any other object that you used in this class       
	        //Create a new instance of standard controller
                ApexPages.StandardController cntr = new       ApexPages.StandardController(roomAllocation);
 
                 test.startTest();
                 //Create instance of roommanager class
                 Roommanager objClsRoommanager =  new Roommanager(cntr);
objClsRoommanager.search();

//Assert your results
                 test.stopTest();

                       
            }
    } 

 

 

This was selected as the best answer
Laytro80Laytro80

Thanks this was just the structure I was looking for.

 

Shashikant SharmaShashikant Sharma

Your welcome Laytro. Please ask if any issue comes in test class creation.