[英]Issues with JUnit Testing :: assertEquals()
I have the following classes but my jUnit assertEquals() testing is not working. 我有以下类,但是我的jUnit assertEquals()测试无法正常工作。 After days of searching and no answer I have decided to post here.
经过几天的搜索并且没有答案,我决定在这里发布。 Everything in the code seems right but it is still not working.
代码中的所有内容似乎都正确,但是仍然无法正常工作。
Contact class: 联系人类别:
@Embeddable
public class Contact {
@Column
private String name;
@Column
private String address;
@Column
private String phone;
public Contact(){
}
public Contact(String name, String address, String phone) {
super();
this.name = name;
this.address = address;
this.phone = phone;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String toString() {
return "Contact [name=" + name + ", address=" + address + ", phone=" + phone + "]";
}
@Override
public boolean equals(Object o){
if(this == o){
return true;
}
if(o == null){
return false;
}
if(this.getClass() != o.getClass()){
return false;
}
Contact contact = (Contact)o;
if(name != contact.getName()){return false;}
if(address != contact.getAddress()){return false;}
if(phone != contact.getPhone()){return false;}
return true;
}
@Override
public int hashCode() {
int result = name != null ? name.hashCode() : 0;
result = 31 * result + (address != null ? address.hashCode() : 0);
result = 31 * result + (phone != null ? phone.hashCode() : 0);
return result;
}
}
Order class: 订单类别:
@Entity
public class Orders {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Long id;
@Embedded
@AttributeOverrides({
@AttributeOverride(name="name", column=@Column(name="weekday_recipient")),
@AttributeOverride(name="address", column=@Column(name="weekday_address")),
@AttributeOverride(name="phone", column=@Column(name="weekday_phone")),
})
private Contact weekdayContact;
@Embedded
@AttributeOverrides({
@AttributeOverride(name="name", column=@Column(name="holiday_recipient")),
@AttributeOverride(name="address", column=@Column(name="holiday_address")),
@AttributeOverride(name="phone", column=@Column(name="holiday_phone"))
})
private Contact holidayContact;
public Orders() {
super();
}
public Long getId() {
return id;
}
public Contact getWeekdayContact() {
return weekdayContact;
}
public void setWeekdayContact(Contact weekdayContact) {
this.weekdayContact = weekdayContact;
}
public Contact getHolidayContact() {
return holidayContact;
}
public void setHolidayContact(Contact holidayContact) {
this.holidayContact = holidayContact;
}
public String toString() {
return "orders [WEEKDAY_CONTACT=" + this.getWeekdayContact().toString() +
", HOLIDAY_CONTACT=" + this.getHolidayContact().toString() + "]";
}
@Override
public boolean equals(Object o){
// self check
if(this == o){
return true;
}
// null check
if(o == null){
return false;
}
//type check and cast
if(getClass() != o.getClass()){
return false;
}
Orders orders = (Orders)o;
// field comparison
return Objects.equals(weekdayContact, orders.weekdayContact)
&&
Objects.equals(holidayContact, orders.holidayContact);
}
}
Test Class 测试班
public class Chapter3Test {
Contact weekdayContact = new Contact("Vienna", "Milkstreet 20", "+336598741");
Contact holidayContact = new Contact("Cocos Island", "Gulay avenue 85", "+5589632147");
Orders orders = new Orders();
EntityManager em = EntitySessionManager.getEntityManager();
@Before
public void setUp(){
orders.setWeekdayContact(weekdayContact);
orders.setHolidayContact(holidayContact);
em.getTransaction().begin();
}
@Test
public void createOrders() {
em.persist(orders);
em.getTransaction().commit();
em.close();
}
@Test
public void retrieveOrders(){
em = EntitySessionManager.getEntityManager();
em.getTransaction().begin();
Orders myOrders = em.find(Orders.class, 2L);
assertEquals("They are not Equal", myOrders.getWeekdayContact(), orders.getWeekdayContact());
assertEquals("They are not Equal", myOrders.getHolidayContact(), orders.getHolidayContact());
em.getTransaction().commit();
em.close();
}
@After
public void closeResources(){
//em.close();
}
}
When i run my test, i get the following results:: 运行测试时,我得到以下结果:
java.lang.AssertionError: They are not Equal expected: chapter3.Contact<Contact [name=Vienna, address=Milkstreet 20, phone=+336598741]> but was: chapter3.Contact<Contact [name=Vienna, address=Milkstreet 20, phone=+336598741]>
You can see that what was expected and what was returned are the same. 您可以看到预期的结果和返回的结果是相同的。
Here: 这里:
private String name;
But then within your equals method: 但是然后在您的equals方法中:
if(name != contact.getName()){return false;}
You are comparing strings with == resp. 您正在比较==和resp的字符串。 != here.
!=这里。
Wrong (see here for details). 错误(有关详细信息,请参见此处 )。 Use
name.equals(contact.getName())
instead! 请改用
name.equals(contact.getName())
!
It is not asssertEquals()
that is wrong. 并不是
asssertEquals()
错误。 That method just calls your implementation of equals()
. 这种方法只是调用您的实现
equals()
。 So when the result is unexpected , then your implementation must be broken! 因此,当结果出乎意料时 ,则必须破坏您的实现!
And hint: when comparing anything (esp. strings) do not trust that they look the same . 提示: 比较任何东西(尤其是字符串)时,请不要相信它们看起来相同 。 The one and only thing that makes objects equal ... is the result of the
equals()
method. 使对象相等的 唯一一件事是
equals()
方法的结果。 Anything else does not count! 别的不计!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.