You need to sign in to do that
Don't have an account?
Darlene Blaney
Problem with test class - can't get more than 48% coverage
I am having a hard time getting the test class to fire part of the code. I think the problem is with the owner of the custom object Territory__c, but can't figure out how to resolve it. Any help will be greatly appreciated...
Here is the class:
public class TestScoreReviewClass {
/***************
* When a new ACT or SAT test score is created, if
* the student application decision is Decision Pending
* or Postponed, create a task for the recruiter to
* review the test scores
****************/
Public static void sendTestScoreTask(Test_Score__c[] test_score){
// Select the Contact.Id that ties to the Test_Score__c
Set<string> contactId = new Set<string>();
Set<ID> recID = new Set<ID>();
for (Test_Score__c tz : test_score){
if(tz.Contact__c != NULL) {
contactid.add(tz.Contact__c);
}
}
// Select the latest application tied to the new test score
Map<ID,Application__c> ApplInfo = new Map<ID,Application__c>([select Id, Student__c, Counselor_Id__c
from application__c
where Student__r.Domestic_Or_International__c = 'Domestic'
and Application_Decision__c in ('Decision Pending','Postponed')
and Student__c in :contactid
order by Entry_Term__c desc, Application_Date__c desc
limit 1]);
Date dt = Date.today();
List<Task> followupTasks = new List<Task>();
// Generate a list of tasks to be generated
Map<ID, List<Task> > tsks = New Map<ID, List<Task> >();
List<Task> tsksadd = New List<Task>();
For (Application__c c : ApplInfo.Values() )
{
//system.debug(c.Counselor_Id__c);
recID.add(c.Counselor_Id__c);
if (c.Student__c != null && c.Counselor_Id__c != null)
{
List<Task> taskss = tsks.Get(c.Student__c);
If (taskss == null)
{
Task tasks = new Task(
WhoId = c.Student__c,
OwnerId = c.Counselor_Id__c,
Priority = 'Normal',
Type = 'Email',
ActivityDate = (dt.addDays(1)),
Status = 'Not Started',
Subject = 'Review New Test Score (ACT or SAT)');
followupTasks.add(tasks);
}
}
}
// insert the entire list
if (followupTasks.size() > 0) {
//system.debug(followupTasks.size());
insert followupTasks;
}
}
}
Here is the test class:
@isTest
private class TestScoreReviewTest {
static testMethod void myUnitTest() {
// Test Task
test.startTest();
Profile p = [SELECT Id FROM Profile WHERE Name='SNC Counselor Plus'];
User u1 = new User(Alias = 'standa', Email='saplingstandarduser@testorg.com',
EmailEncodingKey='ISO-8859-1', LastName='Testing', LanguageLocaleKey='en_US',
LocaleSidKey='en_US', ProfileId = p.Id,
TimeZoneSidKey='America/Mexico_City', UserName='saplingstandarduser@testorg.com');
System.runAs(u1){
// Create Territory
Territory__c tr1 = new Territory__c(
Code__c = 'TestTerr',
County__c = 'Brown',
State__c = 'WI',
OwnerId = u1.Id);
insert tr1;
// Create Student
Contact c1 = new Contact(
FirstName = 'Test',
LastName = 'TestStudent',
Domestic_Or_International__c = 'Domestic',
Territory__c = [Select t.Id from Territory__c t where t.Code__c = 'TestTerr' limit 1].Id);
insert c1;
// Create Application
Application__c a1 = new Application__c(
Student__c = [Select c.Id from Contact c where c.LastName = 'TestStudent' limit 1].Id,
Student_Type__c = 'First Time UG',
Full_or_Part_Time__c = 'Full-Time',
Active_Application__c = true,
Application_Date__c = Date.today(),
Application_Status__c = 'Decision Pending');
insert a1;
// Create Test_Score
Test_Score__c t1 = new Test_Score__c(
Contact__c = [Select c.Id from Contact c where c.LastName = 'TestStudent' limit 1].Id,
Test_Date__c = '09/01/2016',
Test_Type__c = 'School',
ACT_Composite__c = 30,
ACT_English__c = 16,
ACT_Math__c = 16,
ACT_Reading__c = 16,
ACT_Science__c = 16
);
insert t1;
test.stopTest();
}
}
}
Thank you for your time.
Darlene Blaney
St Norbert College
(920)403-3953
Here is the class:
public class TestScoreReviewClass {
/***************
* When a new ACT or SAT test score is created, if
* the student application decision is Decision Pending
* or Postponed, create a task for the recruiter to
* review the test scores
****************/
Public static void sendTestScoreTask(Test_Score__c[] test_score){
// Select the Contact.Id that ties to the Test_Score__c
Set<string> contactId = new Set<string>();
Set<ID> recID = new Set<ID>();
for (Test_Score__c tz : test_score){
if(tz.Contact__c != NULL) {
contactid.add(tz.Contact__c);
}
}
// Select the latest application tied to the new test score
Map<ID,Application__c> ApplInfo = new Map<ID,Application__c>([select Id, Student__c, Counselor_Id__c
from application__c
where Student__r.Domestic_Or_International__c = 'Domestic'
and Application_Decision__c in ('Decision Pending','Postponed')
and Student__c in :contactid
order by Entry_Term__c desc, Application_Date__c desc
limit 1]);
Date dt = Date.today();
List<Task> followupTasks = new List<Task>();
// Generate a list of tasks to be generated
Map<ID, List<Task> > tsks = New Map<ID, List<Task> >();
List<Task> tsksadd = New List<Task>();
For (Application__c c : ApplInfo.Values() )
{
//system.debug(c.Counselor_Id__c);
recID.add(c.Counselor_Id__c);
if (c.Student__c != null && c.Counselor_Id__c != null)
{
List<Task> taskss = tsks.Get(c.Student__c);
If (taskss == null)
{
Task tasks = new Task(
WhoId = c.Student__c,
OwnerId = c.Counselor_Id__c,
Priority = 'Normal',
Type = 'Email',
ActivityDate = (dt.addDays(1)),
Status = 'Not Started',
Subject = 'Review New Test Score (ACT or SAT)');
followupTasks.add(tasks);
}
}
}
// insert the entire list
if (followupTasks.size() > 0) {
//system.debug(followupTasks.size());
insert followupTasks;
}
}
}
Here is the test class:
@isTest
private class TestScoreReviewTest {
static testMethod void myUnitTest() {
// Test Task
test.startTest();
Profile p = [SELECT Id FROM Profile WHERE Name='SNC Counselor Plus'];
User u1 = new User(Alias = 'standa', Email='saplingstandarduser@testorg.com',
EmailEncodingKey='ISO-8859-1', LastName='Testing', LanguageLocaleKey='en_US',
LocaleSidKey='en_US', ProfileId = p.Id,
TimeZoneSidKey='America/Mexico_City', UserName='saplingstandarduser@testorg.com');
System.runAs(u1){
// Create Territory
Territory__c tr1 = new Territory__c(
Code__c = 'TestTerr',
County__c = 'Brown',
State__c = 'WI',
OwnerId = u1.Id);
insert tr1;
// Create Student
Contact c1 = new Contact(
FirstName = 'Test',
LastName = 'TestStudent',
Domestic_Or_International__c = 'Domestic',
Territory__c = [Select t.Id from Territory__c t where t.Code__c = 'TestTerr' limit 1].Id);
insert c1;
// Create Application
Application__c a1 = new Application__c(
Student__c = [Select c.Id from Contact c where c.LastName = 'TestStudent' limit 1].Id,
Student_Type__c = 'First Time UG',
Full_or_Part_Time__c = 'Full-Time',
Active_Application__c = true,
Application_Date__c = Date.today(),
Application_Status__c = 'Decision Pending');
insert a1;
// Create Test_Score
Test_Score__c t1 = new Test_Score__c(
Contact__c = [Select c.Id from Contact c where c.LastName = 'TestStudent' limit 1].Id,
Test_Date__c = '09/01/2016',
Test_Type__c = 'School',
ACT_Composite__c = 30,
ACT_English__c = 16,
ACT_Math__c = 16,
ACT_Reading__c = 16,
ACT_Science__c = 16
);
insert t1;
test.stopTest();
}
}
}
Thank you for your time.
Darlene Blaney
St Norbert College
(920)403-3953
You need to set "Application_Decision__c" field value on "Application__c" object as "Decision Pending".
IF "Application_Decision__c" is not formula field then you need to update your application object like below
Application__c a1 = new Application__c( Student__c = c1.id, Student_Type__c = 'First Time UG', Full_or_Part_Time__c = 'Full-Time', Active_Application__c = true, Application_Date__c = Date.today(),
Application_Decision__c = 'Decision Pending',
Application_Status__c = 'Decision Pending');
insert a1;
Let us know if this will help you
All Answers
================================
Test class
================================
@isTest
private class TestScoreReviewTest {
static testMethod void myUnitTest() {
// Test Task
test.startTest();
Profile p = [SELECT Id FROM Profile WHERE Name='SNC Counselor Plus'];
User u1 = new User(Alias = 'standa', Email='saplingstandarduser@testorg.com',
EmailEncodingKey='ISO-8859-1', LastName='Testing', LanguageLocaleKey='en_US',
LocaleSidKey='en_US', ProfileId = p.Id,
TimeZoneSidKey='America/Mexico_City', UserName='saplingstandarduser@testorg.com');
System.runAs(u1){
// Create Territory
Territory__c tr1 = new Territory__c(
Code__c = 'TestTerr',
County__c = 'Brown',
State__c = 'WI',
OwnerId = u1.Id);
insert tr1;
// Create Student
Contact c1 = new Contact(
FirstName = 'Test',
LastName = 'TestStudent',
Domestic_Or_International__c = 'Domestic',
Territory__c = tr1.Id;
insert c1;
// Create Application
Application__c a1 = new Application__c(
Student__c = c1.Id,
Student_Type__c = 'First Time UG',
Full_or_Part_Time__c = 'Full-Time',
Active_Application__c = true,
Application_Date__c = Date.today(),
Application_Status__c = 'Decision Pending');
insert a1;
// Create Test_Score
Test_Score__c t1 = new Test_Score__c(
Contact__c = c1.Id,
Test_Date__c = '09/01/2016',
Test_Type__c = 'School',
ACT_Composite__c = 30,
ACT_English__c = 16,
ACT_Math__c = 16,
ACT_Reading__c = 16,
ACT_Science__c = 16
);
insert t1;
test.stopTest();
}
}
}
=======================================================
Thanks:)
You need to set "Application_Decision__c" field value on "Application__c" object as "Decision Pending".
IF "Application_Decision__c" is not formula field then you need to update your application object like below
Application__c a1 = new Application__c( Student__c = c1.id, Student_Type__c = 'First Time UG', Full_or_Part_Time__c = 'Full-Time', Active_Application__c = true, Application_Date__c = Date.today(),
Application_Decision__c = 'Decision Pending',
Application_Status__c = 'Decision Pending');
insert a1;
Let us know if this will help you