You need to sign in to do that
Don't have an account?
Angel Esau Vazquez lagos 5
Help me with a test class, I have some mistakes
I am a beginner in apex. I have written this kind of test, but it makes some mistakes, someone can tell me what is wrong or how to do it better.
I only have 35% coverage
These are my codes.
***** Code Visualforce ****
<apex:page controller="deleteRowsExample" action="{!onloadmethod}"> <!--- action="{!onloadmethod}"---> <apex:form > <apex:pageBlock > <apex:commandbutton value="Add Contact" action="{!addContact}" immediate="true"/> <apex:commandButton value="Save Changes" action="{!saveChanges}"/> <!-- Display some account information --> <apex:pageBlockSection columns="1"> <apex:outputField value="{!accountRec.Name}"/> <apex:outputField value="{!accountRec.OwnerId}"/> </apex:pageBlockSection> <!-- Display the related contacts for the Account --> <apex:pageblocktable value="{!Contacts}" var="con"> <apex:variable value="{!0}" var="cnt"/> <apex:column headervalue="Action"> <apex:commandlink value="Remove" action="{!removeContact}" immediate="true"> <!-- Pass the row number to the controller so that we know which row to remove --> <apex:param name="index" value="{!cnt}"/> </apex:commandlink> <apex:variable var="cnt" value="{!cnt+1}"/> </apex:column> <apex:column headerValue="first Name"> <apex:inputfield value="{!con.FirstName}"/> </apex:column> <apex:column headerValue="Last Name"> <apex:inputfield value="{!con.LastName}"/> </apex:column> </apex:pageblocktable> </apex:pageBlock> </apex:form> </apex:page>
*****controller******
public class deleteRowsExample { public List<Contact> allContactList = new List<Contact>(); public List<Contact> deleteContactList = new List<Contact>(); public Account accountRec {get;set;} //Called when the page loads initially from the "action" method on the apex:page. Populates the Account record and the releated contact list public void onloadmethod(){ String aid = System.currentPageReference().getParameters().get('id'); accountRec = [Select Id,OwnerId,Name,Type,Phone,Website from Account where Id=:aid]; allContactList = [Select Id,FirstName,LastName from Contact where AccountId=:accountRec.Id]; } //Send the list of contacts to the visualforce page public List<Contact> getContacts(){ return allContactList; } //Add a temporary contact to the table. Not saved to the database public void addContact(){ Contact c = new Contact(); allContactList.add(c); } //Remove a contact from the table. public void removeContact(){ Integer indexVal = Integer.valueof(system.currentpagereference().getparameters().get('index')); //If the contact is an existing contact then add it to the list to delete from the database if(allContactList[indexVal - 1].Id != null) deleteContactList.add(allContactList[indexVal - 1]); //Remove the contact from the table allContactList.remove(indexVal - 1); } public void saveChanges(){ for(Integer i=0; i<allContactList.size(); i++) { upsert allContactList; //insert allContactList; } //delete the contacts that were removed if(deleteContactList.size() > 0){ delete deleteContactList; } } }
*****Test Class*****
@isTest private class testdeleteRowsExample_Test { @isTest static void agregacuentas(){ Test.startTest(); Account acc = new Account(); acc.Name = 'TestCuenta'; insert(acc); Contact con = new Contact(); con.AccountId = acc.Id; con.FirstName = 'Pedro'; con.LastName = 'Lopez'; con.MailingPostalCode = '45546'; insert (con); Contact con2 = new Contact(); con2.AccountId = acc.Id; con2.FirstName = 'Rosa'; con2.LastName = 'Croy'; con.MailingPostalCode = '66546'; insert (con2); deleteRowsExample Rows = new deleteRowsExample(); Rows.addContact(); Rows.onloadmethod(); Rows.getContacts(); Rows.saveChanges(); Rows.removeContact(); Test.stopTest(); } }
You have to put some page perameters in test class like this. Use this code it will cover your class code coverage up to 95%.
Regards,
Soyab
All Answers
You have to put some page perameters in test class like this. Use this code it will cover your class code coverage up to 95%.
Regards,
Soyab
Hi @Soyab Hussain.
I will try and later I will tell you how it went, thank you very much!