Win a copy of Functional Design and Architecture this week in the Functional programming forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

assertEquals with println

 
Edmund Wong
Greenhorn
Posts: 3
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic