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
nr123nr123 

Test Code for simple class returning SOQL Query Results

I have a very small class that queries 2 objects and displays resullts on a visualforce page. I am having trouble writing the test for this class. Any suggestions would be greatly appreciated. I am getting 0 coverage right now. This is the class that I am trying to test:

 

public with sharing class ReassignController {

    //query for cases that are unassigned
    public List<Opportunity> getUCases() {
        return [SELECT Level_1_Case_Number__c, Name, Id, Worksite_Core__c, Received_Date_Case__c, CIS_Rep__c 
                FROM Opportunity
                WHERE CIS_Rep__c = null  
                    AND Level_1_Case_Number__c !=null 
                    AND Received_Date_Case__c > :date.parse('1/1/2011') ];
    }

     //query for Amendments that are unassigned
    public List<Amendment__c> getUAmends(){
        return [SELECT Level_1__c, Name, CIS__c, Id,AmendmentInstructions__c, Requested_Date__c 
                FROM Amendment__c 
                WHERE   CIS__c='' 
                AND Completed_Date__c = null 
                AND  DeclinedDate__c=null
                AND Requested_Date__c > :date.parse('1/1/2011')];
    }
   
}

 Thanks.

 

Best Answer chosen by Admin (Salesforce Developers) 
rumehta_123rumehta_123

 

Hi,

 

Please follow the below steps for Test coverage.

  • Insert an Opportunity object with the fields mentioned in your query and also satisfying your WHERE clauses.
  • Insert an Amendment__c object with the fields mentioned in your query and also satisfying your WHERE clauses.
  • Finally, create an instance of your classand call your respective get Methods.
ReassignController objReassignController = new ReassignController();
objReassignController.getUCases();
objReassignController.getUAmends();

 

 

Hope this works for you.

All Answers

rumehta_123rumehta_123

 

Hi,

 

Please follow the below steps for Test coverage.

  • Insert an Opportunity object with the fields mentioned in your query and also satisfying your WHERE clauses.
  • Insert an Amendment__c object with the fields mentioned in your query and also satisfying your WHERE clauses.
  • Finally, create an instance of your classand call your respective get Methods.
ReassignController objReassignController = new ReassignController();
objReassignController.getUCases();
objReassignController.getUAmends();

 

 

Hope this works for you.

This was selected as the best answer
nr123nr123

Thank you! That is what I needed.

 

This is my Test Class with 100% coverage:

 

@isTest private with sharing class testReassignController{

@isTest private static void testAssignRecruiterRegion3() {
    Account newAccount = TestFactory.buildTestAccount(1);
        insert newAccount;
    
   List<Opportunity> newOpportunities = new List<Opportunity>();
    for (Integer i = 0;i < 20; i++) {
        Opportunity newOpportunity = TestFactory.buildTestOpportunity(i,newAccount.Id);
        newOpportunities.add(newOpportunity);
     }   
    insert newOpportunities;
    
   List<Amendment__c> newAmendments = new List<Amendment__c>(); 
   for (Integer i = 0;i<20;i++){
        Amendment__c newAmendment = TestFactory.buildTestAmendment(20,newOpportunities[i].Id);   
        newAmendments.add(newAmendment);
   }
     insert newAmendments; 
  ReassignController objReassignController = new ReassignController();
 
        List<Opportunity> o = objReassignController.getUCases();       
        System.AssertEquals(o[0].CIS_Rep__c, null);

        
        List<Amendment__c> a = objReassignController.getUAmends();     
        System.AssertEquals(a[0].CIS__c, null);
        System.AssertNotEquals(a[0].Requested_date__c, null);
  
  }
}