0

this is my trigger

trigger Before_Trigger on eTMFFolder__c ( before insert,before update ) { 

    List<eTMFFolder__c> setupObjectList = [ SELECT id from eTMFFolder__c WHERE isActive__c = true ];

    for ( eTMFFolder__c setup : Trigger.new ) { 
        if(Trigger.isInsert){        
            **if( setup.isActive__c && setupObjectList.size() > 0){**
                setup.addError( 'There Can only one Record Active At the Same Time.' );                 
            }
        } 

        if(Trigger.isUpdate){        
            **if( Trigger.oldMap.get(setup.Id).isActive__c != setup.isActive__c && setup.isActive__c && setupObjectList.size() > 0){**
                setup.addError( 'There Can only one Record Active At the Same Time.' );    
            }           
        }
    }

}

this is my test class of trigger

@isTest

private class Before_TriggerTest{

    public static testMethod void unitTest(){
         eTMFFolder__c obj = new eTMFFolder__c();
         obj.Type__c = 'Dropbox';
         obj.isActive__c = true;
         insert obj;


         List<eTMFFolder__c> setupObjectList = [ SELECT id from eTMFFolder__c WHERE isActive__c = true ];  
         system.assertEquals(setupObjectList.size(),1);       
    }

     public static testMethod void unitTestsecond(){
         list<eTMFFolder__c> eTMFFolder = new list<eTMFFolder__c>();
         eTMFFolder__c obj = new eTMFFolder__c();
         obj.Type__c = 'Dropbox';
         obj.isActive__c = false;
          insert obj;
          obj.isActive__c = true;
          update obj;



         List<eTMFFolder__c> setupObjectList = [ SELECT id from eTMFFolder__c WHERE isActive__c = true ];  
         system.assertEquals(setupObjectList.size(),1); 

    }
}
| |
  • where is bold text ? – Jarvis Feb 19 '16 at 11:53
  • 1
    If you have a different question, you should ask it separately, rather than overwriting an existing question. – martin Feb 21 '17 at 10:37
2

The problem with the first one is that your query is coming up empty here:

List<eTMFFolder__c> setupObjectList = [ SELECT id from eTMFFolder__c WHERE isActive__c = true ];

Your if statement has a condition that setupObjectList.size() > 0, and for there to be eTMFolder__c records in the test environment, you would have to insert them during the test.

One thing you could consider doing is adding a TestSetup method in your test class that you can use to test for duplicated types.

@isTest
private class Before_TriggerTest{

    @TestSetup static void testSetup{
         eTMFFolder__c obj = new eTMFFolder__c();
         obj.Type__c = 'TypeForTestingDuplicate';
         obj.isActive__c = true;
         insert obj;
    }

Then you can add another unit test to your test class where you test that the error is added to the object you try to insert.

    public static @isTest void testDuplicate(){
        eTMFFolder__c obj = new eTMFFolder__c();
        obj.Type__c = 'TypeForTestingDuplicate';
        obj.isActive__c = true;
        Database.SaveResult result = Database.insert(obj);
        system.assertEquals(FALSE,result.isSuccess(),'The save should fail since there is already an active eTMFFolder with the same type.');
    }

Then you could add another test method to test the update failing:

    public static @isTest void testDuplicate(){
        eTMFFolder__c obj = new eTMFFolder__c();
        obj.Type__c = 'TypeForTestingDuplicate';
        obj.isActive__c = FALSE;
        insert obj;

        obj.isActive__c = FALSE;
        Database.SaveResult result = Database.update(obj);
        system.assertEquals(FALSE,result.isSuccess(),'The update should fail since there is already an active eTMFFolder with the same type.');
    }

Then of course you could leave in the tests that you have already written to test that the save is succeeding when there are no active records in the system with the same type.

}
| improve this answer | |
2

You are not checking all possible situations.

public static testMethod void unitTest(){
     eTMFFolder__c obj = new eTMFFolder__c();
     obj.Type__c = 'Dropbox';
     obj.isActive__c = true;
     insert obj;

     eTMFFolder__c obj1 = new eTMFFolder__c();
     obj1.Type__c = 'Dropbox';
     obj1.isActive__c = true;
     try {
         insert obj1;
     }catch (Exception e) {
         // Some asserts might go here
     }


     List<eTMFFolder__c> setupObjectList = [ SELECT id from eTMFFolder__c WHERE isActive__c = true ];  
     system.assertEquals(setupObjectList.size(),1);       
}

Also you might want to add more asserts to your code to test the error.

| improve this answer | |
  • @VipinIndora exactly. That's what you are testing. You want to be able to prove that the trigger working the way that you want it to. – martin Feb 19 '16 at 12:08
  • @OlesMalkov you might want to catch that error that the system is going to throw when you insert obj1. – martin Feb 19 '16 at 12:16
  • Nice, looks good. – martin Feb 19 '16 at 13:06
1

Since yours' is a before trigger, the record you are inserting in the test method unitTest() will not be retrieved in the SOQL query inside the trigger. Hence the size of that list will be zero. You should first create one record with isActive__c set to true and then try to insert another one with same isActive__c to true.

Next, same thing is happening here as well. You should create one record first then try to update a 2nd record with isActive__c to true.

| improve this answer | |

Not the answer you're looking for? Browse other questions tagged or ask your own question.