Unable to cover the code coverage for the below boolean method any suggestions please i tried the below test class and checked logs, its not going inside exception catch block even though i tried to get an exception by making user inactive.am i not calling the method right?

  • class: psController
  • method:isPLUser
public static boolean isPLUser() {
        if (((user)currentUser).Contact.Account.Recordtypeid != Utility.partnerRecType){
            throw new pscUtility.pscException('01', ' Record type is not valid.'); 
        } else if (!((user)currentUser).isActive) {
            throw new pscUtility.pscException('03', 'Partner user is not active');
        } else {
            return true;
    } catch(exception e) {
    return false;

Test class:

User testUser = [SELECT Id,contact.id,isactive ,contact.account.id FROM User WHERE Profile.name = 'Partner Sales'and isactive = true LIMIT 1];
boolean ispilot = pscMainController.isPLUser();
user u = pscMainController.getUserContext();
    boolean ispilot2 = psController.isPLUser();
    testUser.IsActive = false;
} catch (DmlException ex) {
    system.assertEquals(ispilot, testUser.IsActive,'Partner user is not active' );
    System.assertEquals('Partner user is not active', ex.getMessage());
  • Please edit your question to explain what exactly you mean by "Unable to test the boolean value...." Are you getting an error? If so, what is the exact error text? Are you getting unexpected results? If so, what exactly are you expecting & what exactly are your results? (Please read the What topics can I ask about here? section of Help Center where, among other things, it says that your question should include, 'A specific, detailed description of the issue ("it doesn’t work" is not enough to allow the community to help).')
    – Moonpie
    Sep 20, 2022 at 0:12
  • @Moonpie Hi, updated it. Sep 20, 2022 at 0:39

1 Answer 1


Your testmethod needs to run in the context of a running user

// given an active user
User testU = [SELECT Id FROM User WHERE Profile.name = 'Partner Sales'and isactive = true LIMIT 1];
System.runAs(testU) {
   ... logic/asserts for active use case
// Given an inactive user
System.runAs(new User(Id = UserInfo.getUserId()) {
  update new User(Id = testU.Id,IsActive = false);
testU = [SELECT Id FROM User WHERE Id = :testU.Id LIMIT 1]; // refresh our context user
System.runAs(testU) {
   ... logic/asserts for inactive use case

  • tried but couldn't trigger the try block Sep 20, 2022 at 19:54
  • I edited my answer
    – cropredy
    Sep 21, 2022 at 3:10

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .