You need to sign in to do that
Don't have an account?
Kunal Purohit 4
How to achieve code coverage for test class?
Hello All, Below is my 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); TeamworkIntegarionUtility.TasksWrapper tasksObj = (TeamworkIntegarionUtility.TasksWrapper)JSON.deserialize(jsonresponse, TeamworkIntegarionUtility.TasksWrapper.class); try { 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 != null && 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; If(!tasksObj.task.assignedUserIds.isEmpty()){ List<TW_Milestone_Task_User__c> milestoneTaskUserObjList = new List<TW_Milestone_Task_User__c>(); for(String userId : tasksObj.task.assignedUserIds) { TW_Milestone_Task_User__c milestoneTaskUserObj = new TW_Milestone_Task_User__c(); milestoneTaskUserObj.TW_User_ID__r = new Contact(UserId__c = userId); milestoneTaskUserObj.TW_Task_ID__c = twTasksobj.Id; milestoneTaskUserObjList.add(milestoneTaskUserObj); } upsert milestoneTaskUserObjList; } } catch(Exception ex) { string sfId = TeamworkIntegarionUtility.getSaleforceIDByExtenalID('TW_Task__c','TaskId__c',tasksObj.task.id); if(!string.isBlank(sfId)) { TW_Task__c task = new TW_Task__c(ID = sfId); task.TW_Sync_status__c = 'Failed'; update task; } TeamworkIntegarionUtility.logError(tasksObj.task.id,ex.getMessage(),'','TW_Task__c', sfId); } } }I have Test class for it. But i am getting code coverage of 74%. Can somebody plz help me to achieve 75% for below Test class
@isTest public class GetTasksFromTeamWorkTest { static testMethod void testPostTask() { //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; TeamworkIntegarionUtility.eventCreator eveCreaObj = new TeamworkIntegarionUtility.eventCreator(); //eveCreaObj.id; eveCreaObj.firstName = 'eventCreator'; eveCreaObj.lastName = 'Test'; TeamworkIntegarionUtility.Project project = new TeamworkIntegarionUtility.Project(); project.Id=projObj.Id; Project.name='Ptest'; Project.description='testing1'; Project.status='complete'; Project.ownerId='001'; Project.companyId='00121'; Project.categoryId='121'; TeamworkIntegarionUtility.Task task = new TeamworkIntegarionUtility.Task(); task.Id=taskObj.Id; task.name='test'; task.description='testing'; task.priority='low'; task.status='open'; task.progress=1; task.estimatedMinutes=10; task.hasCustomFields=true; task.projectId=projObj.ProjectId__c; task.dueDate=String.valueOf(Date.today().addMonths(1)); task.startDate=String.valueOf(Date.today()); TeamworkIntegarionUtility.TaskList taskList = new TeamworkIntegarionUtility.TaskList(); Tasklist.Id=taskListObj.TaskListId__c; Tasklist.name='Sample test'; Tasklist.description='Testing sample'; Tasklist.milestoneId='001'; Tasklist.projectId=projObj.ProjectId__c; task.taskListId = Tasklist.Id; TeamworkIntegarionUtility.TasksWrapper wrapTask = new TeamworkIntegarionUtility.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(); } static testMethod void testPostTask_Error() { //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; TeamworkIntegarionUtility.eventCreator eveCreaObj = new TeamworkIntegarionUtility.eventCreator(); //eveCreaObj.id; eveCreaObj.firstName = 'eventCreator'; eveCreaObj.lastName = 'Test'; TeamworkIntegarionUtility.Project project = new TeamworkIntegarionUtility.Project(); project.Id=projObj.Id; Project.name='Ptest'; Project.description='testing1'; Project.status='complete'; Project.ownerId='001'; Project.companyId='00121'; Project.categoryId='121'; TeamworkIntegarionUtility.Task task = new TeamworkIntegarionUtility.Task(); task.Id=taskObj.Id; task.name='test'; task.description='testing'; task.priority='low'; task.status='open'; task.progress=1; task.estimatedMinutes=10; task.hasCustomFields=true; task.dueDate=String.valueOf(Date.today().addMonths(1)); task.startDate=String.valueOf(Date.today()); TeamworkIntegarionUtility.TasksWrapper wrapTask = new TeamworkIntegarionUtility.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(); } }
https://salesforce.stackexchange.com/questions/244788/how-do-i-write-an-apex-unit-test
Go through the above discussions to write classes.