Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JUnit testing with int[] result

 
Larry Frissell
Ranch Hand
Posts: 82
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am testing a method that has a string input and returns an int[] (see code below)
The assert fails because the int[] expResult = {2010,1,23} is a different Object, if I manually test each int in the two arrays, I will see they are the same value.



My question is for JUnit testing do you have to override the assertEquals when comparing arrays?
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34396
346
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Larry,
In JUnit 4, a method called assertArrayEquals() was introduced. It is overloaded for all primitives and objects. It was introduced just for this purpose.
 
Larry Frissell
Ranch Hand
Posts: 82
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you, I did find that on the JUnit.org site while I was searching for a solution. However, I keep getting compile errors
cannot find symbol
symbol : method assertArrayEquals(int[],int[])
location: class mvc.DateEntryFormatterunit4Test
assertArrayEquals(expResult, result);
1 error


I am using NetBeans 6.9, I have JUnit 4.5 in the library and the JDK 1.6 on a mac.

I am not sure now what the problem is??
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Either missing an import, or not using the version of JUnit you think you are?
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34396
346
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Larry,
You can be using JUnit 4.X but still using the JUnit 3.8 assert class. Check your static import is for org.junit.Assert.* rather than junit.framework.Assert.*
 
Hebert Coelho
Ranch Hand
Posts: 754
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Working on your code, you could do:

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic