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
Hector DiazHector Diaz 

Create simple Unit test class

Hi 
i have created this simple trigger but i don't know how to do a test class.
please help me, it looks simple trigger and i suppose that is a simple test class.

trigger Block_Unblock_RejectionsTriger on QA_Order_Control__c (before insert,before update) {


for (QA_Order_Control__c objqaoc : Trigger.new)

{
if  (objqaoc.Reject_team__c!=null  )

   {
    objqaoc.Was_Created__c= true;
   
     }
   else {
    objqaoc.Was_Created__c= false;
         }
}


Best Answer chosen by Hector Diaz
Deepak Kumar ShyoranDeepak Kumar Shyoran
To use (seeAllData=true) in test class is not a good practice instead of create your data which will be used in test class

@isTest
private class BlockUnblockRejections_Test {
  static testMethod void unitTest() {
   QA_Order_Control__c  qo = new QA_Order_Control__c(Name='Test Order') ;// And fill other requred field for the object 
   insert qo.
  Test.startTest() ;
     qo.Was_Created__c = true ;
     update qo ;
     system.assertequals(qo.Was_Created__c ,true);
  Test.stopTest() ;
 }
}


All Answers

ramlakhanramlakhan
Hi Dear,
I am also knew to this salesforce codingJust try this code. Hope it will work!

@isTest (SeeAllData=true)
private class Block_Unblock_Rejections_Test {

    static testMethod void myUnitTest() {
    QA_Order_Control__c RtID = [SELECT Reject_team__c FROM QA_Order_Control__c where Reject_team__c !=null limit 1];
      
       QA_Order_Control__c  l = new QA_Order_Control__c( Reject_team__c=RtID.Reject_team__c //put some more fields ,mandatory once compulsory);
        insert l;
    system.assertequals(l.Was_Created__c ,'true');
    }
}
Deepak Kumar ShyoranDeepak Kumar Shyoran
To use (seeAllData=true) in test class is not a good practice instead of create your data which will be used in test class

@isTest
private class BlockUnblockRejections_Test {
  static testMethod void unitTest() {
   QA_Order_Control__c  qo = new QA_Order_Control__c(Name='Test Order') ;// And fill other requred field for the object 
   insert qo.
  Test.startTest() ;
     qo.Was_Created__c = true ;
     update qo ;
     system.assertequals(qo.Was_Created__c ,true);
  Test.stopTest() ;
 }
}


This was selected as the best answer
Pramod_SFDCPramod_SFDC
Hi,

Also to add in to the above Sample code, you cann also include conditions to test the other two conditions, in the test class. Hope you acn create it.So that 100% code is covered.

objqaoc.Was_Created__c= true;
objqaoc.Was_Created__c= false;

Also, you can find the best practices and some sample codde in the below link

>>https://developer.salesforce.com/page/How_to_Write_Good_Unit_Tests


Regards
Pramod
Hector DiazHector Diaz
Hi
thanks so much for your support ramlakhan,Deepak and Pramod.
test class was successful .
best,
Pramod_SFDCPramod_SFDC
Hi,

No worries, great that you were able to complete your test class

Regards
Pramod