Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

method names in jUnit

 
pramod talekar
Ranch Hand
Posts: 323
Eclipse IDE Java Opera
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.>
 
Christophe Verré
Sheriff
Pie
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
pramod talekar
Ranch Hand
Posts: 323
Eclipse IDE Java Opera
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Christophe.
 
pramod talekar
Ranch Hand
Posts: 323
Eclipse IDE Java Opera
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4549
8
Java Netbeans IDE Scala
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 323
Eclipse IDE Java Opera
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 47293
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think this thread would sit better on our testing forum. Moving.
 
Matthew Brown
Bartender
Posts: 4549
8
Java Netbeans IDE Scala
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 323
Eclipse IDE Java Opera
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4549
8
Java Netbeans IDE Scala
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 323
Eclipse IDE Java Opera
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Matthew.

That helped a lot.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic