java Equal方法的JUnit测试

发布于 2024-11-08 11:35:59 字数 1753 浏览 0 评论 0原文

我写了这段代码,但我对 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

找个人就嫁了吧 2024-11-15 11:35:59

您需要创建 EqualMethods 实例来比较它们。像这样:

public class EqualMethodsTest extends TestCase{
    @Test
    public void testEqual2() {
        assertEquals(1, new EqualMethods("goma").equal(new EqualMethods("goma")));
    }
}

编辑:
关于代码的一些评论:

  1. 如果您使用实际版本的 junit,则不需要扩展 TestCase,并且测试方法的名称不需要以“test”开头。
  2. 将方法命名为“equal”或“equal2”可能不是最好的主意...在所有其他对象的根 Object 中,已经有一个名为“equals”的方法...可能会令人困惑。
  3. 很可能 fname == name.getFname() 并不是您想要完成的任务。这会比较对两个字符串的引用,而不是内容。字符串是对象,可以像 string1.equals(string2) 一样进行比较。

You need to create instances of EqualMethods to compare them. Like this:

public class EqualMethodsTest extends TestCase{
    @Test
    public void testEqual2() {
        assertEquals(1, new EqualMethods("goma").equal(new EqualMethods("goma")));
    }
}

Edit:
A few comments about the code:

  1. If you work with an actual version of junit you don't need to extend TestCase and the name of the test method does not need to start with "test".
  2. Naming a method "equal" or "equal2" might not be the best idea ... in Object, the root of all other objects, there is already a method with the name "equals" ... might be confusing.
  3. Most probably 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 this string1.equals(string2).
長街聽風 2024-11-15 11:35:59

这可能是一个更好的方法:

private EqualsMethods a;
private EqualsMethods b;

@Before
public void before {
    a = EqualsMethods("a);
    b = EqualsMethods("b);
}

@Test
public void equalTest() {

    assertTrue(a.equal(b));
}

@Test
public void equal2Test() {

    assertTrue(a.equal2(b));
}

我仍然认为你所做的有点奇怪,你可能应该有两个具有相同属性和方法的类 - 每个类都有一个 equals 方法。然后您应该为这两个类创建测试。不确定你想在这里实现什么目标。

This is probably a better way to do this:

private EqualsMethods a;
private EqualsMethods b;

@Before
public void before {
    a = EqualsMethods("a);
    b = EqualsMethods("b);
}

@Test
public void equalTest() {

    assertTrue(a.equal(b));
}

@Test
public void equal2Test() {

    assertTrue(a.equal2(b));
}

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文