0

This is the Trigger that I am trying to get code coverage for, but my test class is failing.Both testMethods are passing, but it's saying "No Code Coverage". So I guess my two test methods aren't really testing anything? Can someone help explain where I am going wrong, or what I am missing? Thanks!

Trigger

    trigger FinanceApprovalAdDirector on Rate_Request__c (before insert,before update) {
    for(Rate_Request__c fa: trigger.new)
    {
        Market__c m = [Select Ad_Director__c From Market__c WHERE Unit_Number__c = :fa.Owner_Market__c];
        fa.Ad_Director__c = m.Ad_Director__c;
        User u = [Select ManagerId From User WHERE Id = :UserInfo.getUserId()];
        fa.Manager__c = u.ManagerId;
    }
}

My Test Class

@isTest(SeeAllData=true)
public class FinanceApprovalAdDirectorTest{
 static testMethod void testRateRequestManager(){
        Profile p = [SELECT Id FROM Profile WHERE Name='Standard User'];
        User u1 = new User(Alias = 'standt1', Email='standarduser1@testorg.com', EmailEncodingKey='UTF-8', LastName='Testing', LanguageLocaleKey='en_US', LocaleSidKey='en_US', ProfileId = p.Id, TimeZoneSidKey='America/Los_Angeles', UserName='standarduser43431212@testorg.com');      
        User u = new User(Alias = 'standt', Email='standarduser@testorg.com', EmailEncodingKey='UTF-8', LastName='Testing', LanguageLocaleKey='en_US', LocaleSidKey='en_US', ProfileId = p.Id, TimeZoneSidKey='America/Los_Angeles', UserName='standarduser@43431313testorg.com', ManagerId = u1.id);      

        System.runAs(u){
            Market__c m = new Market__c();
            m.Unit_Number__c = '1111';
            m.Name = 'test';
            m.Ad_Director__c = u.id;
            insert m;

            Market__c mTest = [SELECT Ad_Director__c, Unit_Number__c, Name FROM Market__c WHERE Unit_Number__c = :m.Unit_Number__c];

            test.startTest();
            Rate_Request__c rr = new Rate_Request__c();
            rr.Ad_Director__c = mTest.Ad_Director__c;
            rr.Manager__c = u.ManagerId;

            System.assertEquals(u1.id, rr.Manager__c);
            System.assertNotEquals(u.id, rr.Manager__c);
            test.stopTest();
        }
    }

    static testMethod void testRateRequestAdDirector(){
        Profile p = [SELECT Id FROM Profile WHERE Name='Standard User'];
        User u = new User(Alias = 'standt1', Email='standarduser1@testorg.com', EmailEncodingKey='UTF-8', LastName='Testing', LanguageLocaleKey='en_US', LocaleSidKey='en_US', ProfileId = p.Id, TimeZoneSidKey='America/Los_Angeles', UserName='standarduser13131212@testorg.com');      
        User u1 = new User(Alias = 'standt', Email='standarduser@testorg.com', EmailEncodingKey='UTF-8', LastName='Testing', LanguageLocaleKey='en_US', LocaleSidKey='en_US', ProfileId = p.Id, TimeZoneSidKey='America/Los_Angeles', UserName='standarduser@12121313testorg.com', ManagerId = u.id);      

        System.runAs(u){
            Market__c m = new Market__c();
            m.Unit_Number__c = '1111';
            m.Name = 'test';
            m.Ad_Director__c = u.id;
            insert m;

            Market__c mTest = [SELECT Ad_Director__c, Unit_Number__c, Name FROM Market__c WHERE Unit_Number__c = :m.Unit_Number__c];

            test.startTest();
            Rate_Request__c rr = new Rate_Request__c();
            rr.Ad_Director__c = mTest.Ad_Director__c;

            System.assertEquals(u.id, rr.Ad_Director__c);
            System.assertNotEquals(u1.id, rr.Ad_Director__c);
            test.stopTest();
        }     
    }    

}
4
  • It would speed up a proper answer being generated if you would define specifically wha you mean by "failing" (which assert, error, etc)
    – Eric
    Dec 8, 2015 at 14:41
  • Both testMethods are passing, but it's saying "No Code Coverage". So I guess my two test methods aren't really testing anything?
    – Stephen
    Dec 8, 2015 at 14:47
  • See updated answer
    – Eric
    Dec 8, 2015 at 14:49
  • In additon to the excellent answers below ... Best Practice says get rid of SeeAllData=true - mock all data needed by testmethods. SeeAllData=true can lead to failed deployments now or in the future
    – cropredy
    Dec 8, 2015 at 17:46

2 Answers 2

1

First Mistake:

You should never have a SOQL query inside for loop.

Before moving to your test classes, replace your code with this

Map<ID,List<String>> markerOwnerList = new Map<ID,List<String>>();
for(Rate_Request__c fa: trigger.new){
    if(markerOwnerList.containsKey(fa.Owner_Market__c))
        markerOwnerList.get(fa.Owner_Market__c).add(fa.id);
    else
        markerOwnerList.put(fa.Owner_Market__c,new List<String>{fa.id});
}

Map<ID,String> rrDirectorMap = new Map<ID,String>();

for(Market__c m : [Select Ad_Director__c,Unit_Number__c From Market__c WHERE Unit_Number__c = :markerOwnerList.keySet()]){
    for(String rateRequest : markerOwnerList.get(m.Unit_Number__c))
        rrDirectorMap.put(rateRequest,m.Ad_Director__c);
}

User u = [Select ManagerId From User WHERE Id = :UserInfo.getUserId()];
for(Rate_Request__c fa: trigger.new){
    if(rrDirectorMap.containsKey(fa.id))
        fa.Ad_Director__c = rrDirectorMap.get(fa.id);

    fa.Manager__c = u.ManagerId;
}

Regarding your test class, you are not inserting the RateRequest record after creating it.

@isTest(SeeAllData=true)
public class FinanceApprovalAdDirectorTest{
 static testMethod void testRateRequestManager(){
        Profile p = [SELECT Id FROM Profile WHERE Name='Standard User'];
        User u1 = new User(Alias = 'standt1', Email='standarduser1@testorg.com', EmailEncodingKey='UTF-8', LastName='Testing', LanguageLocaleKey='en_US', LocaleSidKey='en_US', ProfileId = p.Id, TimeZoneSidKey='America/Los_Angeles', UserName='standarduser43431212@testorg.com');      
        User u = new User(Alias = 'standt', Email='standarduser@testorg.com', EmailEncodingKey='UTF-8', LastName='Testing', LanguageLocaleKey='en_US', LocaleSidKey='en_US', ProfileId = p.Id, TimeZoneSidKey='America/Los_Angeles', UserName='standarduser@43431313testorg.com', ManagerId = u1.id);      

        System.runAs(u){
            Market__c m = new Market__c();
            m.Unit_Number__c = '1111';
            m.Name = 'test';
            m.Ad_Director__c = u.id;
            insert m;

            Market__c mTest = [SELECT Ad_Director__c, Unit_Number__c, Name FROM Market__c WHERE Unit_Number__c = :m.Unit_Number__c];

            test.startTest();

            Rate_Request__c rr = new Rate_Request__c();
            insert rr;

            System.assertEquals(u1.id, rr.Manager__c);
            System.assertNotEquals(u.id, rr.Manager__c);
            test.stopTest();
        }
    }
}

I hope you will be able to fix your next method.

3
  • The trigger is only looping through one Rate Request at a time, so shouldn't it be fine in this case? I know the rule that its not good practice in most cases, but this one shouldn't be an issue I believe
    – Stephen
    Dec 8, 2015 at 14:56
  • 1
    @Stephen No. If you end up with 51 Rate_Request__c objects that get updated in a single shot, you'll run into the soql query limit with your current trigger. Even if you don't think you'll run into that situation, it's a very good idea to plan for it. You'd also run into this issue as your code base expands, and more things start interacting with Rate_Request__c
    – Derek F
    Dec 8, 2015 at 15:02
  • @Stephen - No, never get into the habit of doing this even if you think the trigger will only process a single record in a transaction. You need to heed this advice or you are starting from a point of failure that will come back to bite you as your code expands and requirements change
    – Eric
    Dec 8, 2015 at 15:03
0

First Issue:

You are not Inserting the Rate_Request__c records so the trigger will never execute.

trigger FinanceApprovalAdDirector on Rate_Request__c (before insert,before update) 

Since the trigger happens on insert or update it will not run unless you insert or update the appropriate records.

Once you do that this will happen

Your query:

[Select Ad_Director__c From Market__c WHERE Unit_Number__c = :fa.Owner_Market__c];
        fa.Ad_Director__c = m.Ad_Director__c;

requires that the Rate_Request__c.Owner_Market__c match the Unit_Number__c.

I do not see in your tests where you are setting the value of the Rate_Request__c.Owner_Market__c` thus it is null and does not match the value or the Unit_number__c and thus will return no results

This will result in a List has no rows for assignment to sObject error

2
  • It's saying that the field Rate_Request__c.Owner_Market__c is not writeable. How would you recommend getting the field to match the Market__C.Unit_Number__c without getting this error message?
    – Stephen
    Dec 8, 2015 at 15:00
  • Ok, I had no way of knowing it was a formula field. Where is that field pulling the data from? That is what you need to set.
    – Eric
    Dec 8, 2015 at 15:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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