• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

regex and JUnit test of constructor  RSS feed

 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • 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: 21424
94
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • 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: 3752
62
Chrome Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Duplicate: http://www.coderanch.com/t/572304/Testing/JUnit-test-regex-constructor
 
Rob Spoor
Sheriff
Posts: 21424
94
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • 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: http://www.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
  • 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: 21424
94
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • 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.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!