Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
    Bookmark Topic Watch Topic
  • New Topic

JUnit test of my regex constructor

 
Ivan Turner
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • 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.
 
Jelle Klap
Bartender
Posts: 1952
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • 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
  • Report post to moderator
Closed as duplicate of http://www.coderanch.com/t/572312/java/java/regex-JUnit-test-constructor
 
    Bookmark Topic Watch Topic
  • New Topic