• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Junit test case for Swapping progam

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am new to Junit, i want to know how to write test case for swapping program. Below is a code for which i want o know junit tests case.

 
Bartender
Posts: 2236
63
IntelliJ IDE Firefox Browser Spring Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
First of all, do you know that your method does not actually swap passed values?
What is being swapped are local copies of those variables (named a and b).
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That method does not have any effects; if you look at it from the outside, it essentially does nothing. It doesn't return the values of the variables a and b, and since Java method calls are pass-by-value only, if you call this method with two variables then their values will not be swapped. You'll have to return the swapped values in some way.

Because the method doesn't have any effects that are visible outside of the method, it will not be possible to write a unit test for it, because there is nothing to check.
 
Sheriff
Posts: 5555
326
IntelliJ IDE Python Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I moved this into our Testing Forum.

However, Paweł and Jesper are right. Your method has no observable output so you cannot make any assertions as to the correct operation of it.
 
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As the the others said, your method has no side-effects to assert against. I.e. if we put stuff in we need to see stuff come out in order to check if it worked (unless you hooked into the System.out - which I assume isn't the intention).

So lets rewrite your method like this:



Now I might write a test something like this:



The method naming convention I used won't be to many people's taste (I copied it from Roy Osherove), but I think it's the best I've seen generally speaking.
It goes like this: UnitUnderTest_TestVariation_ExpectedResult.

I like to separate the setup, execution, and assertions with blank lines, as above.

You'll hear that it's good practise to only assert of one thing per test. In this case you'll see two assertions, but they are really a compound assertion of a single thing - the swap.

If you have Junit4 on the classpath, and the necessary imports, that test should pass. If you mess with the numbers it'll fail accordingly.
 
You learn how to close your eyes and tell yourself "this just isn't really happening to me." Tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic