aspose file tools*
The moose likes Testing and the fly likes method names in jUnit Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Engineering » Testing
Bookmark "method names in jUnit" Watch "method names in jUnit" New topic
Author

method names in jUnit

pramod talekar
Ranch Hand

Joined: Apr 26, 2010
Posts: 316

Hello,

While testing the code using jUnit, if I change the method name, then jUnit skips that method.

Below is my code


and this class extends from the above.


Here, if I change the method name from testParseMultipleLines() to TestParseMultipleLines(), then jUnit skips this method.
I wonder why it is accepting anything that starts only with test*** and NOT Test****.
I haven't declared anywhere that it should test methods that begin with test and NOT Test.

Please advise.

Thanks.>


Thanks,
Pramod
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

Prior to Junit4, test methods must begin with "test", not "Test", neither "TEST". If you are using JUnit 4, you can name it whatever you want, and annotate it with @Test.


[My Blog]
All roads lead to JavaRanch
pramod talekar
Ranch Hand

Joined: Apr 26, 2010
Posts: 316

Thanks Christophe.
pramod talekar
Ranch Hand

Joined: Apr 26, 2010
Posts: 316

Hello,

I'm testing the above class using two different test scenarios:

This is the base class


And this class extends from the base class :


If I try to parse a blank value as shown in testEmptyValues(), the test fails in jUnit.
So I changed my base class CsvParser with these modifications :



But this modification fails the other two methods in jUnit.
So is there any way I can employ both test cases so that it doesn't fail in jUnit ?

Thanks.>
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4366
    
    8

What parsing rules are you trying to apply? Split on a blank line or a comma? Or something more complicated? It's not immediately obvious just from the test cases what the full rules are supposed to be.

String.split() takes a regular expression, so you can use that to split on multiple characters.
pramod talekar
Ranch Hand

Joined: Apr 26, 2010
Posts: 316

Hi Matthew,

My case is sometimes a user might forget to enter something in which case a logic based on String.split() should be used otherwise String Tokenizer should be used which will split it based on whitespaces.
I don't want to show something like "A field can't be left blank" error message to user, but need to continue with whatever a user has keyed in.
I'm new to jUnit so not aware of any special feature available in it.

Thanks for the response.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38393
    
  23
I think this thread would sit better on our testing forum. Moving.
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4366
    
    8

But that's not the problem you've got here. The real difference between the two cases is not that a value is empty - it's that the first two use a space delimiter, and the last one uses a comma delimiter. Are you saying you want to break on either of them?

Try this:

Both StringTokenizer and String.split() are capable of splitting on any characters you want, so there's no need to switch between them for different cases. The issue isn't with JUnit either.
pramod talekar
Ranch Hand

Joined: Apr 26, 2010
Posts: 316

Hey Matthew,

Thanks. This line worked


But I've a doubt.

If I write this, the test fails :


The above code causes the third method to fail.



My case is something like If the user doesn't enter anything, a blank should be taken in. and parser should act accordingly.

In this case, expected is blank but actual is 555-9999.>
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4366
    
    8

StringTokenizer skips empty tokens, so two spaces together work the same as a single space (if you're splitting on whitespace). You could get the effect you want by creating a StringTokenizer with the returnDelims flag set to true (but then you'd have to make sure you skip the delimiters). But I'd suggest just sticking with String.split().
pramod talekar
Ranch Hand

Joined: Apr 26, 2010
Posts: 316

Thanks Matthew.

That helped a lot.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: method names in jUnit