File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes regex and JUnit test of constructor Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "regex and JUnit test of constructor" Watch "regex and JUnit test of constructor" New topic
Author

regex and JUnit test of constructor

Ivan Turner
Ranch Hand

Joined: Feb 27, 2012
Posts: 37
Hi, I'm trying to test my constructor. I've tried different asserts but the constructor test won't pass successfully.
This is the JUnit 4 module.

Here's the code being tested.

Thanks in advance
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19543
    
  16

You're comparing a String to a Pattern object. That's not going to work. Instead compare the String to the Pattern's pattern:


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Martin Vajsar
Sheriff

Joined: Aug 22, 2010
Posts: 3436
    
  47

Rob Spoor wrote:You're comparing a String to a Pattern object. That's not going to work. Instead compare the String to the Pattern's pattern:

Wouldn't assertEquals be better in this situation?
Jelle Klap
Bartender

Joined: Mar 10, 2008
Posts: 1666
    
    7

Duplicate: http://www.coderanch.com/t/572304/Testing/JUnit-test-regex-constructor

Build a man a fire, and he'll be warm for a day. Set a man on fire, and he'll be warm for the rest of his life.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19543
    
  16

Martin Vajsar wrote:
Rob Spoor wrote:You're comparing a String to a Pattern object. That's not going to work. Instead compare the String to the Pattern's pattern:

Wouldn't assertEquals be better in this situation?

Yes it would. Reference equality isn't what needs to be checked.


I'll let you decide which of the two to close.
Ivan Turner
Ranch Hand

Joined: Feb 27, 2012
Posts: 37
Thank you for the help. It worked for both assertEquals and assertSame but I realize assertEquals is the better choice from your posts. I'm new at this and I need to know what "compare the String to the Pattern's pattern" means. Is there another way to say that? From what I know it appears you took objRef.instanceVar.method() which is running the pattern() method on a variable declared as Pattern. Did this method convert the declared Pattern variable to a String?

I believe I've got it. In my String, a regex pattern is stored as a String. A Pattern is not a String! To get the Pattern's variable to show it's pattern, use the pattern() method and that allows for the comparison. Thanks again. You're wonderful!!
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19543
    
  16

Ivan Turner wrote:I believe I've got it. In my String, a regex pattern is stored as a String. A Pattern is not a String! To get the Pattern's variable to show it's pattern, use the pattern() method and that allows for the comparison.

Exactly!

Thanks again. You're wonderful!!

I know, I know And you're welcome of course.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: regex and JUnit test of constructor
 
Similar Threads
JUnit test of my regex constructor
Regex for email verifying
TreeSet contains "something like this"
Please tell me whether this procedure is correct or not.
How to find if there is a number in a string using regular expressions.