File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

assertEquals with println

 
Edmund Wong
Greenhorn
Posts: 3
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 15150
31
Android IntelliJ IDE Java Scala Spring
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
 
Edmund Wong
Greenhorn
Posts: 3
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic