I am very new to Apex, and trying to learn the ropes. I was hoping if anyone could help me with my Test Class for a custom controller. I have read bunch of documentation, but I am still not able to get %100 coverage in my code. For some reason my test class does not test any of my custom controller queries.
Here is the controller:
public with Sharing class SprintTasksCon{
private String sortOrder = 'Name';
public Story__c s { get;set; }
public Sprint__c sp { get;set; }
public Story_Task__c stt { get;set; }
Date target_date = Date.today();
public List <Story_Task__c> getStoryTasks{get;set;}
public List <Sprint__c> getCurrentSprint{get;set;}
public void filterStoryTask(){
//getStoryTasks = new List<Story_Task__c>();
getStoryTasks.clear();
if(stt.Owner__c !=null && s.Sprint__c!=null){
getStoryTasks = [Select Id, Name, Owner__r.Name, Story__r.Name, Subject__c, Story__r.Sprint__r.Name
FROM Story_Task__c
WHERE Owner__c=:stt.Owner__c AND Story__r.Sprint__c=:s.Sprint__c Limit 100];
}
else if(s.Sprint__c == null){
getStoryTasks = [Select Id, Name, Owner__r.Name, Story__r.Name, Subject__c, Story__r.Sprint__r.Name
FROM Story_Task__c
WHERE Owner__c=:stt.Owner__c Limit 100];
}
else if(stt.Owner__c == null){
getStoryTasks = [Select Id, Name, Owner__r.Name, Story__r.Name, Subject__c, Story__r.Sprint__r.Name
FROM Story_Task__c
WHERE Story__r.Sprint__c=:s.Sprint__c Limit 100];
}
else{
getStoryTasks = [Select Id, Name, Owner__r.Name, Story__r.Name, Subject__c, Story__r.Sprint__r.Name
FROM Story_Task__c Limit 100];
}
if (getStoryTasks.isEmpty())
{
ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.ERROR,'No records found');
ApexPages.addMessage(myMsg);
}
}
public SprintTasksCon(){
stt = new Story_Task__c();
s = new Story__c();
getCurrentSprint = [Select Id, Name, Start_Date__c, End_Date__c, Story_Points__c, Velocity__c
FROM Sprint__c target_date
WHERE Start_Date__c <: target_date AND End_Date__c >: target_date Limit 5];
if (getCurrentSprint.isEmpty())
{
ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.ERROR,'No records found');
ApexPages.addMessage(myMsg);
}
getStoryTasks = new List<Story_Task__c>();
getStoryTasks = [Select Id, Name, Owner__r.Name, Story__r.Name, Subject__c, Story__r.Sprint__r.Name
FROM Story_Task__c
Where Story__r.Sprint__r.Start_Date__c <: target_date AND Story__r.Sprint__r.End_Date__c >: target_date
Limit 100];
}
}
Here is my current Test Class. I get only %75 coverage at this point. The lines I get that are not tested are for the filterStoryTasks method. I think I am trying to test that in my test class, but I guess I am not sure why not.
@isTest
public class SprintTasksConTest {
public static testMethod void testMyController(){
Profile pf = [Select Id from Profile where Name='System Administrator'];
User newUser = new User(
LastName='test123',
IsActive=true,
ProfileId=pf.id,
LanguageLocaleKey='en_US',
EmailEncodingKey='UTF-8',
LocaleSidKey='en_US',
Alias='test1',
TimeZoneSidKey='America/Los_Angeles',
CommunityNickname='test1',
Username=String.valueOf(math.random()) + '[email protected]',
Email='[email protected]');
insert newUser;
Sprint__c testSprint = new Sprint__c();
testSprint.Name ='Test Sprint';
testSprint.Start_Date__c = Date.Today().addDays(4);
testSprint.End_Date__c = Date.Today().addDays(-4);
insert testSprint;
Test.StartTest();
PageReference pageRef = Page.SprintTasks;
pageRef.getParameters().put('stt.Owner__c', String.valueOf(testSprint.id));
pageRef.getParameters().put('s.Sprint__c', String.valueOf(newUser.Name));
Test.setCurrentPage(pageRef);
SprintTasksCon controller = new SprintTasksCon();
controller.filterStoryTask();
//controller = new SprintTasksCon();
//controller = setSprint__c ('test');
Test.StopTest();
}
}