Search code examples
java-money

No MonetaryAmountsSingletonSpi loaded


Dependency added in pom.xml:

<dependency>
    <groupId>javax.money</groupId>
    <artifactId>money-api</artifactId>
    <version>1.0.3</version>
</dependency>

Relevant code:

MonetaryAmount mon = Monetary.getDefaultAmountFactory()
                        //getCurrency() returns String
                        .setCurrency(amount.getCurrency())
                        //getContent() returns BigDecimal
                        .setNumber(amount.getContent())
                        .create();

when unittesting i get this exception:

javax.money.MonetaryException: No MonetaryAmountsSingletonSpi loaded.

at javax.money.Monetary.lambda$getDefaultAmountFactory$13(Monetary.java:291)

Solution

  • JavaMoney is split into separate packages for API and implementation, but you added only the API to your POM. You also need an implementation, e.g. the reference implementation:

    <dependency>
      <groupId>org.javamoney</groupId>
      <artifactId>moneta</artifactId>
      <version>1.2.1</version>
      <type>pom</type>
    </dependency>