Forums Register Login

regex and JUnit test of constructor

+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
You're comparing a String to a Pattern object. That's not going to work. Instead compare the String to the Pattern's pattern:
+Pie Number of slices to send: Send
 

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?
+Pie Number of slices to send: Send
+Pie Number of slices to send: Send
 

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.

Jelle Klap wrote:Duplicate: https://coderanch.com/t/572304/Testing/JUnit-test-regex-constructor


I'll let you decide which of the two to close.
+Pie Number of slices to send: Send
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!!
+Pie Number of slices to send: Send
 

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.
That new kid is a freak. Show him this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 3250 times.
Similar Threads
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.
Regex for email verifying
pattern matches
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 18, 2024 20:25:53.