function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Darlene BlaneyDarlene 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
Best Answer chosen by Darlene Blaney
Amit Chaudhary 8Amit Chaudhary 8
Hi Darlene ,

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
 
@isTest
private class TestScoreReviewTest {

    static testMethod void myUnitTest() 
	{        
		test.startTest();
        
			Territory__c tr1 = new Territory__c(
									Code__c = 'TestTerr',
									County__c = 'Brown',
									State__c = 'WI',
									OwnerId = u1.Id);
			insert tr1;
        
			Contact c1 = new Contact(
						FirstName = 'Test',
						LastName = 'TestStudent',
						Domestic_Or_International__c = 'Domestic',
						Territory__c = tr1.id );
			insert c1;    
			
			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;
			
			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();
        
    }
}


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

Siva@51Siva@51
Please Try the below code
================================
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:)
Darlene BlaneyDarlene Blaney
Unfortunately, the changes to the test class did not cause it to fire and it is still at 48%. It has the For (Application__c c : ApplInfo.Values() ) highlighted, but everything in the for loop is red. It's as if it is not detecting the application that it just created in the test class. Any other thoughts? Thank you. Darlene Blaney St Norbert College
Amit Chaudhary 8Amit Chaudhary 8
Hi Darlene ,

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
 
@isTest
private class TestScoreReviewTest {

    static testMethod void myUnitTest() 
	{        
		test.startTest();
        
			Territory__c tr1 = new Territory__c(
									Code__c = 'TestTerr',
									County__c = 'Brown',
									State__c = 'WI',
									OwnerId = u1.Id);
			insert tr1;
        
			Contact c1 = new Contact(
						FirstName = 'Test',
						LastName = 'TestStudent',
						Domestic_Or_International__c = 'Domestic',
						Territory__c = tr1.id );
			insert c1;    
			
			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;
			
			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();
        
    }
}


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
 
This was selected as the best answer
Darlene BlaneyDarlene Blaney
You are awesome!!! It worked great! I am now at 100% tested. Thank you. Darlene Blaney St Norbert College