• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

regex and JUnit test of constructor

 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Sheriff
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're comparing a String to a Pattern object. That's not going to work. Instead compare the String to the Pattern's pattern:
 
Sheriff
Posts: 3837
66
Netbeans IDE Oracle Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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?
 
Bartender
Posts: 1952
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Duplicate: https://coderanch.com/t/572304/Testing/JUnit-test-regex-constructor
 
Rob Spoor
Sheriff
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Ivan Turner
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic