aspose file tools*
The moose likes Beginning Java and the fly likes assertEquals with println Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "assertEquals with println" Watch "assertEquals with println" New topic
Author

assertEquals with println

Edmund Wong
Greenhorn

Joined: Jul 18, 2012
Posts: 3
Hi,

I'm new here, plus I'm new to Java and Eclipse, which is what I'm using to do java programming.

Given this:



Why do I get the following assertion error?

Exception in thread "main" org.junit.ComparisonFailure: expected:<Hello World![]> but was:<Hello World![
]>
at org.junit.Assert.assertEquals(Assert.java:125)
at org.junit.Assert.assertEquals(Assert.java:147)
at HelloWorldTest.main(HelloWorldTest.java:24)

I got this part:

final ByteArrayOutputStream outContent = new ByteArrayOutputStream();

System.setOut(new PrintStream(outContent));

from googling on how to compare what is printed out from "System.out.println" with
an expected string. (I'm c&p from the output window.. I'm noticing that the
"but was:.." part has an extra \n between the []. Is this normal?


Am I doing something wrong?

Any help appreciated.



Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14104
    
  16

The actual output is "Hello World!" with a newline character appended to it; you expect that it is "Hello World!" without a newline character appended.

Either try this in line 24:

Or, in line 9, use print() instead of println(). Note that println() prints the text you specify plus a newline.


Are you using JUnit? Normally you would not have a main() method in your JUnit test. You'd just write a test method with a @Test annotation above it, and then use JUnit from Eclipse to run the test (Run -> Run As... -> JUnit Test).

So, instead of a main() method in class HelloWorldTest, you'd have something like this:


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Edmund Wong
Greenhorn

Joined: Jul 18, 2012
Posts: 3
Jesper de Jong wrote:The actual output is "Hello World!" with a newline character appended to it; you expect that it is "Hello World!" without a newline character appended.

Either try this in line 24:

Or, in line 9, use print() instead of println(). Note that println() prints the text you specify plus a newline.



Oooh. I understand now. Thanks.

Jesper de Jong wrote:
Are you using JUnit? Normally you would not have a main() method in your JUnit test. You'd just write a test method with a @Test annotation above it, and then use JUnit from Eclipse to run the test (Run -> Run As... -> JUnit Test).

So, instead of a main() method in class HelloWorldTest, you'd have something like this:


Yes, I'm using JUnit, but I have no idea how to use it properly. Thanks for the help on that though.

Edmund
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: assertEquals with println