test coverage not increase 74% is covered. bold text not covered

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);       } } 

Replay

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.

}

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.

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.

Category: trigger Time: 2016-07-31 Views: 57

Related post

iOS development

Android development

Python development

JAVA development

Development language

PHP development

Ruby development

search

Front-end development

Database

development tools

Open Platform

Javascript development

.NET development

cloud computing

server

Copyright (C) avrocks.com, All Rights Reserved.

processed in 0.266 (s). 12 q(s)