I am writing a program which converts the values of each currency to another using simple API. I had no trouble at all developing that simple program, but I am currently struggling in testing it. I am a 3 month programmer so don't blame me for code quality - give me tips for the future.

I have already written a test which checks if the conversion from each currency to another is correct, now I am looking for a way to test if a currency shortcut(for ex. EUR, PLN, JPY, AUD..) is correct, what I mean is that test should provide to me an information when a name (AUR or JPU) is not correct. I got a class "Rates" which contains values of JSON Properties of each currency.

package pl.sda.CurrencyConverter_RZ.model;

import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

public class Rates {

    private Double BGN;

    private Double NZD;

    private Double ILS;

    private Double RUB;

    private Double CAD;

    private Double USD;

    private Double PHP;

    private Double CHF;

    private Double AUD;

    private Double JPY;

    private Double TRY;

    private Double HKD;

    private Double MYR;

    private Double HRK;

    private Double CZK;

    private Double IDR;

    private Double DKK;

    private Double NOK;

    private Double HUF;

    private Double GBP;

    private Double MXN;

    private Double THB;

    private Double ISK;

    private Double ZAR;

    private Double BRL;

    private Double SGD;

    private Double PLN;

    private Double INR;

    private Double KRW;

    private Double RON;

    private Double CNY;

    private Double SEK;

    private Double EUR;

package pl.sda.CurrencyConverter_RZ.service;

import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
import pl.sda.CurrencyConverter_RZ.model.Currencies;

import java.math.BigDecimal;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertAll;
class CurrencyServiceTest {

    public static Currencies currencyValue = new Currencies();
    public static CurrencyService currencyService = new CurrencyService();

    static void setUp() {


    @DisplayName("If conversion is correct by multiplying values")
    void shouldReturnTrueByMultiplying() {
        BigDecimal fromPLNtoEUR = currencyService
                .convertCurrencyValue(1, "PLN", "EUR", "2019-04-20", 4);
        BigDecimal fromEURtoPLN = currencyService
                .convertCurrencyValue(1, "EUR", "PLN", "2019-04-20", 4);
        assertAll(() -> assertThat((fromPLNtoEUR.multiply(fromEURtoPLN)).setScale(0, BigDecimal.ROUND_HALF_DOWN))
                () -> assertThat((fromEURtoPLN.multiply(fromPLNtoEUR)).setScale(0, BigDecimal.ROUND_HALF_DOWN))

//    @ParameterizedTest   // <- to check if its needed
//    @CsvSource({"AUD", "JPY"})
    @DisplayName("Tests if shortcut of currency is correct")
    void shouldReturnTrueIfCurrencyShortcutIsCorrect(String currencyShortcut, String correctCurrencyShortcut) {

I am now wondering if I can somehow get those names from a class (only names, not values from JSON), rather that testing them one by one. Hope that someone understand what I am trying to say. Also if someone wouldn't mind giving me tips how to improve my 1st test would be awesome. It returns a BigDecimal value from each currency to another.

0 Answers