I have a simple Apex Class and VFPage that displays records in a print preview which I can then output to Word Doc.
It is now time to push to production. I am new to writing test scripts for code coverage.
I am having a challenge writing the test to achieve the proper code coverage. I am currently at 36%. The specific challenge I am having is testing the custom object records.
Here is the code coverage results:
public class MyVACommunityControllerExtension { private final MyVA_Community__c myva; public List content; // The extension constructor initializes the private member // variable myva by using the getRecord method from the standard // controller. public MyVACommunityControllerExtension(ApexPages.StandardController stdController) { this.myva = (MyVA_Community__c)stdController.getRecord(); } public PageReference records() { return new PageReference('/apex/MyVACommunityDownload'); } public list getContent() { if(content==null) { list cs = new list(); for(Community_Contacts__c c : [Select c.id, c.name, contact__r.name, contact__r.title, c.contact__c, c.role__c, c.myva_community__c from community_contacts__c c WHERE c.myva_community__c = :ApexPages.currentPage().getParameters().get('Id')]){ cs.add(c); } if(cs.size() !=0) { content=cs; } } return content; } }
Here is the Visual Force Page code:
Below is the Test Class:
a. I have intentionally hard-coded ID info to test the test script
b. I have included both @IsTest(SeeAllData=true) and Created my own data
c. I am struggling with how to test the query data results in my Apex Class
d. Thank you in advance for any direction you may be able to provide
/** * An apex page controller that generates WordDoc from data record */ @IsTest public with sharing class TestMyVACommunityControllerExtension { @IsTest public static void testMyVACommunityControllerExtension() { MyVACommunityControllerExtension controller = new MyVACommunityControllerExtension(new ApexPages.StandardController(new MyVA_Community__c())); PageReference pageRef = controller.records(); //Beginning of System Asserts, verify pageRef and URL First System.assertNotEquals(null,pageRef); System.assertEquals('/apex/MyVACommunityPrintView',pageRef.getUrl()); //Verify the pageRef parameters as well Map pageParameters = pageRef.getParameters(); System.assertEquals(0,pageParameters.values().size()); // Create and insert an account Account acc = new Account(name='TestClient'); insert acc; //Create and insert a contact Contact con = new Contact(LastName = 'TestConLN', FirstName = 'TestConFN', Email='[email protected]', AccountId=acc.Id); insert con; //Create and insert a MyVA Community MyVA_Community__c myva = new MyVA_Community__c (Community_Name__c = 'TestCommunity'); insert myva; //Create and insert a Community Contact for MyVA Community Community_Contacts__c comCont = new Community_Contacts__c (MyVA_Community__c = myva.Id, Contact__c= con.Id, role__c ='Director'); //insert comCont; insert comCont; // Perform test Test.startTest(); list cs = controller.getContent(); for(Community_Contacts__c c : [Select c.id, c.name, contact__r.name, contact__r.title, c.contact__c, c.role__c, c.myva_community__c from community_contacts__c c WHERE c.Id !=NULL]){ cs.add(c); } Test.stopTest(); // Verify System.assertEquals(con.Name, myva.Community_Contacts__c); } @isTest static void constructorTest() { // Create and insert an account Account acc = new Account(name='TestClient'); insert acc; //Create and insert a contact Contact con = new Contact(LastName = 'TestConLN', FirstName = 'TestConFN', Email='[email protected]', AccountId=acc.Id); insert con; //Create and insert a MyVA Community MyVA_Community__c myva = new MyVA_Community__c (Community_Name__c = 'TestCommunity'); insert myva; //Create and insert a Community Contact for MyVA Community Community_Contacts__c comCont = new Community_Contacts__c (MyVA_Community__c = myva.Id, Contact__c= con.Id, role__c ='Director'); //insert comCont; insert comCont; // start the test execution context Test.startTest(); // set the test's page to your VF page (or pass in a PageReference) Test.setCurrentPage(Page.MyVACommunityPrintView); // call the constructor MyVACommunityControllerExtension controller = new MyVACommunityControllerExtension(new ApexPages.StandardController(new MyVA_Community__c())); // test action methods on your controller and verify the output with assertions //controller.save(); // stop the test Test.stopTest(); System.assertEquals('Director', comCont.role__c); } }
Here is the ControllerTest that resulted in 100% code coverage:
/** * An apex page controller that generates WordDoc from data record */ @IsTest public with sharing class MyVACommunityControllerExtensionTest { @IsTest public static void MyVACommunityControllerExtensionTest() { MyVACommunityControllerExtension controller = new MyVACommunityControllerExtension(new ApexPages.StandardController(new MyVA_Community__c())); PageReference pageRef = controller.records(); //Beginning of System Asserts, verify pageRef and URL First System.assertNotEquals(null,pageRef); System.assertEquals('/apex/MyVACommunityPrintView',pageRef.getUrl()); //Verify the pageRef parameters as well Map pageParameters = pageRef.getParameters(); System.assertEquals(0,pageParameters.values().size()); // Create and insert an account Account acc = new Account(name='TestClient'); insert acc; //Create and insert a contact Contact con = new Contact(FirstName = 'TestConFN', LastName = 'TestConLN', Email='[email protected]', Title='Director', AccountId=acc.Id); insert con; //Create and insert a MyVA Community MyVA_Community__c myva = new MyVA_Community__c (Community_Name__c = 'TestCommunity', City__c = 'Los Angeles', State__c = 'CA', Name_of_Existing_Group__c = 'ExistingGroup', Georgraphy__c='geography', VA_District__c='Pacific', Milestone_2_Co_Chairs__c='In Progress', Milestone_3_Board_Members__c='In Progress', Milestone_4_1st_Board_Meeting_Scheduled__c = Date.valueOf('2015-10-01'), Milestone_5_1st_public_Forum_Scheduled__c= Date.valueOf('2015-11-20'), Charter__c='Yes', Collaboration__c='collaboration', State_VA_Office_Engagement__c='No', State_DVA_POC__c='theContact', Next_Steps__c='These are next steps'); insert myva; //Create and insert a Community Contact for MyVA Community Community_Contacts__c comCont = new Community_Contacts__c (MyVA_Community__c = myva.Id, Contact__c= con.Id, role__c ='VHA Board Member'); insert comCont; // Verify that the success page displays System.assertEquals('/apex/MyVACommunityPrintView',pageRef.getUrl()); Community_Contacts__c[] content = [Select c.id, c.name, contact__r.name, contact__r.email, contact__r.title, c.contact__c, c.role__c, c.myva_community__c from community_contacts__c c WHERE c.Id !=NULL]; MyVACommunityControllerExtension controllerTest = new MyVACommunityControllerExtension(new ApexPages.StandardController(myva)); pageRef = controllerTest.records(); controllerTest.getContent(); //Test.stopTest(); // Verify System.assertEquals('[email protected]', content[0].contact__r.email); System.assertEquals('VHA Board Member', content[0].role__c); System.assertEquals('TestConFN TestConLN', content[0].contact__r.name); System.assertEquals('Director', content[0].contact__r.title); System.assertEquals(content.size() !=0, true); } }