So I'm redirecting my print outputs to a stream, which becomes a char array. Then, when I use Junit, I use .toString() on the char array to make it become a long string. However, when I compare the string with the exact string that it should be, it says they are not equal--if and only if I'm on a Windows 7 computer. When I'm on a linux, or a mac, it seems to work fine, and the test passes. This doesn't seem to make sense for it to work on one computer but not another.

This is the code:

ByteArrayOutputStream os = new ByteArrayOutputStream();
		PrintStream myPrintStream = new PrintStream(os);
		/*
		 * make reference to default print stream
		 */
		PrintStream defaultOutstream = System.out;
		System.setOut(myPrintStream);
		
		m.logic(args); // this actually runs my program that creates the output
		
		System.setOut(defaultOutstream); // returns output stream to System.out
		String expected = 
			"Pop\tTitle\n" +
			"-------------------\n" +
			"94\tClose Shave, A (1995)\n" +
			"93\tWrong Trousers, The (1993)\n" +
			"93\tWallace & Gromit: The Best of Aardman Animation (1996)\n" +
			"86\tGrand Day Out, A (1992)\n" +
			"81\tToy Story (1995)\n";
	
		String actual = os.toString(); // converts to string
		
		
		assertEquals(expected, actual);

I assure you, that the expected and the os.toString() is exactly the same, in fact it's copy pasted from it. I've also tried doing assertTrue(expected.compareTo(actual)) which still returns false on a Windows 7 computer. Is this even possible?

Additional info: I'm using Eclipse, Junit 4, tried on Windows 7 32 and 64-bit. Works fine on non-Windows 7 it seems...

Anyone have any idea what could be causing this? When I trace the error on Eclipse, it shows the actual outut and the expected output, and they're exactly the same, yet it says it's not the same. Usually it would highlight the difference, whether it be a space, an extra hard return, or anything.

Thanks for your help/

Why are you using a PrintStream? You do know that a PrintStream will change the character encoding, right? Since the string created using s = "...." is UTF-8 encoded and the bytes (not chars) that you are reading from the ByteArrayOutputStream represent characters encoded with the System default encoding. Therein may lay your problem.

Try doing getBytes on your s= "....." String and compare that byte array with the byte array that backs the ByteArrayOutputStream.

Edit: P.S. I am not saying that those byte arrays will be the same, BTW. I am saying that this is one way in which you can more more finely check what the difference is. BTW getBytes also translates to the default character encoding so they probably will be the same, but then try using getBytes("UTF-8") and compare them again.

Having said all this, that "toString" should have created another UTF-8 String (I can guarantee it did), so they should still have been equals, but it is worth checking.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.