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
KatherineCKatherineC 

How to write test class for this trigger

I got this trigger with some help, now I tried to run a test got 0 coverage, please help. When a Pint's field Semi-monthly check box is checked, a new pint is generated.

TRIGGER:

trigger SemiMonthly on Pint__c (after update) {
    List<Pint__c> pints = new List<Pint__c>();
   for (Pint__c p : Trigger.new) {
           if (p.Semi_Monthly__c == true ) {
               Pint__c newPint = new Pint__c();
               newPint.Account__c = p.Account__c;                
               pints.add(newPint);
        }
    }
   if(pints.size() > 0) insert pints;
}

TEST

@isTest
public class TestSemiMonthly {
    static testMethod void insertNewPint() {
      
       Pint__c PintToCreate = new Pint__c();
PintToCreate.Account__c = 'ABC';
insert PintToCreate;
}
}
Best Answer chosen by KatherineC
Boris BachovskiBoris Bachovski
OK, first of all, the Account__c field takes in an ID of the record rather than the name. So if you want to use your existing account named "ABC", you will need to query it's ID
Account myAcount = [SELECT Id FROM Account WHERE Name = 'ABC' LIMIT 1];
But you shouldn't use existing data in your test classes. You can do this by adding `@IsTest(SeeAllData=true)` annotation to your test class, but again it's against the best practices.

Better approach is to create a test account record inside your test method and use that as a parent:

@isTest
public class TestSemiMonthly
{
    static testMethod void insertNewPint()
    {
    	// All these records created in your test method are only valid for the tests
    	// and they do not actually get inserted in the database, nor they're available 
    	// outside this context

    	Account account = new Account();
    	account.Name = 'Test Account';
    	insert account;

        Pint__c PintToCreate = new Pint__c();
        PintToCreate.Account__c = account.Id; // HERE we need an ID, not a Name
        insert PintToCreate;
 
        Test.StartTest();
 
        PintToCreate.Semi_Monthly__c = true;
        update PintToCreate;
 
        Test.StopTest();
    }
}


All Answers

Boris BachovskiBoris Bachovski
In your test class you're only inserting a record. Try update the same record having Semi_Monthly__c field to true. So it should look something like this:

@isTest
public class TestSemiMonthly 
{
    static testMethod void insertNewPint() 
    {
	    Pint__c PintToCreate = new Pint__c();
		PintToCreate.Account__c = 'ABC';
		insert PintToCreate;

		Test.StartTest();

		PintToCreate.Semi_Monthly__c = true;
		update PintToCreate;

		Test.StopTest();
	}
}

KatherineCKatherineC
Thanks for the help but it's still 0 coverage, don't know what's missing.
Boris BachovskiBoris Bachovski
Do you have any errors when running the test class?
KatherineCKatherineC
Errors:   System.StringException: Invalid id: ABC

Stack Trace:   Class.TestSemiMonthly.insertNewPint: line7, column 1

But I did have an account named ABC 
Boris BachovskiBoris Bachovski
OK, first of all, the Account__c field takes in an ID of the record rather than the name. So if you want to use your existing account named "ABC", you will need to query it's ID
Account myAcount = [SELECT Id FROM Account WHERE Name = 'ABC' LIMIT 1];
But you shouldn't use existing data in your test classes. You can do this by adding `@IsTest(SeeAllData=true)` annotation to your test class, but again it's against the best practices.

Better approach is to create a test account record inside your test method and use that as a parent:

@isTest
public class TestSemiMonthly
{
    static testMethod void insertNewPint()
    {
    	// All these records created in your test method are only valid for the tests
    	// and they do not actually get inserted in the database, nor they're available 
    	// outside this context

    	Account account = new Account();
    	account.Name = 'Test Account';
    	insert account;

        Pint__c PintToCreate = new Pint__c();
        PintToCreate.Account__c = account.Id; // HERE we need an ID, not a Name
        insert PintToCreate;
 
        Test.StartTest();
 
        PintToCreate.Semi_Monthly__c = true;
        update PintToCreate;
 
        Test.StopTest();
    }
}


This was selected as the best answer
KatherineCKatherineC
Wow, 100% coverage!!! Thanks so much Boris!!