• 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

Help with testing basic String array whose enclosing method returns boolean.

 
Ranch Hand
Posts: 231
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello there,

I am trying to write a unit test for a helper class that was already pre-written (excuse me that development was not test first development).

Here is the particular helper class's code:



Am trying to write a JUnit test case for this method, here's what I've come up with so far:



Any help is greatly appreciated...>
 
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What's this "actual[i]" you're talking about?

You should be checking to see if the method returns true for every code group you send it--what else could you possibly do?
 
James Dekker
Ranch Hand
Posts: 231
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

David,

What's this "actual[i]" you're talking about?



I think this is the heart of my question...

Just used this as pseudo code.

Don't most test cases use this convention:

assertEquals(expected, actual)

You should be checking to see if the method returns true for every code group you send it...



Can you elaborate on this?

Given the code belonging to the helper class does anyone know of a good way to unit test this?

Many thanks for responding!
 
David Newton
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

James Dekker wrote:Don't most test cases use this convention:

assertEquals(expected, actual)


No, they use whatever they need to test. You *could* use assertEquals(isValidCode(group), true), but it's silly, sort of like return (foo == true) ? true : false.

Can you elaborate on this?


Not much to elaborate, but:Personally, I'd implement the code under test as a map, rather than a ginormous if statement. I'd also be pretty cautious about this--this is one of those kinds of methods where if there's suddenly another valid group, you won't necessarily know to test it. Whereas if the valid groups are in a map, you have a list of the valid groups you can use to test. Of course, then you're just testing the JRE's implementation of Map.contains(), which seems equally silly.
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
HI James,

"assertTrue()" doesn't have an "actual" argument -- it accepts a boolean, and the test passes if the boolean is true; so your assertTrue() call would just look like:

assertTrue("Code doesn't belong in group", MyHelper.isValidCode(validCodeGroup[i]);

There are a few other things you would test: first, test that a bunch of non-valid codes return false -- i.e., define another array "invalidCodeGroup" and then try

assertFalse("Code doesn't belong in group", MyHelper.isValidCode(invalidCodeGroup[i]);

I'd also test lower-case valid codes, to make sure the method returns "true" for them too.

Finally, you'll probably want to test what happens if you pass in an empty string (should return false) and what happens if you pass in null (should return false, I'd say, but the actual method will throw a NullPointerException, which I think should be fixed.)
 
David Newton
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ernest Friedman-Hill wrote:the actual method will throw a NullPointerException, which I think should be fixed.)


I'm not totally sure I agree on that--it depends on how the method is to be used.

If it's expected to always receive a non-null then an NPE may be appropriate, as it would indicate a programming error.
 
Well behaved women rarely make history - Eleanor Roosevelt. tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic