mardi 29 septembre 2015

How do I properly write out a JUnit test for a method that counts how many e's are in a string?

Here is the method I have defined that is supposed to accept a String as input and should return the number of times the char 'e' occurs as an int:

public int count_e(String input){
    int count = 0;
    for (int i = 0;i<input.length();i++){
        char e = 'e';
        if (input.charAt(i)==e){
            count=count+1;
            i++;
            return count;
        }
        else{
            count=count+1;
            i++;
        }
    }
    return count;
}

}

I am trying to write a JUnit test to see if I can input a string into the method and return the correct number of e's. Below is my test, and at the moment I keep getting an error saying that my method count_e is undefined for type String.

Can someone tell me why it is coming up as undefined?

@Test
public void testCount_e() {
    String input= "Isabelle";
    int expected= 2;
    int actual=input.count_e();
    assertTrue("There are this many e's in the String.",expected==actual);
}

}

Aucun commentaire:

Enregistrer un commentaire