dan*_*iol 8 java junit matcher junit4 assertthat
我有一个提供Restriction-object 的特定方法(在哪里Restriction是一个接口).由于它的实现已经是测试,我只想测试我的方法是否实际上提供了一个对象RestrictionImpl.
我看到有一些我可以assertThat和它一起使用的匹配器,我想,匹配器isA是想要完成这项任务的东西.
简化我的代码看起来像这样:
public static Restriction getRestriction() {
return new RestrictionImpl();
}
Run Code Online (Sandbox Code Playgroud)
我的测试看起来像那样;
@Test
public void getRestriction_returnsRestrictionImpl() {
assertThat(getRestriction(), isA(RestrictionImpl.class));
}
Run Code Online (Sandbox Code Playgroud)
但是这不会编译.我所能做的就是测试,如果a RestrictionImpl是Restriction......但是没有必要这样做.
我误解了目的isA吗?它的意义是什么?
更新:
使用assertThat(getRestriction(), is(instanceOf(RestrictionImpl.class)))会工作,但我认为这isA是一个快捷方式.以我想要的方式
打电话assertThat需要它有签名assertThat(T, Matcher<? extends T>),但它的签名是assertThat(T, Matcher<? super T>)
我发现了一个描述我的问题的问题:https:
//github.com/hamcrest/JavaHamcrest/issues/27
看起来isA这个版本的junit中只有错误的签名.它意味着快捷方式is(isIntanceOf(...)),但事实并非如此.
| 归档时间: |
|
| 查看次数: |
5223 次 |
| 最近记录: |