简体   繁体   English

Java Equal方法的JUnit测试

[英]JUnit testing of java Equal method

I wrote this code but I am still new in JUnit and have no idea of testing equal and equal2 method. 我写了这段代码,但是我仍然是JUnit的新手,不知道测试equal和equal2方法。 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. 我在此代码中的目标是使用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? 目的是创建一个等于等于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);

    }

}

You need to create instances of EqualMethods to compare them. 您需要创建EqualMethods实例进行比较。 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". 如果您使用的是实际版本的junit,则无需扩展TestCase,并且测试方法的名称无需以“ 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. 将方法命名为“ equal”或“ equal2”可能不是最好的主意……在对象(所有其他对象的根)中,已经有一个名为“ equals”的方法……可能会造成混淆。
  3. Most probably fname == name.getFname() does not what you want to accomplish. fname == name.getFname()最有可能不是您想要完成的。 This compares the references to two strings, not the content. 这会将引用与两个字符串而不是内容进行比较。 Strings are objects and are to be compared like this string1.equals(string2) . 字符串是对象,将像这样string1.equals(string2)进行比较。

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. 我仍然认为您的操作有些奇怪,您可能应该拥有两个具有相同属性和方法的类-每个类都具有一个equals方法。 Then you should created tests for both those classes. 然后,您应该为这两个类创建测试。 Not sure what your trying to achieve here. 不知道您要在这里实现什么。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM