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
neao18neao18 

Strange behavior with test class!!

Hi all i have a strange problem with test coverage. It seems that even the test failed in testclass still the code coverage is 100% 

Note: I have a validation set by me to give me a error on field values if null.

* I know my code is bad but this is for just testing one senario

 

trigger webservice2 on Web_Service__c  (before insert) {

    list<Web_Service__c  > n= new list<Web_Service__c  >();
    for(Web_Service__c  w: trigger.new){
        n.add(w);
    }
    for(Web_Service__c  w: n){
        w.Rss_Url__c='';
    }
    Database.SaveResult[] lsr = Database.update(n, false);

}

                 

 and the test class:

 

@isTest
private class testwebservice2 
{
public class applicationException extends Exception {}
    public static testmethod void testing() 
    {
        
        Web_Service__c  a= new Web_Service__c (Rss_Url__c='www.h.com');
        insert a;
    }
}

 What my question is even after if it gives me this error :

 

System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, webservice2: execution of BeforeInsert caused by: System.SObjectException: DML statment cannot operate on trigger.new or trigger.old Trigger.webservice2: line 17, column 1: []

 

my code covers 100%

 

And also that can I deploy this code to production with this test failed class??

sfdcsushilsfdcsushil

Hi,

 

Your code is gettting executed till last statement, So the code coverage is 100%. If you write some other statements after the 
Database.upate statement, Code coverage will go down as code fails at this statement. 

As per my understanding, This code will not get deployed as test is failing.

 

Regards,

Sushil

neao18neao18

Thanks also i Updated the code can you explain why this also give 100% coverage with no error??

 

list<atp__c> n= new list<atp__c  >();
    for(Web_Service__c  w: trigger.new){
        atp__c a= new atp__c();
        n.add(a);
    }
    
    Database.SaveResult[] lsr = Database.update(n, false);
    for(Database.SaveResult d: lsr){
        if(d.isSuccess()){
        system.debug('###');
        }
        else{
        system.debug('###');
        }
    }

 as atp__c has a reequired field, the test should fail right!!

sfdcsushilsfdcsushil

You have changed the object that you were updating earlier. So no error now.

sfdcsushilsfdcsushil

You can't call database operation on object on which trigger is getting invoked. 

neao18neao18

Yes i know that i intennintentionally wrote like that:  

 

but for this 

list<atp__c> n= new list<atp__c  >();
    for(Web_Service__c  w: trigger.new){
        atp__c a= new atp__c();
        n.add(a);
    }
    
    Database.SaveResult[] lsr = Database.update(n, false);
    for(Database.SaveResult d: lsr){
        if(d.isSuccess()){
        system.debug('###');
        }
        else{
        system.debug('###');
        }
    }

 as atp__c has a reequired field, the test should fail right!!

sfdcsushilsfdcsushil

What do you mean by required field? Have you put any validation rule?

Name is required only if you are keeping it as text field. For Auto number it will automatically take next number in sequence.