mercredi 27 février 2019

How to create a apex test class for my apex class

It is my first apex class and i don't really know how to implement a proper test class. My goal is to achieve test coverage of 75%.

Here is what i did :

Apex class:

public with sharing class AccountController {

@AuraEnabled
public static List<Account> findAll() {

    User userDetails =[SELECT Id, Name, Email, Profile.Name, UserRole.Name FROM User
        where Id=:userinfo.getUserId() ];

    // Theme4t is theme that is used by mobille app for  android or iphone 
    if(((userDetails.UserRole.Name).equals('yon')|| (userDetails.UserRole.Name).equals('bon')|| (userDetails.UserRole.Name).contains('non')
        || (userDetails.UserRole.Name).contains('go')) && UserInfo.getUiTheme() != 'Theme4t'){
       return [SELECT id, name, AccountStatus__c, ShippingLatitude, ShippingLongitude, ShippingCity
        FROM Account
        WHERE ShippingLatitude != NULL AND ShippingLongitude != NULL 
        LIMIT:22000];

    }else {
       return [SELECT id, name, AccountStatus__c, ShippingLatitude, ShippingLongitude, ShippingCity
        FROM Account
        WHERE OwnerId =: UserInfo.getUserId() AND ShippingLatitude != NULL AND ShippingLongitude != NULL 
        LIMIT:5000]; 

    }
}

Apex test class:

@isTest 
public class AccountControllerTest 
{
    static testMethod void testMethod1() 
           {
                          Account acc = new Account();
                          acc.Name='Test';
                          insert acc;

                         User userDetails = new User();
                         userDetails.UserRole.Name='yon';
                         insert userDetails;

                          List<Account> lstAcc = AccountController.findAll();

                         UserRole ur = new UserRole(Name = 'bon');
                         insert ur;
                           List<Account> lstAcc2 =AccountController.findAll();     
    }
}

Aucun commentaire:

Enregistrer un commentaire