gpt4 book ai didi

java - JUnit 的 hamcrest 库是否有一个 API 来测试公共(public)变量而不是属性

转载 作者:行者123 更新时间:2023-12-01 10:54:45 25 4
gpt4 key购买 nike

我正在尝试测试具有特定值的公共(public)变量。问题是我正在测试公共(public)变量,而不是 JavaBean。

Assert.assertThat(clientMapper.entries, Matchers.<ClientMapper.MapperEntry>hasItem(
Matchers.allOf(
Matchers.hasProperty("from", Matchers.is("/v1")),
Matchers.hasProperty("to", Matchers.is("/v2"))
)
));

我正在测试的类(class)是

public static class MapperEntry {
public String from;
public String to;
}

测试失败,因为它没有找到 JavaBean 属性 fromto,这是可以理解的,但如何测试公共(public)字段?

更新:我正在尝试寻找 java 7 的解决方案

最佳答案

NitorCreations' matchers library有字段匹配器:

Assert.assertThat(clientMapper.entries, Matchers<ClientMapper.MapperEntry>hasItem(
Matchers.allOf(
com.nitorcreations.Matchers.hasField("from", Matchers.is("/v1")),
com.nitorcreations.Matchers.hasField("to", Matchers.is("/v2"))
)));

关于java - JUnit 的 hamcrest 库是否有一个 API 来测试公共(public)变量而不是属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33674130/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com