You need to sign in to do that
Don't have an account?
Kunal Purohit 4
How to get 100% code coverage for Apex Class
Hello All, I have written below Apex Class and its Test Class... Can somebody help me to achieve 100% code coverage?
Apex Class
Apex Class
@RestResource(urlMapping='/Tasks/*') global class GetTasksFromTeamWork { @HttpPost global static void handleTWTaskRequest() { RestRequest req = RestContext.request; String jsonresponse = req.requestBody.toString(); system.debug('jsonresponse'+jsonresponse); TasksWrapper tasksObj = (TasksWrapper)JSON.deserialize(jsonresponse, TasksWrapper.class); TW_Task_List__c taskListRefference = new TW_Task_List__c(TaskListId__c = tasksObj.task.taskListId); // Contact contactRefference = new Contact(UserId__c = tasksObj.task.taskListId); TW_Task__c twTasksobj = new TW_Task__c(); twTasksobj.Name = tasksObj.task.name; twTasksobj.TW_Description__c = tasksObj.task.description; twTasksobj.TW_Due_Date__c = Date.valueOf(tasksObj.task.dueDate); twTasksobj.TW_Progress__c = Decimal.valueOf(tasksObj.task.progress); twTasksobj.TW_Priority__c = tasksObj.task.priority; twTasksobj.TW_Start_Date__c = Date.valueOf(tasksObj.task.startDate); twTasksobj.TW_Estimated_Total_Time__c = Decimal.valueOf(tasksObj.task.estimatedMinutes); twTasksobj.TaskId__c = tasksObj.task.id; twTasksobj.TW_Task_List_Name__r = taskListRefference; if(tasksObj.task.parentId != '0'){ TW_Task__c parentTaskRefference = new TW_Task__c(TaskId__c = tasksObj.task.parentId); twTasksobj.TW_Parent_Task__r = parentTaskRefference; } system.debug('twTasksobj =>'+twTasksobj); upsert twTasksobj TaskId__c; } public class TasksWrapper{ public EventCreator eventCreator; public Project project; public Task task; public TaskList taskList; public List<Tags> users; } public class Project { public String id; public String name; public String description; public String status; public Object startDate; public Object endDate; public List<Tags> tags; public Integer ownerId; public Integer companyId; public Integer categoryId; public String dateCreated; } public class Task { public String id; public String name; public String description; public String priority; public String status; public List<Tags> assignedUserIds; public String parentId; public String taskListId; public String startDate; public String dueDate; public Integer progress; public Integer estimatedMinutes; public List<Tags> tags; public Integer projectId; public String dateCreated; public String dateUpdated; public Boolean hasCustomFields; } public class TaskList { public Integer id; public String name; public String description; public String status; public Integer milestoneId; public Integer projectId; public Object templateId; public List<Tags> tags; } public class EventCreator { public Integer id; public String firstName; public String lastName; public String avatar; } public class Tags { } }Test Class
@isTest public class GetTasksFromTeamWorkTest { static testMethod void testPost() { //Insert Records Account acc = new Account(Name = 'Testing'); insert acc; String Uid = UserInfo.getUserId(); Contact con = new Contact(LastName = 'TestCon',Title = 'Testing ', Phone = '1223456789', Email = 'test123@gmail.com', UserId__c = Uid); insert con; Opportunity OppObj = new Opportunity (Name = 'Opp Test', AccountId = acc.Id, StageName= 'Qualification', CloseDate = system.today()+2); insert OppObj; TW_Project__c projObj = new TW_Project__c(Name = 'Test Project', TW_Opportunity__c = OppObj.Id , ProjectId__c = '438022'); insert projObj; TW_Milestone__c milestoneObj = new TW_Milestone__c(Name = 'Test Milestone', MilestoneId__c = '2233', TW_Project__c = projObj.Id); insert milestoneObj; TW_Task_List__c taskListObj = new TW_Task_List__c(Name = 'Test TaskList', TW_Project__c = projObj.Id, TW_Milestone__c = milestoneObj.Id , TaskListId__c = '2336'); insert taskListObj; TW_Task__c taskObj = new TW_Task__c(Name = 'Test Task', TaskId__c = '1234', TW_Task_List_Name__c = taskListObj.Id); insert taskObj; TW_Time__c timeObj = new TW_Time__c(TW_Project__c = projObj.Id,TW_Who__c = con.Id, TimeId__c = '2336', TW_Task__c = taskObj.Id); insert timeObj; GetTasksFromTeamWork.eventCreator eveCreaObj = new GetTasksFromTeamWork.eventCreator(); //eveCreaObj.id; eveCreaObj.firstName = 'eventCreator'; eveCreaObj.lastName = 'Test'; GetTasksFromTeamWork.Project project = new GetTasksFromTeamWork.Project(); GetTasksFromTeamWork.Task task = new GetTasksFromTeamWork.Task(); GetTasksFromTeamWork.TaskList taskList = new GetTasksFromTeamWork.TaskList(); GetTasksFromTeamWork.TasksWrapper wrapTask = new GetTasksFromTeamWork.TasksWrapper(); wrapTask.eventCreator = eveCreaObj; wrapTask.project = project; wrapTask.task = task; wrapTask.taskList = taskList; String myJSON = JSON.serialize(wrapTask); RestRequest req = new RestRequest(); RestResponse res = new RestResponse(); req.requestURI = '/services/apexrest/GetTasksFromTeamWork/'; req.httpMethod = 'POST'; req.requestBody = Blob.valueof(myJSON); RestContext.request = req; RestContext.response= res; GetTasksFromTeamWork.handleTWTaskRequest(); } }
Greetings!
You need to check which lines are covered and which are not to change the code according to the code.
I would suggest you to follow the best practices which are listed in the below article:https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_testing_best_practices.htm
Please mark it as best answer if it helps you to fix the issue.
Thank you!
Regards,
Shirisha Pathuri