0

I followed the following link to override the System.out.println call in java.

this is the program

public class palstr
{
    public String rev(String a)
    {
        String b=new StringBuffer(a).reverse().toString();
        return b;
    }
    public int check(String a,String b)
    {
        if(a.equals(b))
        {
                    return 1;
        }
        else
        {
                    return 0;
        }
    }

   public static void main(String a){
     palstr obj = new palstr();
     String b = obj.rev(a);
     int check= obj.check(a, b);

     if(check == 1){
       System.out.println("palindrome");
     } else {
       System.out.println("not palindrome");
     } 
   }
}

and this is the testcase

import java.io.*;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import static org.junit.Assert.*;

public class palstrTest
{
    @Test
    public void mainTest(){
        System.out.println("madam is a palindrome string");
        ByteArrayOutputStream outContent = new ByteArrayOutputStream();
        System.setOut(new PrintStream(outContent));
        palstr.main("madam");
        assertEquals("palindrome", outContent);
    }
 }

when i am running the test i am getting the following error

.madam is a palindrome string
E
Time: 0.026
There was 1 failure:
1) mainTest(palstrTest)
java.lang.AssertionError: expected:<palindrome> but was:<palindrome
>
Community
  • 1
  • 1
  • Well `outContent` is a `ByteArrayOutputStream`, not a `String`. Those two are different things - it sounds like you should convert the contents of the `ByteArrayOutputStream` into a string and then compare those... – Jon Skeet Apr 05 '17 at 11:55
  • As you saw when doing what my answer suggested, the types you try to compare mismatch -> do what Jon said. – luk2302 Apr 05 '17 at 12:55

0 Answers0