我正在編寫一個JUnit測試用例來比較兩個StringBuffers。 (該測試將檢查字符串逆轉。)JUnit在字符串和StringBuffer之間看到的不同行爲
@Test
public void testReverseString() {
StringBuffer orig = new StringBuffer("fedcba");
StringBuffer exp = new StringBuffer("abcdef");
// function will reverse the string so
// orig will be "abcdef" after the call
Question1_2.reverseString(orig);
//System.out.println(exp + " " + orig); // output - abcdef abcdef
//assertEquals(exp.toString(), orig.toString()); This Passes
//assertEquals(exp, orig); This FAILS
//assertTrue(exp.equals(orig)); This FAILS
}
按本JUnit to test for StringBuffers的assertEquals(EXP,原稿);應該管用。
assertEquals(exp.toString(), orig.toString()); This Passes
//assertEquals(exp, orig); This FAILS
//assertTrue(exp.equals(orig)); This FAILS
我不確定我是否誤解了一些基本的東西wrt String,StringBuffer和JUnit assertEquals。
感謝 阿米特
'Question1_2.reverseString(orig)'什麼是'Question1_2'這個??在節目中? –
Question1_2.reverseString(orig) - 它是我的類,用於反轉字符串..它只conatins邏輯扭轉字符串 – jaamit