简体   繁体   中英

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. 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);

    }

}

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) .

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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