2012-10-30 126 views
2

我在UTF-8編碼文件中進行了以下junit測試。編碼測試在Ant中失敗,但在Eclipse中工作

public class EncodingTest { 

private static byte[] utf8Bytes; 

private static byte[] utf8Bytes2; 

private static byte[] utf8Bytes3; 

static { 
try { 
    utf8Bytes = "åæø".getBytes("UTF-8"); 
    utf8Bytes2 = new byte[] { (byte) 0xc3, (byte) 0xa5, (byte) 0xc3, (byte) 0xa6, (byte) 0xc3, (byte) 0xb8 }; 
    utf8Bytes3 = "\u00E5\u00E6\u00F8".getBytes("UTF-8"); 
} catch (final UnsupportedEncodingException e) { 
    assertTrue(false); 
} 

} 

@Test 
public void testUTF8Encoding1() throws UnsupportedEncodingException { 
assertTrue(Arrays.equals(utf8Bytes, utf8Bytes2)); 
} 

@Test 
public void testUTF8Encoding2() throws UnsupportedEncodingException { 
assertTrue(Arrays.equals(utf8Bytes2, utf8Bytes3)); 
} 

@Test 
public void testUTF8Encoding3() throws UnsupportedEncodingException { 
assertEquals(new String(utf8Bytes), new String(utf8Bytes2)); 
} 

@Test 
public void testUTF8Encoding4() throws UnsupportedEncodingException { 
assertEquals(new String(utf8Bytes2), new String(utf8Bytes3)); 
} 
} 

測試在ant中失敗,但在eclipse中工作。我使用的是Ant任務如下:

<target 
    name="test" 
    depends="compile-tests" 
    description="Test the full company tests" > 

    <junit fork="yes" haltonfailure="yes" > 
     <jvmarg value="-Dfile.encoding=UTF-8"/> 
     <classpath> 

      <path refid="test.classpath" /> 

      <fileset dir="war/WEB-INF/lib/" > 

       <include name="*.jar" /> 
      </fileset> 
     </classpath> 

     <formatter 
      type="plain" 
      usefile="false" /> 
     <!-- to screen --> 
     <!-- formatter type="plain"/--> 
     <!-- to file --> 

     <batchtest> 

      <fileset 
       dir="test/classes/" 
       includes="**/*Test.class" /> 
     </batchtest> 
    </junit> 
</target> 

輸出如下:

test: 
[junit] Testsuite: com.company.appengine.EncodingTest 
[junit] Tests run: 4, Failures: 2, Errors: 0, Time elapsed: 0,028 sec 
[junit] 
[junit] Testcase: testUTF8Encoding1 took 0,004 sec 
[junit]  FAILED 
[junit] 
[junit] junit.framework.AssertionFailedError: 
[junit]  at com.company.appengine.EncodingTest.testUTF8Encoding1(EncodingTest.java:42) 
[junit] 
[junit] Testcase: testUTF8Encoding2 took 0 sec 
[junit] Testcase: testUTF8Encoding3 took 0,001 sec 
[junit]  FAILED 
[junit] expected:<[åæø]> but was:<[åæø]> 
[junit] junit.framework.AssertionFailedError: expected:<[åæø]> but was:<[åæø]> 
[junit]  at com.company.appengine.EncodingTest.testUTF8Encoding3(EncodingTest.java:52) 
[junit] 
[junit] Testcase: testUTF8Encoding4 took 0 sec 

任何想法?

+0

你使用相同的JVM和配置與螞蟻和Eclipse? – logoff

+0

我怎麼知道哪個JVM使用? –

+0

奇怪的是,你得到了junit.framework.AssertionFailedError,但是你正在使用@Test。你應該包括org.junit.Assert,而不是junit.framework.Assert。 –

回答

2

請首先確保javac知道您的源文件使用utf-8編碼。如果使用javac task編譯,請提供encoding屬性。

+0

你是對的! –

相關問題