-3
public String replaceVowels(String s){
   String replacement = "";
   for(int i = 0; i <s.length(); i++){
       if(s.charAt(i)=='a' || s.charAt(i)=='A' ||
               s.charAt(i)=='e' || s.charAt(i)=='E'||
               s.charAt(i)=='i' || s.charAt(i)=='I'||
               s.charAt(i)=='o' || s.charAt(i)=='O'||
               s.charAt(i)=='u' || s.charAt(i)=='U'){
           replacement = replacement + '*';
         }else{
             replacement = replacement + s.charAt(i);
         }

   }
   return replacement;

}

I want the code to replace vowels by * in the string and here is my test code

@Test public void tests8(){
code.Solution s =  new code.Solution();
String input = "book ";
String expected = "b**k";
String actual = s.replaceVowels(input);
assertTrue("Expected was" +expected+"but the actual was" +actual  ,  expected == actual);

}

The error is so weird when I run the junit it said that

expected was b * * k but the actual was b * * k

Whats wrong with my code?

QifengSun
  • 33
  • 1
  • 7
  • You have a space at the end. So replaceVowels returns `"b**k "` but you expected `"b**k"`. – user253751 Feb 10 '15 at 22:38
  • 1
    Voting to close this as a simple typographical error. – user253751 Feb 10 '15 at 22:38
  • Even if the space is removed, you will need to use [`expected.equals(actual)`](http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) (or just use `assertEquals` instead). – rgettman Feb 10 '15 at 22:40

3 Answers3

0

You should use assertEquals(String, Object, Object), since you need to compare the values of the two strings, not their identity.

(And you probably don't need your custom message any more if you do)

Andy Turner
  • 137,514
  • 11
  • 162
  • 243
0

Remove the trailing space in "b**k " and also use the string.equals(String other) function to compare strings, not the == operator. == will only work when comparing string literals because they will point to the same object.

Cihan Tek
  • 5,349
  • 3
  • 22
  • 29
0

In addition to the typo @immibis mentions, your testcase reads: assertTrue("Expected was" +expected+"but the actual was" +actual , expected == actual);

It should read: assertTrue("Expected was" +expected+"but the actual was" +actual , expected.equals(actual));

== is not the same as .equals(I).

== means are they the same instance, whereas .equals() means they have the same content.

pojo-guy
  • 966
  • 1
  • 12
  • 39