2015-11-03 18 views
0

有沒有辦法將JUnit配置爲在顯示錯誤消息時以十六進制打印整數?在JUnit失敗消息中使用十六進制

例如,有沒有辦法讓JUnit來顯示該

Failure: does_not_touch_other_bits(PackedArrayTest): expected:<0xabcd1234> but was:<0> 
Tests run: 1, Failures: 1 

,而不是這個

Failure: does_not_touch_other_bits(PackedArrayTest): expected:<-1430689110> but was:<0> 
Tests run: 1, Failures: 1 

回答

2

不,你不能配置JUnit來打印整數作爲十六進制值。

但是你可以編寫自己的assertEqualsHex(int expected, int value)方法。

另一種可能是你使用這個assertMethod:

public static void assertEquals(String message, Object expected, 
      Object actual) 

,並提供在消息參數預期的十六進制值。

+0

此外,使用Assertion框架(Hamcrest,AssertJ,Truth等)併爲此編寫自定義匹配器將是一個想法,但可能是矯枉過正。 –