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
User 444User 444 

I want help in writing test class with 100%code coverage for below apex class.

Can someone please help in writing test class for below apex class with 100% code coverage. thanks!

public static List<String> getUserName(List<String> currentOwnerValues) {
    List<String> ownerNames = new List<String>();
set<String> uniqueIds = new set<String>(); 
    for (String currentOwnerValue : currentOwnerValues) {
        String trimmedId = currentOwnerValue.trim();
       If(!string.isBlank(trimmedId)){
uniqueIds.add(trimmedId);
}
}

Map<String, User> userMap = new Map<String, User>();
if(uniqueIds.size()>0){
for(   User u = [SELECT Id, Name FROM User WHERE FederationIdentifier IN :uniqueIds];
}
for(String currentOwnerValue : currentOwnerValues){
String trimmedId = currentOwnerValue.trim();
User u = userMap.get(trimmedId);
string ownername;
         
            if (u != null) {
                ownerName = String.escapeSingleQuotes(u.Name);
            }
        } catch (Exception ex) {
            ownerName = String.escapeSingleQuotes(trimmedId);
        }
        ownerNames.add(ownerName);
    }
    return ownerNames;
}

 
Best Answer chosen by User 444
Gian Piere VallejosGian Piere Vallejos

I think this method would help you: 

@isTest
private class MyControllerTest {
    @isTest
    static void testUserMethod() {

        List <String> federationLists = new List <String> ();
        for( User usr : [SELECT Id, FederationIdentifier FROM User WHERE FederationIdentifier != NULL LIMIT 5]) {
            federationLists.add(usr.FederationIdentifier);
        }

        List <String> res = TestController.getUserName(federationLists);
        Assert.isNotNull(res, 'Result is not null.');
    }
}

All Answers

Gian Piere VallejosGian Piere Vallejos

I think this method would help you: 

@isTest
private class MyControllerTest {
    @isTest
    static void testUserMethod() {

        List <String> federationLists = new List <String> ();
        for( User usr : [SELECT Id, FederationIdentifier FROM User WHERE FederationIdentifier != NULL LIMIT 5]) {
            federationLists.add(usr.FederationIdentifier);
        }

        List <String> res = TestController.getUserName(federationLists);
        Assert.isNotNull(res, 'Result is not null.');
    }
}
This was selected as the best answer
User 444User 444
Hi Gian,
Thanks!That helped, it is 98% now.

(if (u==null)) -->else condition didnt cover up. Can you please check.
 if (u != null) { ownerName = String.escapeSingleQuotes(u.Name); } }
else { ownerName = String.escapeSingleQuotes(trimmedId); }
The highlighted else code is not covered up. Could you please help.
Gian Piere VallejosGian Piere Vallejos

Hello, 

I think your main code is missing some lines after "for( User u = [SELECT Id, Name FROM User WHERE FederationIdentifier IN :uniqueIds];".

I'll rewrite your code in this way:

public with sharing class TestController {

    public static List<String> getUserName(List<String> currentOwnerValues) {
        
        Set <String> uniqueIds = new set <String>(); 
        for (String currentOwnerValue : currentOwnerValues) {
            String trimmedId = currentOwnerValue.trim();
            if (!String.isBlank(trimmedId)){
                uniqueIds.add(trimmedId);
            }
        }

        Map<String, User> userMap = new Map<String, User>();
        if(uniqueIds.size()>0){
            if (
                User.SObjectType.getDescribe().isAccessible() &&
                Schema.SObjectType.User.fields.Name.isAccessible()) {
                    for( User u : [SELECT Id, Name FROM User WHERE FederationIdentifier IN :uniqueIds]){
                        userMap.put(u.Id, u);
                    }
                }
        }
        
        List<String> ownerNames = new List<String>();
        for(String currentOwnerValue : currentOwnerValues){            
            String trimmedId = currentOwnerValue.trim();
            string ownername = '';
            if (userMap.containsKey(trimmedId)) {
                User u = userMap.get(trimmedId);
                ownerName = String.escapeSingleQuotes(u.Name);
            } else {
                ownerName = String.escapeSingleQuotes(trimmedId);
            }            
            ownerNames.add(ownerName);
        }

        return ownerNames;
    }
}
 

And I'm getting 100% coverage with this Test class: 

@isTest
private class MyControllerTest {
    @isTest
    static void testUserMethod() {

        List <String> federationLists = new List <String> ();
        for( User usr : [SELECT Id, FederationIdentifier FROM User WHERE FederationIdentifier != NULL LIMIT 5]) {
            federationLists.add(usr.FederationIdentifier);
        }

        List <User> users = [SELECT Id, Name FROM User WHERE FederationIdentifier IN :federationLists];
        if (!users.isEmpty()) {
            federationLists.add(users[0].Id);
        }

        List <String> res = TestController.getUserName(federationLists);
        Assert.isNotNull(res, 'Result is not null.');
    }
}
If your main class is different to mine, please send it complete and I'll double check. 
User 444User 444
That worked, Thanks a lot!