This week's book giveaway is in the Cloud/Virtualization forum.
We're giving away four copies of Mastering Corda: Blockchain for Java Developers and have Jamiel Sheikh on-line!
See this thread for details.
Win a copy of Mastering Corda: Blockchain for Java Developers this week in the Cloud/Virtualization 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Bear Bibeault
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Jj Roberts
  • Carey Brown
Bartenders:
  • salvin francis
  • Frits Walraven
  • Piet Souris

How do you handle since Unit test is not very accurate ?

 
Ranch Hand
Posts: 717
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear experts,

I am running into another puzzle which I hope to know how to handle.

So here's the test I am trying to run :



So, when you run the test, it will actually shows the System.out.println("Error! There seems to be a problem");

How do you handle this kind of situation, especially for beginner like me, since they are not supposed to print that line since input did == expected.



 
Saloon Keeper
Posts: 12608
273
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Attach a debugger to your test and step through the code until you find the line that is incorrect.

If you do so, you will find that input != expected. Why? Well, what does the != operator mean for arrays?
 
Saloon Keeper
Posts: 23006
156
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you use the jUnit testing framework it does this sort of stuff for you.

Instead of "if" statements and "println"s, jUnit has a set of assert methods that can be used to test, throw an Exception on failure, and report the offending values.
 
tangara goh
Ranch Hand
Posts: 717
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:Attach a debugger to your test and step through the code until you find the line that is incorrect.

If you do so, you will find that input != expected. Why? Well, what does the != operator mean for arrays?



But, I find this fake "passed" can be very dangerous to people who are ignorant about it.

Therefore, I find JUnit itself is not 100% reliable....
 
Tim Holloway
Saloon Keeper
Posts: 23006
156
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

tangara goh wrote:
Therefore, I find JUnit itself is not 100% reliable....



I'm not following that. If I code an "assertEquals( a_test_array, testedObject.getArray() )" method call, I'd expect that thanks to the wonders of polymorphism (and/or introspection) that jUnit would be able to do an element-by-element verification of an array.

On the other hand, "a_test_array != testedObject.getArray()" would almost invariably return true, because to be otherwise would require both arguments to be the exact same instance of an array, and not merely that the tested object's array has the proper values in it.
 
Sheriff
Posts: 22054
113
Eclipse IDE Spring VI Editor Chrome Java Ubuntu Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tim Holloway wrote:If I code an "assertEquals( a_test_array, testedObject.getArray() )" method call, I'd expect that thanks to the wonders of polymorphism (and/or introspection) that jUnit would be able to do an element-by-element verification of an array.


Unfortunately you'd be wrong. JUnit has separate assertArrayEquals methods for comparing arrays element by element. assertEquals is not overloaded to take arrays.
 
Tim Holloway
Saloon Keeper
Posts: 23006
156
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's probably better that waym since you can thus compare for equality or identity, whichever your need,

Regardless of the method name, though the assertion does exist, though, so to say jUnit doesn't handle it accurately doesn't make sense.
 
Stephan van Hulst
Saloon Keeper
Posts: 12608
273
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

tangara goh wrote:But, I find this fake "passed" can be very dangerous to people who are ignorant about it.


What fake "passed"? You want to see if the contents of two arrays are equal right? Then just don't use the != operator, because it doesn't compare array contents.

Therefore, I find JUnit itself is not 100% reliable....


Like Tim, I really don't understand what you mean by this. JUnit is fine, and should be your first choice for writing unit tests.

If your tests are unreliable, that's your fault, not JUnit's.
 
Marshal
Posts: 7933
548
Mac OS X VI Editor BSD Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

tangara goh wrote:But, I find this fake "passed" can be very dangerous to people who are ignorant about it.

Therefore, I find JUnit itself is not 100% reliable....


As been said already, it did what you asked it for. There might be well cases where you really want to check whether compared references are exact same instances, although much rarer.

There are tools which can advice you along the way about such developer mistakes. Such tools might be SonarCube/SonarCloud or simply an IDE extension as SonarLint, which would highlight it for you asking to verify your intention.
 
reply
    Bookmark Topic Watch Topic
  • New Topic