java Equal方法的JUnit测试
我写了这段代码,但我对 JUnit 还很陌生,不知道测试 equal 和 equal2 方法。下面是我写的代码。我在这段代码中的目的是使用 equal 方法查看 fname 是否等于 lname ,并使用 equal2 检查 fname 是否与 fname (它本身)相同,也许我的代码也有错误。
public class EqualMethods {
/**
* @param args
*/
private String fname;
private String lname;
public EqualMethods(String fl)
{
fname = fl;
}
public EqualMethods(String f, String l)
{
fname = f;
lname = l;
}
public String getFname() {
return fname;
}
public String getLname()
{
return lname;
}
public void setLname(String lname)
{
this.lname = lname;
}
public void setFname(String fname) {
this.fname = fname;
}
public int equal(EqualMethods name)
{
if(fname == name.getFname() && lname == name.getLname())
{
return 1;
}
else
{
return 0;
}
}
public int equal2(Object o)
{
if(o.getClass() == EqualMethods.class )
{
EqualMethods e = (EqualMethods) o;
if(this.fname.equals(e.fname))
{
return 1;
}
return 0;
}
return 0;
}
public String toString()
{
return (" My first name is: "+fname + " Last name is: " + lname);
}
目标是创建一个 equal 和 equal2 的 Junit 测试用例,因为我创建的测试用例没有提供正确的输出。这是我编写的 JUnit 测试用例,但我无法使我的方法静态,但如何解决它?
public class EqualMethodsTest extends TestCase{
@Test
public void testEqual2() {
String name = "goma";
int ret = 1;
int ans ;
ans= EqualMethods.equal2(name);
assertEquals(ret,ans);
}
}
I wrote this code but I am still new in JUnit and have no idea of testing equal and equal2 method. Below is the code I wrote. My object in this code is to see if the fname is equal to lname using equal method and by using equal2 to check if fname is same as fname(it self) maybe my code have errors too.
public class EqualMethods {
/**
* @param args
*/
private String fname;
private String lname;
public EqualMethods(String fl)
{
fname = fl;
}
public EqualMethods(String f, String l)
{
fname = f;
lname = l;
}
public String getFname() {
return fname;
}
public String getLname()
{
return lname;
}
public void setLname(String lname)
{
this.lname = lname;
}
public void setFname(String fname) {
this.fname = fname;
}
public int equal(EqualMethods name)
{
if(fname == name.getFname() && lname == name.getLname())
{
return 1;
}
else
{
return 0;
}
}
public int equal2(Object o)
{
if(o.getClass() == EqualMethods.class )
{
EqualMethods e = (EqualMethods) o;
if(this.fname.equals(e.fname))
{
return 1;
}
return 0;
}
return 0;
}
public String toString()
{
return (" My first name is: "+fname + " Last name is: " + lname);
}
The objective is to create a Junit test case to equal and equal2 as the test case i created does not provide a proper output.Here is the JUnit test case I wrote but I cant make my method static though how to get around it?
public class EqualMethodsTest extends TestCase{
@Test
public void testEqual2() {
String name = "goma";
int ret = 1;
int ans ;
ans= EqualMethods.equal2(name);
assertEquals(ret,ans);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要创建 EqualMethods 实例来比较它们。像这样:
编辑:
关于代码的一些评论:
fname == name.getFname()
并不是您想要完成的任务。这会比较对两个字符串的引用,而不是内容。字符串是对象,可以像string1.equals(string2)
一样进行比较。You need to create instances of EqualMethods to compare them. Like this:
Edit:
A few comments about the code:
fname == name.getFname()
does not what you want to accomplish. This compares the references to two strings, not the content. Strings are objects and are to be compared like thisstring1.equals(string2)
.这可能是一个更好的方法:
我仍然认为你所做的有点奇怪,你可能应该有两个具有相同属性和方法的类 - 每个类都有一个 equals 方法。然后您应该为这两个类创建测试。不确定你想在这里实现什么目标。
This is probably a better way to do this:
I still think what your doing is a bit odd though, you should probably have two classes with the same attributes and methods - each with an equals method. Then you should created tests for both those classes. Not sure what your trying to achieve here.