JUnit - Comparative Error on Formatted String

My testing of the modules failed with the following error. Is there a way to compare softer ones to pass a test for such problems?

I do not want to specify DecimalFormatter in more detail, but to make the test more forgiving.

junit.framework.ComparisonFailure: expected:<[-]0.31 mm> but was:<[−]0.31 mm> 

Security Code

 public void testCode() { Locale.setDefault(Locale.US); assertEquals("-0.31 mm", codeUnderTest(-0.000314d); } 

If the code generating the string is

 private static final int OFFSET_FRACTION_DIGITS_SI = 2; private static final double UNIT_MULTIPLIER_SI = 1000d; // 1 m in mm private static final String UNIT_MM = "mm"; public String codeUnderTest(double value) { DecimalFormat df = new DecimalFormat(); df.setMaximumFractionDigits(OFFSET_FRACTION_DIGITS_SI); df.setMinimumFractionDigits(OFFSET_FRACTION_DIGITS_SI); value *= UNIT_MULTIPLIER_SI; StringBuilder builder = new StringBuilder(); builder.append(df.format(value)); builder.append(" "); builder.append(UNIT_MM); return builder.toString(); } 

Update:

More testing showed that the Unicode point returned by DecimalFormat is the Unicode character "MINUS SIGN" (U + 2212), and the character specified as expected is the Unicode character "HYPHEN-MINUS" (U + 002D). Therefore, I am looking for a way to match such characters with a single unicode point. The same applies to the thousandth separator, which will be the Unicode character "NO-BREAK SPACE" (U + 00A0) as opposed to the regular Unicode character "SPACE" (U + 0020).

+5
source share
1 answer

Perhaps try assertTrue ():

 public void testCode() { assertTrue(codeUnderTest(-0.000314d).equals("-0.31 mm")); } 
-1
source

All Articles