0

I'm having trouble reaching 75% code coverage on a class I created. Right now it's at 43% and I have no Idea why the rest isn't covered.

My class:

global class QA_Bisemanal implements Schedulable {
global void execute(SchedulableContext SC) {

UserRole      Role  = [SELECT Id from UserRole where Name='Tech Ninja'];    
List<User>    us    = [Select Id, Name, UserRoleId From User Where IsActive=True And UserRoleId =: Role.Id];
List<task> tas = new list<task>();

    system.debug(Us[0].Name);


    Integer i;
    integer j; 
    integer mod;
    DateTime dia;
    String num_dia;


    for (User Usu: US)
    {

     List<Account> accts = [SELECT Id, Name, OwnerID from account where Cliente_Activo__c=True AND OwnerId=:Usu.id];

        i = 0;
        j = 1;

    system.debug(accts.size());

     For (Account acc: accts)
     {

        dia       =   Date.today()+i;
        num_dia   =     dia.format('u');

        if (num_dia=='5'){
            i = i + 2;
        } else if (num_dia=='6'){
            i = i + 1;
        }



        Task t= new task();
        t.Subject       =   'Extra Mile Bisemanal Cliente';
        t.ActivityDate  =   Date.today()+i;
        t.Description   =   'Tener una interacción de valor con el cliente. Deja escrito en este espacio lo que hiciste. Puedes compartir una nueva plantilla, enseñar una nueva funcionalidad, buscar algún buen insight dentro de su data, compartir la grabación de un webinar o invitar a uno.';
        t.Status        =   'Open';
        t.WhatId        =   acc.Id;
        t.OwnerId       =   acc.OwnerId;
        t.Priority      =   'Normal';
        t.Contrase_a_Adwords__c  =   num_dia;

        tas.add(t);

        system.debug('Cuentas'+tas.size()); 

        mod = math.mod(j,2);
        if (mod==0){
            i++;
        }
        j++;
     }

    }

insert tas;

}

}

Test Class:

@isTest(seealldata=false) private class QA_Bisemanal_isTest{ public static String CRON_EXP = '0 0 0 15 3 ? 2022'; @testsetup static void createtestdata(){

UserRole rol = new UserRole();
rol.name='Tech Ninja';

user u = new user();

u.LastName = 'Test Code';
u.Email = 'test@test.com';
u.Alias = 'Tcode';
u.Username = Crypto.getRandomLong()+'@'+Crypto.getRandomLong()+'.abc';
u.CommunityNickname = 'test12';
u.LocaleSidKey = 'en_US';
u.TimeZoneSidKey = 'GMT';
u.ProfileID = '00e36000000ao2F';
u.LanguageLocaleKey = 'en_US';
u.EmailEncodingKey = 'UTF-8'; 
u.isactive=true;
u.UserRoleId=rol.id;
insert u;

List<Account> Ac  = new list<Account>();
for(integer i=0;i<15;i++){
    Account myaccount = new account();
    myaccount.Name='Scrum Diario The Influence';
    myaccount.Cliente_Activo__c=true;
    myaccount.OwnerId=u.id;
    myaccount.Pais__c = 'Guatemala';
    myaccount.Pagina_Web__c = 'Pagina.com';
    ac.add(myaccount);

}

Insert ac;

 }
 static testmethod void test1(){
    Test.startTest();
        system.schedule('test job', CRON_EXP, new QA_Bisemanal());
    Test.stopTest();
}
}

The test class stops covering on the second for loop as if the list accts was empty while in the Test class I created 15 accounts that should match the criteria of the query that fills this list.

  • I don’t see 1 assertion in your class, you should probably read through the trailhead to learn more on testing your code. – glls Jun 15 '18 at 22:03
5

It is not the Accounts that fail to match your criteria, it is your User. Since you do not insert your UserRole, its Id is null, meaning the inserted user doesn't match the query in your main class.

UserRole rol = new UserRole();
rol.name='Tech Ninja';

user u = new user();

// ... code snipped ... 
u.UserRoleId=rol.id; // rol.Id is null here
insert u;

Then in main class:

UserRole      Role  = [SELECT Id from UserRole where Name='Tech Ninja'];

If the above query doesn't fail, you must have a Role with that name in your org. Roles are metadata and can be seen in test context! So you're getting back not the test role that you just created, but a real Role.

List<User>    us    = [Select Id, Name, UserRoleId From User Where IsActive=True And UserRoleId =: Role.Id];

This is presumably returning an empty list, unless you have 'Tech Ninja' users existing in your org - again, Users are metadata.

Later,

 List<Account> accts = [SELECT Id, Name, OwnerID from account where Cliente_Activo__c=True AND OwnerId=:Usu.id];

This will return no Accounts, because you cannot see the real Accounts owned by any Tech Ninja users that might be returned by the User query.

As glls points out in the comments, you should be making assertions throughout your test class to show that the behavior of your code is as expected. Otherwise, what you have is a smoke test - it only shows whether or not your code blows up with an exception, not that it behaves correctly. Here's a couple of great testing resources:

  • Fresh profile pic :p – glls Jun 15 '18 at 22:39

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.