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: 30

Related post

  • Make notes next to a piece of text (not margin notes) I'm not sure what they're called (image included) 2014-12-19

    EDIT - I'm not sure what these are called but how would one go about doing something like this in LaTeX : Cheers EDIT #1 This partially works - not very well though \documentclass{book} \usepackage{amsmath} \begin{document} \[ \left. \begin{minipage}

  • Test coverage for the "Continue" function 2014-06-10

    I have a trigger that I only have 92% test coverage for and when I review the lines not covered using the Developer Console, it is only the two lines "continue;". How do I incorporate these into my test class so I can get the highest coverage po

  • Trigger Test Class code coverage not increase over 29 2015-07-07

    I have written the trigger below trigger CreditrequestTrigger on Credit_Request__c (after update) { String body = ''; String subject = ''; List<User> Userlist = new List<User>([SELECT Name, Email FROM User WHERE User_License__c = 'Salesforce'

  • Writing a test method to increase code coverage 2014-10-23

    I have been trying for about a week to figure out how to write a test method that will increase my code coverage and cannot figure it out. This is the code I have. public class ConvertToVolunteerJob { private Lead lead; private Case newCase; private

  • How to increase test coverage when Ids are hard coded in the trigger? 2015-07-17

    I have a trigger on Opportunity which runs when the Opportunity is closed as Won. The trigger checks if the Opportunity line item has a specific product(Id is hard coded in the code) and then does sequence of events. I am trying to increase the test

  • Is it appropriate to not follow the O/C principle if you have unit test coverage? 2016-02-20

    I find it more convenient to 'modify'existing classes rather than extending it. Thus, I violate the open closed principle of 'not modifying the compiled and tested source code, instead extending the functionality'. I feel comfortable in modifying the

  • Why can I not deploy to a sandbox without test coverage? 2014-09-26

    I have a directory of apex classes, checked into version control, that I need to be able to programatically delete and then re-deploy into a Salesforce Sandbox. I do not want to deploy to production, I just need a quick way to delete all of these fil

  • How to increase test coverage for Batch job when there is already process running 2015-11-22

    1) I have a custom object "ObjectA__c" with three custom fields "Start_Date__c" and "End_Date__c" and "Status__c". 2) There is already process on this object which runs every time a record of object (ObjectA__c) is

  • Test coverage for a user trigger: How to prevent "DML not allowed on User" in test-class? 2016-06-17

    I want to deploy a trigger on user. Therefore I need > 1% coverage on the trigger. To invoke the trigger in my test class, I've tried this: @isTest public class elfTriggerFrameworkTest { public static testMethod void testObject() { User u = new User(

  • Is it realistic to ask your team to take on a 100% automated test coverage goal as a stretch task? 2011-07-02

    In a financial services environment - is it realistic to ask your team of ten programmers to take on a 100% automated unit and integration test coverage goal as a stretch task for a legacy java system (>1.5M SLOC)? [as a two year goal in increments]

  • Apex class won't save for lack of test coverage 2013-06-20

    I have been using Force.com IDE to work on VisualForce pages and classes directly in a live development environment. I was unaware of the requirement that 75% of your code had to be covered by tests. Suddenly today, when I wanted to change one line o

  • Test coverage for Http callout Exceptions 2015-10-05

    I'm writing test coverage for Apex class methods called via JavaScript remoting. For the following method, how would I create test code coverage so that it will generate an Http Exception? Is there a way to accomplish this via a setting in the MultiS

  • Reasonable test coverage requirements when dealing with a contractor? 2010-11-02

    We're outsourcing some work to an external developer, so I'm busy writing up a contract about what constitutes a deliverable. So far I require that the code is shipped with automated tests. But, what is a reasonable way to specify the detail of tests

  • Unit testing coverage 2011-03-29

    How much of your code do you unit test? Do you try to get 100% coverage of everything or do you skip some classes? Background We've made a lightweight ORM layer which we have written plenty of tests for. On top of that we use Code Contracts to specif

  • What's the difference between code coverage and test coverage? 2012-05-10

    At first I though it was the same but with eclemma it seems different. But I can't find anything about it. --------------Solutions------------- Test coverage and code coverage as generic terms can mean a broad variety of different measures of what ha

  • test coverage reality 2012-07-10

    I am NOT doing test driven development and I write my test classes after the actual code is written. In my current project I have a test coverage (Line coverage) of 70% for 3000 lines of Java code. I am using JUnit, Mockito and Sonar for testing. But

  • How to understand existing test coverage? 2012-08-01

    I believe this is a common issue for any QA that enters a project with existing test base. I entered the project which have lots of unit tests and FIT tests for existing functionality. When I test a certain defect or new sub-feature added to existing

  • Test Coverage fail on simple before insert & update apex trigger 2013-10-10

    Apex n00b here. This has been gnawing me up over past few weeks. I have this very simple before insert / update trigger on Opportunity that auto-selects the Price Book based on a dropdown value containing Sales Office (State) location info. Here's my

  • TDD and complete test coverage where exponential test cases are needed 2014-01-02

    I am working on a list comparator to assist sorting an unordered list of search results per very specific requirements from our client. The requirements call for a ranked relevance algorithm with the following rules in order of importance: Exact matc

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 1.069 (s). 13 q(s)