![](/img/trans.png)
[英]java.lang.AssertionError: java.lang.AssertionError: Expected :lastName is required Actual :loginName is required
[英]Hamcrest - Matchers.hasProperty: getting a weird Error : java.lang.AssertionError: No property "firstName" and No property "lastName"
我创建了一个类 Item,想检查 Item 类属性“firstName”是否为“test”,“lastName”是否为“best”,但出现以下错误。 我并没有真正明白这里出了什么问题。 我几乎不怀疑是否需要完成以下事情。
- 我是否需要覆盖 firstName 和 lastName 的相等方法。
- 是否有可能发生 jar 冲突
- 或者我错过了什么
提前致谢。
这是我的代码:
@Test
public void testName(){
List<Item> items = new ArrayList<Item>();
Item item1= new Item("test","best");
Item item2= new Item("test","best");
Item item3= new Item("test","best");
items = Arrays.asList(item1,item2,item3);
assertThat(items,hasItems(allOf(Matchers.<Item>hasProperty("firstName",is("test")),
Matchers.<Item>hasProperty("lastName",is("best")))));
}
类项目{
public Item(){}
public Item(String firstName,String lastName){
this.firstName=firstName;
this.lastName=lastName;
}
private String firstName;
private String lastName;
@Override
public boolean equals(Object obj) {
if (obj == null) {
return false;
}
if (obj.getClass() != this.getClass()) {
return false;
}
final Item other = (Item) obj;
if ((this.firstName == null) ? (other.firstName != null) : !this.firstName.equals(other.firstName)) {
return false;
}
return true;
}
@Override
public int hashCode() {
int hash = 3;
hash = 53 * hash + (this.firstName != null ? this.firstName.hashCode() : 0);
return hash;
}
}
我收到以下错误:
Expected: (a collection containing (hasProperty("firstName", is "test") and hasProperty("lastName", is "best")))
but: a collection containing (hasProperty("firstName", is "test") and hasProperty("lastName", is "best")) hasProperty("firstName", is "test") No property "firstName", hasProperty("firstName", is "test") No property "firstName", hasProperty("firstName", is "test") No property "firstName"
java.lang.AssertionError:
Expected: (a collection containing (hasProperty("firstName", is "test") and hasProperty("lastName", is "best")))
but: a collection containing (hasProperty("firstName", is "test") and hasProperty("lastName", is "best")) hasProperty("firstName", is "test") No property "firstName", hasProperty("firstName", is "test") No property "firstName", hasProperty("firstName", is "test") No property "firstName"
at org.hamcrest.MatcherAssert.assertThat(MatcherAssert.java:20)
at org.hamcrest.MatcherAssert.assertThat(MatcherAssert.java:8)
at ImportTest.testName(ImportTest.java:220)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at
我终于能够解决这个问题了。 问题是我在同一个类中创建了 Item 类并且它不是公开的。 我将我的 Item 类创建为一个单独的 Java 类并将其公开,它对我有用。
public class Item {
public Item(){}
public Item(String firstName,String lastName){
this.firstName=firstName;
this.lastName=lastName;
}
private String firstName;
private String lastName;
// here is the public getter and setters
@Override
public boolean equals(Object obj) {
if (obj == null) {
return false;
}
if (obj.getClass() != this.getClass()) {
return false;
}
final Item other = (Item) obj;
if ((this.firstName == null) ? (other.firstName != null) : !this.firstName.equals(other.firstName)) {
return false;
}
return true;
}
@Override
public int hashCode() {
int hash = 3;
hash = 53 * hash + (this.firstName != null ? this.firstName.hashCode() : 0);
return hash;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.