How to write test conditions for try and catch blocks in apex. My code coverage is not 100% by below code. Please help.
Apex class:
public static List<String> getUserName(List<String> currentOwnerValues) {
List<String> ownerNames = new List<String>();
for (String currentOwnerValue : currentOwnerValues) {
String ownerName = currentOwnerValue;
try {
User u = [SELECT Id, Name FROM User WHERE FederationIdentifier = :currentOwnerValue];
if (u != null) {
ownerName = String.escapeSingleQuotes(u.Name);
}
} catch (Exception ex) {
ownerName = String.escapeSingleQuotes(currentOwnerValue);
}
ownerNames.add(ownerName);
}
return ownerNames;
}
Apex Test class:
public static void getUserName() {
List<User> usersList = new List<User>();
List<String> ownerList = new List<String>();
controller.getUserName(ownerList);
User u = new User();
u.FederationIdentifier = '123456';
usersList.add(u);
for (User u1 : usersList) {
ownerList.add(u.FederationIdentifier);
}
controller.getUserName(ownerList);
}