File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Testing and the fly likes Help with testing basic String array whose enclosing method returns boolean. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Engineering » Testing
Bookmark "Help with testing basic String array whose enclosing method returns boolean." Watch "Help with testing basic String array whose enclosing method returns boolean." New topic
Author

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

James Dekker
Ranch Hand

Joined: Dec 09, 2006
Posts: 215
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...>
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

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

Joined: Dec 09, 2006
Posts: 215

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
Rancher

Joined: Sep 29, 2008
Posts: 12617

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.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

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.)


[Jess in Action][AskingGoodQuestions]
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Help with testing basic String array whose enclosing method returns boolean.
 
Similar Threads
How to use boolean?
Class and Object Problem
Byte array output from KeyPairGenerator
populating the data from array list
Overloaded methods