javatestngutf8-decode

TestNG is unable to print Arabic Characters


I have to print arabic string (I'm using TestNG framework). My code is like this:

    @Test
    public void test() throws UnsupportedEncodingException{
        String countryString = "المملكة العربية السعودية";
        String utfCountryString = new String(countryString.getBytes(), "utf-8");
        System.out.println("UTF String : "+utfCountryString);
        System.out.println("Original String : "+countryString);
    }

But when I run it using TestNG, I'm getting the following Output:

UTF String : ??????? ??????? ????????
Original String : ??????? ??????? ????????
PASSED: test

===============================================
Default test
Tests run: 1, Failures: 0, Skips: 0
===============================================

However, the output is perfectly fine if I run it under main() method or use jUnit.

Please tell me how can I run it using TestNG. Thanks.


Solution

  • Problem occurs when file encoding to the java compiler is not defined for UTF-8 encoding. As you mentioned you are using Ant as build tool, define the following encoding in build.xml file.

    <javac ... encoding="UTF-8" ... />
    

    Tested and working fine for me.