Skip to content
Advertisement

Kotlin Android Studio: org.junit.ComparisonFailure with NBSP

I have this code

@Test
fun price_twelve_cupcakes() {
    val viewModel = OrderViewModel()
    viewModel.setQuantity(12)
    viewModel.price.observeForever {}
    assertEquals("$27,00", viewModel.price.value)
}

And I get this Error

And This Comparison Failure

Advertisement

Answer

I encountered the same issue with the NBSP sign! Spent some time to figure out what to do with the tests

TL;DR:

  1. Pass locale in your string formatters
  2. For tests of locales that use NBSP instead of a space (French, Ukrainian etc.) paste the unicode version of NBSP (u00A0) in your test string

So

assertEquals("27,00 Br", priceToCheck)

should become

assertEquals("27,00u00A0Br", priceToCheck)

Also note that different locales not only have different grouping sign (comma, dot, space, NBSP etc.) but also a different cents separator (comma, dot)

It’s caused by Locale’s string formatting settings and involves DecimalFormat, String.format, NumberFormat.getCurrencyInstance() If you want your string to be formatted in a stable way no matter what default locale is set, you can use either DecimalFormat(pattern of choice) or pass the Locale you want (say Locale(“en”,”IE”) for Ireland)

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement