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
venky reddy 6venky reddy 6 

write a Test class for a piece of code

public class Test
{
   public pagerefernce testmethod()
  {
     if(input.length() == 3 && input.subString(0,1) == char1 && input.subString(1,2) == char2 && input.subString(2,3) == char3)
     {
      stmt;
     }
       else
      {
           stmt;
      }
  }
}
 
pconpcon
What you have there makes no sense.  Can you please post the code you are trying to test?  We can help you with how you should right your test cases from there.
Wizno @ ConfigeroWizno @ Configero
Please post the code you're trying to write coverage for, as pcon stated, we can't make sense of that. 

Do keep this in mind though. Your Test Class is essentially performing the same function as you manually going in and executing the actions that would make your code run. 

So let's say you write a trigger to create a Case any time an Account is created. To manually test the functionality you would go to the Accounts Tab and create an Account. In the test class you'd write code that would Create an Account sObject, and then insert it. Bam, 100% coverage :-) 
Then you can do an assert to verify that the Case exists for the Account. 
venky reddy 6venky reddy 6
Hi,
 
Thanks for giving the reply


public class captcha
{
   List<String> characters;
   public String input {get; set;}
   public String result {get; set;}
   String char1;
   String char3;
   String char5;  
//In our contructor we will populate a list of strings with numbers and letters
public captcha()
{
     characters = new List<String>{'a','b','c','d','e','f','g','h', 'i','j','k','l','m','n','o','p','q','r','s','t','u','v','w', 'x','y','z','1','2','3','4','5','6','7','8','9','0' };
}  
//This methods simply returns a random number between 0 and the size of the character list
public Integer randomNumber()
{
     Integer random = Math.Round(Math.Random() * characters.Size());
     if(random == characters.size()){ random--; } return random; }
    /*Here we have 6 get methods that return 6 random characters to the page. For chars 1,3, and 5 (the black characters) we are saving the the      values so that we can compare them with the user's input */
public String getChar1()
{
     char1 = characters[randomNumber()];
      return char1;
}
public String getChar2()
{
    return characters[randomNumber()];
}
public String getChar3()
{
   char3 = characters[randomNumber()];
   return char3;
}
public String getChar4()

    return characters[randomNumber()];
}
public String getChar5()
{
     char5 = characters[randomNumber()];
     return char5;
}
public String getChar6()
{
     return characters[randomNumber()];
}
  /*In the validate method we make sure that the 3 characters entered equal the three black characters: char1, char3, char5*/
public void validate()
  {
      if(input.length() == 3 && input.subString(0,1) == char1 && input.subString(1,2) == char3 && input.subString(2,3) == char5)
      {
         result = 'Whoohoo! You got it right.';
      }
       else
     {
         result = 'Come on...the letters aren\'t even disfigured.';
      }
  }
}

This is my class i need for test class for this, it is captcha code. can you help me this

Thanks
venkat
 
pconpcon
I have updated your code for a little bit of ease or readability and for a little bit of making tests easier to write
 
public class Captcha {
    @testVisible
    private static SUCCES_MSG = 'Whoohoo! You got it right.';

    @testVisible
    private static ERROR_MSG = 'Come on...the letters aren\'t even disfigured.';

    @testVisible
    private List<String> characters;

    @testVisible
    private String char1;

    @testVisible
    private String char3;

    @testVisible
    private String char5;

    public String input {
        get;
        set;
    }
    public String result {
        get;
        set;
    }

    public Captcha() {
        characters = new List<String>{
            'a','b','c','d','e','f','g','h',
            'i','j','k','l','m','n','o','p',
            'q','r','s','t','u','v','w','x',
            'y','z','1','2','3','4','5','6',
            '7','8','9','0'
        };
    }

    public Integer randomNumber() {
        Integer random = Math.Round(Math.Random() * characters.Size());
        if (random == characters.size()) {
            random--;
        }
        return random;
    }

    public String getChar1() {
        this.char1 = characters[randomNumber()];
        return this.char1;

    }

    public String getChar2() {
        return characters[randomNumber()];
    }

    public String getChar3() {
        this.char3 = characters[randomNumber()];
        return this.char3;
    }

    public String getChar4() {
        return characters[randomNumber()];
    }

    public String getChar5() {
        this.char5 = characters[randomNumber()];
        return this.char5;
    }
    public String getChar6() {
        return characters[randomNumber()];
    }

    public void validate() {
        if (
            this.input.length() == 3 &&
            this.input.subString(0,1) == this.char1 &&
            this.input.subString(1,2) == this.char3 &&
            this.input.subString(2,3) == this.char5
        ) {
            result = SUCCESS_MSG;
        } else {
            result = ERROR_MSG;
        }
    }

Below is how I would test the validate method (both a positive test and a negative test)
 
static testMethod void validate_valid() {
    String testChar1 = 'a';
    String testChar3 = 'b';
    String testChar5 = 'c';
    String testInput = testChar1 + testChar3 + testChar5;
    
    Captcha testCaptcha = new Captcha();
    testCaptcha.input = testInput;
    testCaptcha.char1 = testChar1;
    testCaptcha.char3 = testChar3;
    testCaptcha.char5 = testChar5;
    
    Test.startTest();

    testCaptcha.validate();

    Test.stopTest();

    System.assertEquals(
        Captcha.SUCCESS_MSG,
        testCaptcha.result,
        'Did not get the expceted result message'
    );
}   

static testMethod void validate_invalid() {
    String testChar1 = 'a';
    String testChar3 = 'b';
    String testChar5 = 'c';
    String testInput = 'def';

    Captcha testCaptcha = new Captcha();
    testCaptcha.input = testInput;
    testCaptcha.char1 = testChar1;
    testCaptcha.char3 = testChar3;
    testCaptcha.char5 = testChar5;

    Test.startTest();

    testCaptcha.validate();

    Test.stopTest();

    System.assertEquals(
        Captcha.ERROR_MSG,
        testCaptcha.result,
        'Did not get the expceted result message'
    );
}

This should get you coverage on your validation methods, and should get you started on how the test methods should look for your other getChar* methods.
 
venky reddy 6venky reddy 6
Hi pcon,

Thanks for your helping. it is working fine.
pconpcon
Great!  Glad to hear.  If you then could please mark the answer above as the best answer so that others will know what the solution was as well as removing your question from the unanswered queue.