File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Testing and the fly likes JUnit test of my regex constructor Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » Testing
Reply locked New topic
Author

JUnit test of my regex constructor

Ivan Turner
Ranch Hand

Joined: Feb 27, 2012
Posts: 37
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

Joined: Mar 10, 2008
Posts: 1666
    
    7

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.


Build a man a fire, and he'll be warm for a day. Set a man on fire, and he'll be warm for the rest of his life.
Jelle Klap
Bartender

Joined: Mar 10, 2008
Posts: 1666
    
    7

Closed as duplicate of http://www.coderanch.com/t/572312/java/java/regex-JUnit-test-constructor
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JUnit test of my regex constructor
 
Similar Threads
Regex for email verifying
TreeSet contains "something like this"
Please tell me whether this procedure is correct or not.
Replace text by HTML links
How to find if there is a number in a string using regular expressions.