Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JUnit testing of java Equal method

Tags:

java

junit

junit4

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

    }

}
like image 646
Splitter Avatar asked May 21 '11 07:05

Splitter


People also ask

How do you check if two objects are equal in JUnit?

assertEquals() calls equals() on your objects, and there is no way around that. What you can do is to implement something like public boolean like(MyClass b) in your class, in which you would compare whatever you want. Then, you could check the result using assertTrue(a. like(b)) .

Which methods Cannot be tested by JUnit test class?

Which methods cannot be tested by the JUnit test class? Explanation: When a method is declared as “private”, it can only be accessed within the same class. So there is no way to test a “private” method of a target class from any JUnit test class.

Does assertEquals use equal?

assertEquals. Asserts that two objects are equal. If they are not, an AssertionError is thrown with the given message. If expected and actual are null , they are considered equal.


2 Answers

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).
like image 199
Arne Deutsch Avatar answered Sep 29 '22 11:09

Arne Deutsch


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.

like image 32
Jon Avatar answered Sep 29 '22 11:09

Jon