Junit - Test fails on French or German string assertion - The Koch Family
The Koch Family The Koch Family

Latest news

جاري التحميل ...

Junit - Test fails on French or German string assertion

In my previous post about building a regex to check a text without special characters but allow German and French. I met a problem that the unit test works fine on my machine using Eclipse, but it was fail when running on Jenkins' build job.

Here is my test:

public void shouldAllowFrenchAndGermanCharacters(){
String source = "ÄäÖöÜüß áÁàÀâÂéÉèÈêÊîÎçÇ";

Production code:

public static boolean isExistNotAllowedCharacters(String source){  
Pattern regex = Pattern.compile("^[a-zA-Z_0-9_ÄäÖöÜüß áÁàÀâÂéÉèÈêÊîÎçÇ]*$");
Matcher matcher = regex.matcher(source);
return !matcher.matches();

The result likes the following:
Failed tests:
SpecialCharactersUtilsTest.shouldAllowFrenchAndGermanCharacters:32 null

A guy from stackoverflow.com says:

"This is probably due to the default encoding used for your Java source files. The ö in the string literal in the JUnit source code is probably being converted to something else when the test is compiled. To avoid this, use Unicode escapes (\uxxxx) in the string literals in your JUnit source code"

So, I tried to find what and where exactly  the \uxxxx is. The answer they are Unicode character codes, and they could be easy to find. The following is an example:


I changed the function to use Unicode characters instead:

public static boolean isExistSpecialCharater(String source){
Pattern regex = Pattern.compile("^[a-zA-Z_0-9_\u00c4\u00e4\u00d6\u00f6\u00dc\u00fc\u00df\u00e0\u00c0\u00e1\u00c1\u00e2\u00c2\u00e9\u00c9\u00e8\u00c8\u00ea\u00ca\u00ee\u00ce\u00e7\u00c7\u0020\u0027]*$");
Matcher matcher = regex.matcher(source);
return !matcher.matches();


And, modified the test case also:

public void shouldAllowFrenchCharacters(){
String source = "\u00e0\u00c0\u00e1\u00c1\u00e2\u00c2\u00e9\u00c9\u00e8\u00c8\u00ea\u00ca\u00ee\u00ce\u00e7\u00c7\u0020\u0027";

Yeah, it works. Besides, I have already made it by writing an automation test with Selenium to make sure that it can also work on GUI as my expectation.

[1]. http://stackoverflow.com/questions/4237581/comparing-unicode-characters-in-junit
[2]. http://www.widecodes.com/0zxqPkPkej/junit-fails-on-french-string-assertion.html


If you like the content of our blog, we hope to stay in constant communication, just enter your email to subscribe to the blog's express mail to receive new blog updates, and you can send a message by clicking on the button next ...

إتصل بنا

About the site

author The Koch Family <<  Welcome! I'm so glad that you stopped by Your Modern Family blog. Together, we will talk about raising kids, organizing the home and saving money! and Tips & tricks and more…

< Learn more ←

Blog stats

Sparkline 2513183

All Copyrights Reserved

The Koch Family