Как написать контрольные примеры для этой услуги калькулятора оплаты с помощью Junit / Mockito? - PullRequest
0 голосов
/ 27 сентября 2018

У меня есть этот класс обслуживания -

package com.test.common.fee;

import java.io.File;
import java.io.IOException;
import java.math.BigDecimal;
import java.math.MathContext;

import javax.annotation.PostConstruct;

import org.codehaus.jackson.map.ObjectMapper;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

@Service
public class FeeCalcService {

    @Value("${json.config.folder}")
    String jsonConfigFolder;

    FeeConfigEntity feeConfig = new FeeConfigEntity();

    @PostConstruct
    public void init() throws IOException {
        ObjectMapper jsonMapper = new ObjectMapper();
        File jsonFile = getFilesInFolder(jsonConfigFolder);

        // deserialize contents of each file into an object of type
        feeConfig = jsonMapper.readValue(jsonFile, FeeConfigEntity.class);
    }

    public BigDecimal calculateFee(BigDecimal amount)
    {
        String type = feeConfig.getType();
        Integer total = feeConfig.getDetails().size();
        BigDecimal fee = new BigDecimal(0);
        if(type.equals("slab"))
        {
            if(total>1)
            {
                for(FeeConfigDetailsEntity eachSlab : feeConfig.getDetails()){
                    BigDecimal min = BigDecimal.valueOf(eachSlab.getDetails().getMin());
                    BigDecimal max = BigDecimal.valueOf(eachSlab.getDetails().getMax());
                    if((amount.compareTo(min) == 1 || amount.compareTo(min) == 0)
                            &&
                        (amount.compareTo(max) == -1 || amount.compareTo(min) == 0)
                    )
                    {
                        float value = eachSlab.getDetails().getValue();
                        if(eachSlab.getDetails().getType().equals("flat"))
                        {
                            fee = BigDecimal.valueOf(value);
                        }
                        else if(eachSlab.getDetails().getType().equals("fixed"))
                        {
                            MathContext mc = new MathContext(4); // 4 precision
                            fee = amount.multiply(BigDecimal.valueOf(value), mc).divide(BigDecimal.valueOf(100), mc);
                        }
                        break;
                    }
                }
            }
        }
        else if(total>1)
        {
            //incorrect setup
        }
        else
        {//expected flat/fixed
            float value = feeConfig.getDetails().get(0).getDetails().getValue();

            if(type.equals("flat"))
            {
                fee = BigDecimal.valueOf(value);
            }
            else if(type.equals("fixed"))
            {
                MathContext mc = new MathContext(4); // 4 precision
                fee = amount.multiply(BigDecimal.valueOf(value), mc).divide(BigDecimal.valueOf(100), mc);
            }
        }

        return fee;
    }

    /*public List<ContextOperatorBean> getMatchingOperators(String context) {
        return operators.stream().filter(operator -> checkIfMatches(operator, context)).collect(Collectors.toList());
    }

    private boolean checkIfMatches(ContextOperatorBean operator, String context) {
        // TODO implement
        return false;
    }*/

    private File getFilesInFolder(String path) {
        // TODO implement   
        File test = new File(path);
        return test;
    }
}

Функция init () загружает файл json этой структуры в класс FeeConfigEntity -

{
    "type": "slab",
    "details": [{
            "slabName": "slab_1",
            "details": {
                "min": 0,
                "max": 100,
                "type": "fixed",
                "value": "12"
            }
        },
        {
            "slabName": "slab_2",
            "details": {
                "min": 101,
                "max": null,
                "type": "flat",
                "value": "100"
            }
        }
    ]
}

Структура класса FeeConfigEntity -

package com.test.common.fee;

import java.util.List;

public class FeeConfigEntity {

    private String type;

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public List<FeeConfigDetailsEntity> getDetails() {
        return details;
    }

    public void setDetails(List<FeeConfigDetailsEntity> details) {
        this.details = details;
    }

    private List<FeeConfigDetailsEntity> details;

    }

По сути, это услуга, которая возвращает вывод с комиссией / комиссией, которая будет применяться к сумме, согласно структуре комиссий, определенной в файле json.

Это то, как я ее называюмое заявление -

BigDecimal fee = feeCalcService.calculateFee(amount);

Я очень плохо знаком с тестированием Junit и не понимаю, как именно это должно быть сделано.

Моя идея -

  • В структуру класса обслуживания необходимо внести некоторые изменения.
  • Функция init ничего не возвращает, поэтому я не могу поставить() thenReturn () здесь, иначе я мог бы переписать здесь то, что мне нужно.

Ответы [ 2 ]

0 голосов
/ 27 сентября 2018

По моему мнению, FeeCalcService следует протестировать с несколькими различными FeeConfigEntity сущностями.

Существуют разные способы достижения этого, например:

Добавить 2 конструктора в ваш класс:

public FeeCalcService() {
}

FeeCalcService(FeeConfigEntity feeConfig) {
    this.feeConfig = feeConfig;
}

Второй используется только для тестирования.

Затем напишите несколько тестов, подобных этому:

@Test
public void test1() {
    FeeConfigEntity config1 = new FeeConfigEntity();
    config1.setType(...);
    config1.setDetails(...);
    Assert.assertEquals(new BigDecimal(10), new FeeCalcService(config1).calculateFee(new BigDecimal(100)));
}

Mockito здесь не нужен.Это особенно полезно, когда некоторая часть обработки делегирована другому классу, но здесь это не так.

0 голосов
/ 27 сентября 2018

Создайте объект для FeeCalcService и вызовите init() с объектом.Затем вызовите calculateFee с тем же объектом, затем подтвердите фактическое значение с ожидаемым значением.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...