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
Wes BarnesWes Barnes 

Test Method Assistance...

All --

 

I am quite new to creating Apex classes and test methods so please be gentle.  I have been coming through posts on the forms and trying to follow some of the cookbook examples but I have not been able to figure htis out.  I have created an extension controller to get some support information that relates to an account.  Here is the code for the controller extension:

 

 

public class ServicesExtension {

    public ServicesExtension(ApexPages.StandardController controller) {

    }

        Public Integer getCaseCount(){
                return [Select Count() from Case where AccountId= :System.currentPageReference().getParameters().get('id')];
        }
        Public Integer getCaseCountOpen(){
                return [Select Count() from Case where AccountId= :System.currentPageReference().getParameters().get('id') and isClosed = false];

        }
        Public Integer getCaseCountOpenThisYear(){
                return [Select Count() from Case where AccountId= :System.currentPageReference().getParameters().get('id') and isClosed = true and CreatedDate = THIS_YEAR];

        }
        Public Case getLastOpenCase(){
                return [Select Id, CaseNumber, Subject, CreatedDate from Case where AccountId= :System.currentPageReference().getParameters().get('id') and isClosed = false Order By CreatedDate desc limit 1];
        }

}

 

 

And I have the following created for the test method:

 

 

@isTest
private class ServicesExtentionTests {

    static testMethod void myUnitTest() {
    
    Account testAccount = new Account();
    testAccount.Name = 'Echo360 Test Company';
    testAccount.Classroom_Management_System__c='Blackboard';
	testAccount.Name='My Test Company';
	testAccount.Type='Customer';
	insert testAccount;
	
	Case testCase = new Case();
	testCase.Subject = 'My Test Case';
	testCase.Status = 'Open';
	testCase.AccountId = testAccount.Account_ID__c;
	insert testCase;

	PageReference pageRef = Page.AccountSummary;
    Test.setCurrentPage(pageRef);
    ApexPages.currentPage().getParameters().put('id', testAccount.id);
    
    ServicesExtension acctController = new ServicesExtension(new ApexPages.StandardController(testAccount));
    Account updatedAccount = [select Classroom_Management_System__c, name, type FROM account where id = :testAccount.Id];
    system.assertEquals('Blackboard', updatedAccount.Classroom_Management_System__c);
    system.assertEquals('My Test Company', updatedAccount.Name);
    system.assertEquals('Customer', updatedAccount.Type);
    
    ServicesExtension caseController = new ServicesExtension(new ApexPages.StandardController(testCase));
    integer caseCount = caseController.getCaseCount();
    Case updatedCase = [Select Subject, Status, AccountID from Case.ID where AccountId = :testCase.Id];
    system.assertEquals('My Test Case', updatedCase.Subject);
    system.assertEquals('Open', updatedCase.Status);
    }	        
}

 

 

The code coverage is only 27% and I think that this is because I am not covering the 3 count queries I have in the controller and the last open case query.  I am not sure how I would test this for these types of queries.

 

Again if this is something that is APEX 101 for many of you I am sorry.

 

Thanks,


Wes

Best Answer chosen by Admin (Salesforce Developers) 
Anand@SAASAnand@SAAS

Don't see your test method called getCaseCountOpen,getCaseCountOpenThisYear,getLastOpenCase methods of your controller. I would explicitly call them and then assert the "Integer" returned with what you are expecting.

All Answers

Anand@SAASAnand@SAAS

Don't see your test method called getCaseCountOpen,getCaseCountOpenThisYear,getLastOpenCase methods of your controller. I would explicitly call them and then assert the "Integer" returned with what you are expecting.

This was selected as the best answer
Wes BarnesWes Barnes

Anand --

Thanks for the reply and pointing me in the right direction.  I misunderstood the requirements of the test methods in that I thought they had to cover the VF page fields as well.  I have re-written my test method to only test for the controller extensionrequirements.

 

 

@isTest
private class ServicesExtentionTests {

    static testMethod void myUnitTest() {
    
    Account testAccount = new Account();
    testAccount.Name = 'Test Company';
	insert testAccount;
	
	PageReference pageRef = Page.AccountSummary;
    Test.setCurrentPage(pageRef);
    ApexPages.currentPage().getParameters().put('id', testAccount.id);
    
    ServicesExtension acctController = new ServicesExtension(new ApexPages.StandardController(testAccount));
    Account updatedAccount = [select Classroom_Management_System__c, name, type FROM account where id = :testAccount.Id];
    
    integer caseCount = [select count() from case where account.id = :testAccount.Id];
    integer openCaseCount = [select count() from case where account.id = :testAccount.ID and isClosed = false];
    integer openCasesThisYear = [Select Count() from Case where AccountId= :testAccount.Id and isClosed = true and CreatedDate = THIS_YEAR];
    caseCount = acctController.getCaseCount();
    openCaseCount = acctController.getCaseCountOpen();
    openCasesThisYear = acctController.getCaseCountOpenThisYear();
    system.assertEquals(0,caseCount);
    system.assertEquals(0,openCaseCount);
    system.assertEquals(0,openCasesThisYear);
    }	        
}

 

 

Thanks again,

 

Wes