• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

assertEquals with println

 
Greenhorn
Posts: 3
  • Likes 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.



 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Likes 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
  • 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
 
Never trust an airline that limits their passengers to one carry on iguana. Put this tiny ad in your shoe:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
    Bookmark Topic Watch Topic
  • New Topic