Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 723
YaMaisiukDranko Avatar asked Oct 27 '25 03:10

YaMaisiukDranko


1 Answers

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,00\u00A0Br", 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)

like image 153
George Avatar answered Oct 29 '25 19:10

George



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!