2017-03-01 52 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。这是没有必要的,因为你会已经返回,如果这是假的)

相关问题