You need to sign in to do that
Don't have an account?
Holly Havelka 10
Need Help Writing My Test Class
Hi All,
I am still learning how to write test class coverage. Working on an org where a controller never had proper test code coverage.
Here is the controller:
I am still learning how to write test class coverage. Working on an org where a controller never had proper test code coverage.
Here is the controller:
public class UpdateResumeController { public List<Organizational_History__c> breakthroughHistoryList {get; set;} public List<Organizational_History__c> educationHistoryList {get; set;} public List<Organizational_History__c> employmentHistoryList {get; set;} public List<SelectOption> sitesOptionList {get; set;} public Boolean editBreakthrough {get; set;} public Boolean editEducation {get; set;} public Boolean editEmployment {get; set;} public Boolean allowEdit {get; set;} public String loggedInContactId; private Id organizationId; private Id breakthroughRecordTypeId; private Id educationRecordTypeId; private Id employmentRecordTypeId; public String getLoggedInContactId() { return loggedInContactId; } public void setLoggedInContactId(String currentContactId) { if (String.isEmpty(loggedInContactId)) { loggedInContactId = currentContactId; breakthroughHistoryList = [SELECT Organization__c, Type__c, Breakthrough_Year__c FROM Organizational_History__c WHERE Breakthrough_Contact__c = :loggedInContactId AND RecordType.Name = 'Breakthrough History' ORDER BY Breakthrough_Year__c ASC]; educationHistoryList = [SELECT School__c, Graduation_Year__c, Field_of_Study__c, Degree__c, City__c, State__c, Country__c FROM Organizational_History__c WHERE Education_Contact__c = :loggedInContactId AND RecordType.Name = 'Educational History' ORDER BY Graduation_Year__c ASC]; employmentHistoryList = [SELECT Employer__c, Role__c, City__c, State__c, Country__c, Start_Year__c, End_Year__c FROM Organizational_History__c WHERE Employment_Contact__c = :loggedInContactId AND RecordType.Name = 'Employment History' ORDER BY Start_Year__c ASC]; organizationId = [SELECT AccountId FROM Contact WHERE Id = :loggedInContactId].AccountId; breakthroughRecordTypeId = [SELECT Id FROM RecordType WHERE Name = 'Breakthrough History'].Id; educationRecordTypeId = [SELECT Id FROM RecordType WHERE Name = 'Educational History'].Id; employmentRecordTypeId = [SELECT Id FROM RecordType WHERE Name = 'Employment History'].Id; } } public UpdateResumeController(){ sitesOptionList = new List<SelectOption>(); List <Account> sitesList = [SELECT Portal_Display_Name__c FROM Account WHERE RecordType.Name = 'Breakthrough Organization' AND Program_Status__c IN ('Active', 'Inactive')]; for (Account acc : sitesList){ if (acc.Portal_Display_Name__c != null){ sitesOptionList.add(new SelectOption(acc.Id, acc.Portal_Display_Name__c)); } } editBreakthrough = false; editEducation = false; editEmployment = false; String contactId = ApexPages.currentPage().getParameters().get('Id'); system.debug(contactId); system.debug(loggedInContactId); allowEdit = loggedInContactId == contactId || contactId == null; } public void enableEditBreakthrough(){ editBreakthrough = true; } public void cancelEditBreakthrough(){ breakthroughHistoryList = [SELECT Organization__c, Type__c, Breakthrough_Year__c FROM Organizational_History__c WHERE Breakthrough_Contact__c = :loggedInContactId AND RecordType.Name = 'Breakthrough History' ORDER BY Breakthrough_Year__c ASC]; editBreakthrough = false; } public void addBreakthroughHistory(){ List<Organizational_History__c> breakthroughHistoryListAux = new List<Organizational_History__c>(); breakthroughHistoryListAux.add(new Organizational_History__c(Breakthrough_Contact__c=loggedInContactId, RecordTypeId = breakthroughRecordTypeId)); breakthroughHistoryListAux.addAll(breakthroughHistoryList); breakthroughHistoryList = breakthroughHistoryListAux; enableEditBreakthrough(); } public void saveBreakthroughHistory(){ upsert breakthroughHistoryList; editBreakthrough = false; } public void enableEditEducation(){ editEducation = true; } public void cancelEditEducation(){ educationHistoryList = [SELECT School__c, Graduation_Year__c, Field_of_Study__c, Degree__c, City__c, State__c, Country__c FROM Organizational_History__c WHERE Education_Contact__c = :loggedInContactId AND RecordType.Name = 'Educational History' ORDER BY Graduation_Year__c ASC]; editEducation = false; } public void addEducationHistory(){ List<Organizational_History__c> educationHistoryListAux = new List<Organizational_History__c>(); educationHistoryListAux.add(new Organizational_History__c(Organization__c = organizationId, Education_Contact__c=loggedInContactId, RecordTypeId = educationRecordTypeId)); educationHistoryListAux.addAll(educationHistoryList); educationHistoryList = educationHistoryListAux; enableEditEducation(); } public void saveEducationHistory(){ upsert educationHistoryList; editEducation = false; } public void enableEditEmployment(){ editEmployment = true; } public void cancelEditEmployment(){ employmentHistoryList = [SELECT Employer__c, Role__c, City__c, Country__c, State__c, Start_Year__c, End_Year__c FROM Organizational_History__c WHERE Employment_Contact__c = :loggedInContactId AND RecordType.Name = 'Employment History' ORDER BY Start_Year__c ASC]; editEmployment = false; } public void addEmploymentHistory(){ List<Organizational_History__c> employmentHistoryListAux = new List<Organizational_History__c>(); employmentHistoryListAux.add(new Organizational_History__c(Organization__c = organizationId, Employment_Contact__c=loggedInContactId, RecordTypeId = employmentRecordTypeId)); employmentHistoryListAux.addAll(employmentHistoryList); employmentHistoryList = employmentHistoryListAux; enableEditEmployment(); } public void saveEmploymentHistory(){ upsert employmentHistoryList; editEmployment = false; } public PageReference deleteRecord(){ String recordId = Apexpages.currentPage().getParameters().get('recordId'); String recordType = Apexpages.currentPage().getParameters().get('recordType'); system.debug(recordType); system.debug(recordId); delete [SELECT Id FROM Organizational_History__c WHERE Id = :recordId]; Integer i = 0; if (recordType == 'breakthrough'){ breakthroughHistoryList = [SELECT Organization__c, Type__c, Breakthrough_Year__c FROM Organizational_History__c WHERE Breakthrough_Contact__c = :loggedInContactId AND RecordType.Name = 'Breakthrough History' ORDER BY Breakthrough_Year__c ASC]; } else if (recordType == 'education'){ educationHistoryList = [SELECT School__c, Graduation_Year__c, Field_of_Study__c, Degree__c, City__c, State__c FROM Organizational_History__c WHERE Education_Contact__c = :loggedInContactId AND RecordType.Name = 'Educational History' ORDER BY Graduation_Year__c ASC]; } else if (recordType == 'employment'){ employmentHistoryList = [SELECT Employer__c, Role__c, City__c, State__c, Country__c, Start_Year__c, End_Year__c FROM Organizational_History__c WHERE Employment_Contact__c = :loggedInContactId AND RecordType.Name = 'Employment History' ORDER BY Start_Year__c ASC]; } return null; } }Here is the test class (not sufficient only at 20%):
@isTest private class UpdateResumeControllerTest { static testMethod void myUnitTest() { //RecordType bo = [SELECT Id FROM RecordType WHERE Name = 'Breakthrough Organization']; //RecordType alumni = [SELECT Id FROM RecordType WHERE Name = 'Student Alumni']; Account testAcc = AccountUtils.createBreakthroughOrgAccount('Breakthrough Alumni Test', true); testAcc.Program_Status__c='Inactive'; update testAcc; //insert testAcc; Contact testContact = ContactUtils.createAlumniContact('MyNew', 'Alumnus1', 'myalumnus1@org.test', testAcc.Id, true); //insert testContact; //Profile testProf = [SELECT Id FROM Profile WHERE Name='Portal Per Login User']; User testUser = UserUtils.createAlumniCommunityUser(testContact, false, true); //insert testUser; Test.startTest(); system.runAs(testUser){ UpdateResumeController urc = new UpdateResumeController(); urc.enableEditBreakthrough(); system.assert(urc.editBreakthrough); } Test.stopTest(); } }
Replace Lines (24 - 41) with the above.
Try now.
All Answers
uncomment the line15 and Line 20.
Let me know if it works.
I put in your suggestions, see below: But it will not let me save due to this error:
Error: Compile Error: Invalid identifier 'urc.deleteRecord'. Apex identifiers must start with an ASCII letter (a-z or A-Z) followed by any number of ASCII letters (a-z or A-Z), digits (0 - 9), '$', '_', or unicode characters from U+0080 to U+FFFE. at line 41 column 28
Any thoughts?
and check the code coverage. Let me know, How much code it is covering.
Class.UpdateResumeController.saveBreakthroughHistory: line 66, column 1
Class.UpdateResumeControllerTest.myUnitTest: line 32, column 1
I need the name of it, to write a snippet of code for your test class.
Replace Lines (24 - 41) with the above.
Try now.
Then we may cross the 75%. Try it.
Please do not forget to mark this thread as SOLVED and answer as the BEST ANSWER if it helps address your issue.
Best,
Nithesh
So I did get coverage to 75% with the below test class, but I am wondering if you could answer why the saveBreakthroughHistory; or even the urc.setLoggedInContactId(testContact.Id); lines are not working? Trying to get better at writing test code coverage.
The reasons can be many, As i see some commented code, they might be effecting these Lines i guess. Honestly, It is kinda hard to debug the Controller code (being it so big) and I dont have clear understanding on your config either. I cant debug it on my dev org, since i dont have your config.
You should consult your dev team about that. since they have better understanding on functionality and configuration of your Org. For last time, Try uncommenting some code, and check if the errors Still persist. Sorry for not being able to get you past 80%.
Please do not forget to mark this thread as SOLVED and answer as the BEST ANSWER if it helps address your issue.
Best,
Nithesh