Java JUnit最大/最小值

Java JUnit最大/最小值,java,testing,junit,Java,Testing,Junit,如何从映射值测试Bigdecimal最大值 我最初使用对象映射器映射字符串 字符串包含特定货币的日期和汇率 我想创建一个测试max/min值的方法 了解测试LocalDate的方法会很好 Map.Entry<LocalDate, BigDecimal> getMinRate(Response response) { return Collections.min(response.getBpi().entrySet(), Map.Entry.comparingByVal

如何从映射值测试Bigdecimal最大值

我最初使用对象映射器映射字符串

字符串包含特定货币的日期和汇率

我想创建一个测试max/min值的方法

了解测试LocalDate的方法会很好

Map.Entry<LocalDate, BigDecimal> getMinRate(Response response) {
        return Collections.min(response.getBpi().entrySet(), Map.Entry.comparingByValue());
    }
Map.Entry getMinRate(响应){
返回Collections.min(response.getBpi().entrySet(),Map.Entry.comparingByValue());
}

我希望我能正确回答您的问题:

响应测试类

import org.junit.Test;
import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.HashMap;
import java.util.Map;
import static org.junit.Assert.assertEquals;

public class ResponseUtilTest {

    @Test
    public void bigDecimalIsMinimumAndLocalDateAreValid() {
        ResponseUtil responseUtil = new ResponseUtil();
        Map<LocalDate, BigDecimal> bpi = new HashMap<>();
        bpi.put(LocalDate.of(2018, 10, 23), BigDecimal.valueOf(10));
        bpi.put(LocalDate.of(2018, 10, 22), BigDecimal.valueOf(20));
        bpi.put(LocalDate.of(2018, 10, 21), BigDecimal.valueOf(30));

        Map.Entry<LocalDate, BigDecimal> minRate = responseUtil.getMinRate(new Response(bpi));

        assertEquals(LocalDate.of(2018, 10, 23), minRate.getKey());
        assertEquals(BigDecimal.valueOf(10), minRate.getValue());
    }
}
import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.Collections;
import java.util.Map;

public class ResponseUtil {
    Map.Entry<LocalDate, BigDecimal> getMinRate(Response response) {
        return Collections.min(response.getBpi().entrySet(), Map.Entry.comparingByValue());
    }
}
import org.junit.Test;
导入java.math.BigDecimal;
导入java.time.LocalDate;
导入java.util.HashMap;
导入java.util.Map;
导入静态org.junit.Assert.assertEquals;
公共类响应测试{
@试验
public void bigdecimiliminumandLocalDateAreValid(){
ResponseUtil ResponseUtil=新的ResponseUtil();
Map bpi=新的HashMap();
bpi.put(LocalDate.of(2018,10,23),BigDecimal.valueOf(10));
bpi.put(LocalDate.of(2018,10,22),BigDecimal.valueOf(20));
bpi.put(LocalDate.of(2018,10,21),BigDecimal.valueOf(30));
Map.Entry minRate=responseUtil.getMinRate(新响应(bpi));
assertEquals(LocalDate.of(2018,10,23),minRate.getKey());
assertEquals(BigDecimal.valueOf(10),minRate.getValue());
}
}
ResponseUtil.class

import org.junit.Test;
import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.HashMap;
import java.util.Map;
import static org.junit.Assert.assertEquals;

public class ResponseUtilTest {

    @Test
    public void bigDecimalIsMinimumAndLocalDateAreValid() {
        ResponseUtil responseUtil = new ResponseUtil();
        Map<LocalDate, BigDecimal> bpi = new HashMap<>();
        bpi.put(LocalDate.of(2018, 10, 23), BigDecimal.valueOf(10));
        bpi.put(LocalDate.of(2018, 10, 22), BigDecimal.valueOf(20));
        bpi.put(LocalDate.of(2018, 10, 21), BigDecimal.valueOf(30));

        Map.Entry<LocalDate, BigDecimal> minRate = responseUtil.getMinRate(new Response(bpi));

        assertEquals(LocalDate.of(2018, 10, 23), minRate.getKey());
        assertEquals(BigDecimal.valueOf(10), minRate.getValue());
    }
}
import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.Collections;
import java.util.Map;

public class ResponseUtil {
    Map.Entry<LocalDate, BigDecimal> getMinRate(Response response) {
        return Collections.min(response.getBpi().entrySet(), Map.Entry.comparingByValue());
    }
}
import java.math.BigDecimal;
导入java.time.LocalDate;
导入java.util.Collections;
导入java.util.Map;
公共类响应{
Map.Entry getMinRate(响应){
返回Collections.min(response.getBpi().entrySet(),Map.Entry.comparingByValue());
}
}

一句话中的问题看起来很好也很清楚,这里唯一的问题是我自己没有给出答案:(好的,我们都在学习,所以现在我有一个例子,我可以从那里拿出来,一个问题->如果我只想测试一个大十进制怎么办?我会简单地删除assert equals吗?也许单独测试它们更好,但是,我想,根据逻辑@I.bondarenkoSure,你可以删除LocalDate assert,只留下
assertEquals(BigDecimal.valueOf(10),minRate.getValue());
。您还应该将测试方法重命名为
void bigdecimalinimimimum()
Dude谢谢你,我现在写测试感觉更舒服了,非常感谢你,我为描述不清楚表示歉意没有问题,很高兴能帮助你!如果你对答案满意,请将其标记为解决方案:)当然,请看一下修改后的答案。我只使用java.base库。如果你希望我们可以继续聊天。