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

JUnit test of my regex constructor

 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 1952
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Well, assertSame() asserts that two references point to the same object, and this clearly isn't the case, as one of them points to a String instance, and the other to a Pattern instance.
By the way the UsernameValidator is not thread-safe - if it is intended to be.
A simple solution would be to eagerly create a static final instance of Pattern, which is thread-safe, and create and keep a Matcher for that pattern in the scope of the validate() method, as Matcher is not thread-safe.
 
Jelle Klap
Bartender
Posts: 1952
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Closed as duplicate of https://coderanch.com/t/572312/java/java/regex-JUnit-test-constructor
 
Don't get me started about those stupid light bulbs.
    Bookmark Topic Watch Topic
  • New Topic