java.util.Optional 是否有任何 Hamcrest 匹配器?
Is there any Hamcrest Matcher for java.util.Optional?
我正在寻找一个 Hamcrest 匹配器来单元测试 return 一种 java.util.Optional 类型的方法。类似于:
@Test
public void get__Null(){
Optional<Element> element = Element.get(null);
assertThat( sasi , isEmptyOptional());
}
@Test
public void get__GetCode(){
Optional<Element> element = Element.get(MI_CODE);
assertThat( sasi , isOptionalThatMatches(allOf(hasproperty("code", MI_CODE),
hasProperty("id", notNullValue())));
}
是否有任何可用的实现抛出 Maven 存储库?
目前我掌握的信息如下:
目前 Java Hamcrest 使用 1.6 版本,并与许多使用旧版本 Java 的项目集成。
因此与Java 8 相关的功能将在与Java 8 兼容的未来版本中添加。提出的解决方案是有一个扩展库支持,这样有需要的人都可以使用扩展库。
我是 Hamcrest Optional 的作者,它现在可以在 Maven Central 上找到。
示例:检查 Optional 是否包含以某个值开头的字符串
import static com.github.npathai.hamcrestopt.OptionalMatchers.hasValue;
import static org.hamcrest.Matchers.startsWith;
Optional<String> optional = Optional.of("dummy value");
assertThat(optional, hasValue(startsWith("dummy")));
直到它进入 Hamcrest,或者如果您不能添加外部库。如果您不介意添加 class,则以下内容适用于空的可选
class EmptyOptionalMatcher<T> extends BaseMatcher<Optional<T>> {
private Optional<T> optionalActual;
public EmptyOptionalMatcher() {
}
@Override
public boolean matches(Object item) {
optionalActual = (Optional<T>) item;
if (optionalActual == null) {
return false;
}
boolean empty = !optionalActual.isPresent();
return empty;
}
@Override
public void describeTo(Description description) {
description.appendText("optional is empty");
}
@Override
public void describeMismatch(Object item, Description description) {
if (optionalActual == null) {
description.appendText(" optional was NULL?");
} else {
description.appendText(" was: " + optionalActual.get());
}
}
}
然后有一个匹配器助手或通用 class 以及您可以导入和使用的静态方法:
public static <T> Matcher<? super Optional<T>> emptyOptional() {
return new EmptyOptionalMatcher<>();
}
用法为:
assertThat(someOptional, is(emptyOptional()));
或阴性测试为
assertThat(someOptional, is(not(emptyOptional())));
Narendra Pathai 的 Hamcrest Optional 确实做得很好。
import static com.github.npathai.hamcrestopt.OptionalMatchers.isEmpty;
import static com.github.npathai.hamcrestopt.OptionalMatchers.isPresent;
import static com.github.npathai.hamcrestopt.OptionalMatchers.isPresentAnd;
import static com.github.npathai.hamcrestopt.OptionalMatchers.isPresentAndIs;
import static org.hamcrest.Matchers.startsWith;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
@Test
public void testOptionalValue() {
Optional<String> option = Optional.of("value");
assertTrue(option.isPresent()); // the old-fashioned, non-diagnosable assertion
assertThat(option, isPresent());
assertThat(option, isPresentAndIs("value"));
assertThat(option, isPresentAnd(startsWith("v")));
assertThat(option, isEmpty()); // fails
}
上面的最后一个断言失败并产生了很好的可诊断消息:
java.lang.AssertionError:
Expected: is <Empty>
but: had value "value"
在 Maven 上可用:
<dependency>
<groupId>com.github.npathai</groupId>
<artifactId>hamcrest-optional</artifactId>
<version>2.0.0</version>
<scope>test</scope>
</dependency>
如果您只想验证某个对象的可选字段 present/not 存在,您可以使用以下习惯用法:
预期对象:
public class BusinessObject {
private Long id;
private String name;
private Optional<AnotherObject> anotherObject;
}
Hamcrest 测试如下所示:
assertThat("BusinessObject is not as expected", businessObject, allOf(
hasProperty("id", equalTo(1L)),
hasProperty("name", equalTo("Some title")),
hasProperty("anotherObject", hasProperty("present", equalTo(true)))
));
我正在寻找一个 Hamcrest 匹配器来单元测试 return 一种 java.util.Optional 类型的方法。类似于:
@Test
public void get__Null(){
Optional<Element> element = Element.get(null);
assertThat( sasi , isEmptyOptional());
}
@Test
public void get__GetCode(){
Optional<Element> element = Element.get(MI_CODE);
assertThat( sasi , isOptionalThatMatches(allOf(hasproperty("code", MI_CODE),
hasProperty("id", notNullValue())));
}
是否有任何可用的实现抛出 Maven 存储库?
目前我掌握的信息如下:
目前 Java Hamcrest 使用 1.6 版本,并与许多使用旧版本 Java 的项目集成。
因此与Java 8 相关的功能将在与Java 8 兼容的未来版本中添加。提出的解决方案是有一个扩展库支持,这样有需要的人都可以使用扩展库。
我是 Hamcrest Optional 的作者,它现在可以在 Maven Central 上找到。
示例:检查 Optional 是否包含以某个值开头的字符串
import static com.github.npathai.hamcrestopt.OptionalMatchers.hasValue;
import static org.hamcrest.Matchers.startsWith;
Optional<String> optional = Optional.of("dummy value");
assertThat(optional, hasValue(startsWith("dummy")));
直到它进入 Hamcrest,或者如果您不能添加外部库。如果您不介意添加 class,则以下内容适用于空的可选
class EmptyOptionalMatcher<T> extends BaseMatcher<Optional<T>> {
private Optional<T> optionalActual;
public EmptyOptionalMatcher() {
}
@Override
public boolean matches(Object item) {
optionalActual = (Optional<T>) item;
if (optionalActual == null) {
return false;
}
boolean empty = !optionalActual.isPresent();
return empty;
}
@Override
public void describeTo(Description description) {
description.appendText("optional is empty");
}
@Override
public void describeMismatch(Object item, Description description) {
if (optionalActual == null) {
description.appendText(" optional was NULL?");
} else {
description.appendText(" was: " + optionalActual.get());
}
}
}
然后有一个匹配器助手或通用 class 以及您可以导入和使用的静态方法:
public static <T> Matcher<? super Optional<T>> emptyOptional() {
return new EmptyOptionalMatcher<>();
}
用法为:
assertThat(someOptional, is(emptyOptional()));
或阴性测试为
assertThat(someOptional, is(not(emptyOptional())));
Narendra Pathai 的 Hamcrest Optional 确实做得很好。
import static com.github.npathai.hamcrestopt.OptionalMatchers.isEmpty;
import static com.github.npathai.hamcrestopt.OptionalMatchers.isPresent;
import static com.github.npathai.hamcrestopt.OptionalMatchers.isPresentAnd;
import static com.github.npathai.hamcrestopt.OptionalMatchers.isPresentAndIs;
import static org.hamcrest.Matchers.startsWith;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
@Test
public void testOptionalValue() {
Optional<String> option = Optional.of("value");
assertTrue(option.isPresent()); // the old-fashioned, non-diagnosable assertion
assertThat(option, isPresent());
assertThat(option, isPresentAndIs("value"));
assertThat(option, isPresentAnd(startsWith("v")));
assertThat(option, isEmpty()); // fails
}
上面的最后一个断言失败并产生了很好的可诊断消息:
java.lang.AssertionError:
Expected: is <Empty>
but: had value "value"
在 Maven 上可用:
<dependency>
<groupId>com.github.npathai</groupId>
<artifactId>hamcrest-optional</artifactId>
<version>2.0.0</version>
<scope>test</scope>
</dependency>
如果您只想验证某个对象的可选字段 present/not 存在,您可以使用以下习惯用法:
预期对象:
public class BusinessObject {
private Long id;
private String name;
private Optional<AnotherObject> anotherObject;
}
Hamcrest 测试如下所示:
assertThat("BusinessObject is not as expected", businessObject, allOf(
hasProperty("id", equalTo(1L)),
hasProperty("name", equalTo("Some title")),
hasProperty("anotherObject", hasProperty("present", equalTo(true)))
));