2017-03-01 69 views
0

我正在研究這個小Java實踐項目,我應該編程一種約會應用程序...我們有這些在線單元測試來檢查我們的工作,我總是喜歡在繼續前測試我寫的每種方法和類。在第一個類中,有幾個方法沒有通過測試,我不知道爲什麼我嘗試了很多不同的東西。第一種方法稱爲getTitle,它只是一個普通的getter方法,它讓我回到分配給構造函數中標題的值(參見下面的代碼)。不傳遞的第二種方法是必須覆蓋的.equals方法。我會在相應代碼後發佈我在下面列出的錯誤。Java - 構造函數將null分配給字符串而不是所需的值

以下是一個類的構造函數:

public class Interest { 
private String title; 
private Map<String, Float> alternatives; 

public Interest(String title, Map<String, Float> alternatives) 
{ 
    if (title.isEmpty()) //Title must contain something. 
     throw new IllegalArgumentException(); 
    if (alternatives == null) //If alternatives points at null, I have to create an empty map. 
     this.alternatives = new HashMap<String, Float>(); 
    else 
    { 
     this.alternatives = new HashMap<String, Float>(alternatives); //Map must be a copy. 
     this.title = title; //This is where my problem is happening. 
    } 
} 

下面是的getTitle方法的代碼:

public String getTitle() 
{ 
    return this.title; 
} 

測試口口聲聲說:

testGetTitle 

Cause of failure: 
java.lang.AssertionError: expected:<Family Guy> but was:<null> 
at org.junit.Assert.fail(Assert.java:88) 
at org.junit.Assert.failNotEquals(Assert.java:743) 
at org.junit.Assert.assertEquals(Assert.java:118) 
at org.junit.Assert.assertEquals(Assert.java:144) 
at TestInterest.testGetTitle(TestInterest.java:56) 

我嘗試了幾個不同的東西,比如在構造函數中我嘗試創建String的副本,或者在getTitle方法中,我嘗試過返回一個新的字符串(this.title),但我仍然有同樣的錯誤...我也試過使用Concat,但它沒有工作。

而測試只是試圖運行程序與預先分配的值和每種方法的測試。

我有一個問題,第二個方法如下:

@Override 
public boolean equals(Object obj) 
{ 
    if (obj == this) 
     return true; 
    if (!(obj instanceof Interest)) { 
     return false; 
    } 

    if (obj instanceof Interest && this.title == ((Interest) obj).getTitle() && this.alternatives == ((Interest) obj).getAlternatives()) 
     return true; 
    if (this == (Interest) obj) 
     return true; 
    else 
     return false; 
} 

它不斷告訴我:

testEqualsObject 

Cause of failure: 
java.lang.AssertionError: Two Interests should be equal when identical. 
at org.junit.Assert.fail(Assert.java:88) 
at org.junit.Assert.assertTrue(Assert.java:41) 
at TestInterest.testEqualsObject(TestInterest.java:104) 

我認爲我認爲平等的所有選項,但不知道..

任何幫助將不勝感激,我沒有那麼多的編程經驗,我正在嘗試學習Java,它有時會受到所有這些單元測試的挫折......


整個類的代碼,如果它有助於:

package jpp.exams.dating; 

import java.util.Collections; 
import java.util.HashMap; 
import java.util.Map; 
import java.util.Objects; 

public class Interest { 
private String title; 
private Map<String, Float> alternatives; 

public Interest(String title, Map<String, Float> alternatives) 
{ 
    if (title.isEmpty() && title != null) //Title must contain something. 
     throw new IllegalArgumentException(); 
    if (alternatives == null) //If alternatives points at null, I have to create an empty map. 
     this.alternatives = new HashMap<String, Float>(); 
    else 
    { 
     this.alternatives = new HashMap<String, Float>(alternatives); //Map must be a copy. 
     this.title = title; //This is where my problem is happening. 
    } 
} 

public String getTitle() 
{ 
    return this.title; 
} 

public Map<String, Float> getAlternatives() 
{ 
    return new HashMap<String, Float>(alternatives); 
} 

public float matchAlternative(String alternative) 
{ 
    if (alternative == null || title == null) 
     throw new IllegalArgumentException(); 
    else if (title.equals(alternative)) 
     return 1f; 
    else if (this.alternatives.containsKey(alternative)) 
     return (float) this.alternatives.get(alternative); 
    else 
     return 0f; 
} 

@Override 
public String toString() 
{ 
    String s = title + "\n"; 

    for (Map.Entry<String, Float> entry : this.alternatives.entrySet()) { 
     String key = entry.getKey(); 
     Float f = entry.getValue(); 
     s = s.concat("\t" + key + ": " + f + "\n"); 
    } 
    s = s.substring(0, s.length() - 1); //removes last new line 

    return s; 
} 

@Override 
public boolean equals(Object obj) 
{ 
    if (obj == this) 
     return true; 
    if (!(obj instanceof Interest)) { 
     return false; 
    } 

    if (obj instanceof Interest && this.title == ((Interest) obj).getTitle() && this.alternatives == ((Interest) obj).getAlternatives()) 
     return true; 
    if (this == (Interest) obj) 
     return true; 
    else 
     return false; 
} 

public int hashCode() 
{ 
    return Objects.hash(title); 
} 

}

+2

因爲你只分配它在一個分支..... – Qix

+0

你能進一步解釋嗎? –

+0

還要注意,您在不檢查'title'是否爲'null'的情況下調用'title.isEmpty()'。這對NPE有風險。 –

回答

0

問題是,你是實例替代品的類= NULL

2

關於空標題:在構造函數中如果alternatives不爲空,則只分配一個值爲title。從其他塊中取出this.title = title;部分。

關於equals方法:你沒有用==一些比較,你應該已經使用.equals,所以更改

if (obj instanceof Interest && this.title == ((Interest) obj).getTitle() && this.alternatives == ((Interest) obj).getAlternatives()) 

if (this.title.equals(((Interest) obj).getTitle()) && this.alternatives.equals(((Interest) obj).getAlternatives())) 

(你會發現我也去掉了instanceof -check。這是沒有必要的,因爲你會已經返回,如果這是假的)

相關問題