3

how do I cover the else part in this test class

public with sharing class RecUtility {
public static boolean flag=true;
public static boolean runonce(){
if(flag){
   flag=false;
  }
   else {
       return flag;
   }
  return true;
  }
}

here is the test class

@isTest
private class RecUtilityTest
{
static testMethod void testMethod1()
{
    RecUtility r = new RecUtility();
    RecUtility.runonce();
}
}
6

I'd fix the code so it's covered easier. Aside from that, you shouldn't need to set the flag within the test itself, as you're trying to validate the behavior.

Here's how I'd reduce the complexity:

public class RecUtility {
    static Boolean flag = true;
    public static Boolean runOnce() {
        return flag && !(flag = false);
    }
}

Then, your unit test would assert the values from running the method:

@isTest static void testRecUtility() {
    // Verify flag is set
    System.assert(RecUtility.runOnce(), 'Expected true on first run');
    // Verify flag is not set
    System.assert(!RecUtility.runOnce(), 'Expected false on second run'); 
}

Notice how I do not modify flag directly in the test. This is because we're testing how the behavior works, and we want to know when we make a breaking change in the code. In fact, you might later decide to alter how runOnce determines that it runs once, and this should not cause you to have to change your unit tests, assuming that the intended behavior is identical.

|improve this answer|||||
2

This will cover your complete class

@isTest
    private class RecUtilityTest
    {
       static testMethod void testMethod1() {
          RecUtility.runonce();
          RecUtility.flag = false;
          RecUtility.runonce();
       }
    }
|improve this answer|||||
  • 1
    This will produce the compile error "Class static variable cannot be accessed via object instance". The flag needs to be accessed via the class name because it is static. – Keith C Jul 5 '16 at 16:27
  • There are no asserts to test the value of the flag – Eric Jul 5 '16 at 16:40
2

Since the variable in question is public, you should be able to modify it from the test class, using RecUtility.flag = someBooleanValue;

However, as in @sfdcfox's approach, and as mentioned in the comments, the best solution is to use System.assert methods to verify that the correct result is being returned, and to call the method a number of times to ensure the correct behavior.

@isTest
private class RecUtilityTest {
    static testMethod void TestRunOnce() {
        System.assertEquals(true, RecUtility.runonce()); 
        System.assertEquals(false, RecUtility.runonce());
    }
}
|improve this answer|||||
  • 1
    This will produce the compile error "Class static variable cannot be accessed via object instance". The flag needs to be accessed via the class name because it is static. – Keith C Jul 5 '16 at 16:28
  • Good catch, Ive edited my question to reflect this. – battery.cord Jul 5 '16 at 16:32
  • 2
    There are no asserts to test the value of the flag – Eric Jul 5 '16 at 16:40
  • @battery.cord For what it's worth, assertEquals (and assertNotEquals), expects the "expected" value first, followed by the "actual" value second. If you reverse the parameters, has you've done here, failed tests would get nonsense failures like "Expected: false, Actual: true", instead of reading what you meant to say: "Expected: true, Actual: false". – sfdcfox Jul 5 '16 at 20:59
  • @sfdcfox It's a subtle change, but I'd argue an important one. I've updated my code to reflect the correct function signatures. – battery.cord Jul 6 '16 at 13:31

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.