java Equal方法的JUnit测试

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6080250/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 14:14:09  来源:igfitidea点击:

JUnit testing of java Equal method

javajunitjunit4

提问by Splitter

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.

我写了这段代码,但我还是 JUnit 的新手,不知道测试 equal 和 equal2 方法。下面是我写的代码。我在这段代码中的目的是使用 equal 方法查看 fname 是否等于 lname,并通过使用 equal2 来检查 fname 是否与 fname(it self) 相同,也许我的代码也有错误。

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?

目标是创建一个 Junit 测试用例,使其等于和 equal2,因为我创建的测试用例没有提供正确的输出。这是我编写的 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);

    }

}

采纳答案by Arne Deutsch

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

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

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

回答by Jon

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.

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