我有以下类但我的jUnit assertEquals()测试不起作用。经过几天的搜索,没有回答,我决定在这里发帖。代码中的所有内容似乎都是正确的,但它仍无效。
联系班级:
@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;
}
}
订单类:
@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);
}
}
测试类
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();
}
}
当我运行我的测试时,我得到以下结果::
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]>
您可以看到预期的内容和返回的内容是相同的。
答案 0 :(得分:3)
下面:
-Djava.net.preferIPv4Stack=true
但是在你的equals方法中:
private String name;
您正在使用== resp比较字符串。 !=这里。
错误(有关详细信息,请参阅here)。请改用if(name != contact.getName()){return false;}
!
错误不是name.equals(contact.getName())
。该方法只需调用 的asssertEquals()
实现。因此,当结果意外时,您的实施必须中断!
提示:当比较时,任何事物(尤其是字符串)都不相信它们看起来相同。 唯一使对象等于的东西是equals()
方法的结果。其他任何事情不计数!