Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java Introduction to Functional Programming Functional Programming Fundamentals Higher Order Function for Price Conversion

Closure Java Challenge is broken

Unit tests in this challenge are broken. See below.. please fix.

As you can see the formatter in the createPriceConverter method can not possibly return the format required by the unit test. You would have to manually delete all possible Currency symbols for all possible locales after the format is created like this:

.replace("\u20ac\u00a0", "")
.replace("$\u00a0", "$")
// etc.
$ gradle prepareSubmission

> Task :test FAILED

com.teamtreehouse.challenges.homes.MainTest > creatingPriceConverterWorksForAllCountries FAILED
    java.lang.AssertionError: Hmm...double check your 'createPriceConverter' method.  I'm getting different results
    Expected: a string containing "1,00 (Euro)"
         but: was "1,00 € (Euro)"
        at org.hamcrest.MatcherAssert.assertThat(MatcherAssert.java:20)
        at org.junit.Assert.assertThat(Assert.java:865)
        at com.teamtreehouse.challenges.homes.MainTest.creatingPriceConverterWorksForAllCountries(MainTest.java:113)

com.teamtreehouse.challenges.homes.MainTest > argentinaRecordsAreFormattedAndConverted FAILED
    java.lang.AssertionError:
    Expected: is "First is $1.00 (USD) which is $15,48 (Argentine Peso)"
         but: was "First is $1.00 (USD) which is $ 15,48 (Argentine Peso)"
        at org.hamcrest.MatcherAssert.assertThat(MatcherAssert.java:20)
        at org.junit.Assert.assertThat(Assert.java:865)
        at org.junit.Assert.assertThat(Assert.java:832)
        at com.teamtreehouse.challenges.homes.MainTest.argentinaRecordsAreFormattedAndConverted(MainTest.java:124)

13 tests completed, 2 failed

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':test'.
> There were failing tests. See the report at: file:///Users/kerby/Downloads/lcc-java-fp-houses-1.1.0-CLOSURE/build/reports/tests/test/index.html

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.

* Get more help at https://help.gradle.org

BUILD FAILED in 2s
4 actionable tasks: 2 executed, 2 up-to-date

3 Answers

Here is the hack to pass the Unit Tests anyways: use .replace() on the return values of both the createPriceConverter() and the getPriceConversionForRecord() methods like this:

  public static Function<Integer, String> createPriceConverter(Locale locale, BigDecimal usdRate) {
    // TODO:  Examine the hardcoded `getArgentinaPriceConverter` method
    // TODO:  Using the arguments passed into this method return a function that will work for any locale and rate
    return usdPrice -> String.format("%s (%s)",
      NumberFormat.getCurrencyInstance(locale).format(usdRate.multiply(BigDecimal.valueOf(usdPrice))),
      Currency.getInstance(locale).getDisplayName()).replace("\u00a0\u20ac ", " ");

  }


  public static String getPriceConversionForRecord(HousingRecord record,
                                                   Function<Integer, String> priceConverter) {
    NumberFormat usFormatter = NumberFormat.getCurrencyInstance(Locale.US);
    return String.format("%s is %s (USD) which is %s",
      record.getRegionName(),
      usFormatter.format(record.getCurrentHomeValueIndex()),
      priceConverter.apply(record.getCurrentHomeValueIndex())
    ).replace("$\u00a0", "$");
  }
Andre Kucharzyk
Andre Kucharzyk
4,479 Points

What's interesting to me is the reason why it's broken, cause I don't believe that teacher didn't test the exercise before posting it (for me the exercise is also broken).

Hi lukej and Andre Kucharzyk. This is very strange. I've just completed this challenge (which passed) and the test runs fine. My code is the same as lukej's (minus the replace() part of course).