JUnit testing, exception in threa main

Posted by Crystal on Stack Overflow See other posts from Stack Overflow or by Crystal
Published on 2010-05-10T02:05:55Z Indexed on 2010/05/10 2:08 UTC
Read the original article Hit count: 306

Filed under:
|
|

I am new to JUnit and am trying to follow my prof's example.

I have a Person class and a PersonTest class. When I try to compile PersonTest.java, I get the following error:

Exception in thread "main" java.lang.NoSuchMethodError: main

I am not really sure why since I followed his example.

Person.java

public class Person implements Comparable
{
    String firstName;
    String lastName;
    String telephone;
    String email;

    public Person()
    {
       firstName = "";
       lastName = "";
       telephone = "";
       email = "";
    }

    public Person(String firstName)
    {
        this.firstName = firstName;
    }

    public Person(String firstName, String lastName, String telephone, String email) 
    {
        this.firstName = firstName;
        this.lastName = lastName;
        this.telephone = telephone;
        this.email = email;
    }

    public String getFirstName()
    {
        return firstName;
    }

    public void setFirstName(String firstName)
    {
        this.firstName = firstName;
    }

    public String getLastName()
    {
        return lastName;
    }

    public void setLastName(String lastName)
    {
        this.lastName = lastName;
    }

    public String getTelephone()
    {
        return telephone;
    }

    public void setTelephone(String telephone)
    {
        this.telephone = telephone;
    }

    public String getEmail()
    {
        return email;
    }

    public void setEmail(String email)
    {
        this.email = email;
    }

    public int compareTo(Object o)
    {
        String s1 = this.lastName + this.firstName;
        String s2 = ((Person) o).lastName + ((Person) o).firstName;
        return s1.compareTo(s2);
    }

    public boolean equals(Object otherObject)
    {
        // a quick test to see if the objects are identical
        if (this == otherObject) {
            return true;
        }

        // must return false if the explicit parameter is null
        if (otherObject == null) {
            return false;
        }

        if (!(otherObject instanceof Person)) {
            return false;
        }

        Person other = (Person) otherObject;
        return firstName.equals(other.firstName) && lastName.equals(other.lastName) &&
            telephone.equals(other.telephone) && email.equals(other.email);
    }

    public int hashCode() 
    {
        return this.email.toLowerCase().hashCode();
    }

    public String toString()
    {
        return getClass().getName() + "[firstName = " + firstName + '\n'
                                    + "lastName = " + lastName + '\n'
                                    + "telephone = " + telephone + '\n'
                                    + "email = " + email + "]";
    }
}

PersonTest.java

import org.junit.Test; // JDK 5.0 annotation support
import static org.junit.Assert.assertTrue; // Using JDK 5.0 static imports
import static org.junit.Assert.assertFalse; // Using JDK 5.0 static imports
import junit.framework.JUnit4TestAdapter; // Need this to be compatible with old test driver

public class PersonTest
{
    /** 
    A test to verify equals method.
    */
    @Test 
    public void checkEquals()
    {
        Person p1 = new Person("jj", "aa", "[email protected]", "1112223333");
        assertTrue(p1.equals(p1)); // first check in equals method
        assertFalse(p1.equals(null)); // second check in equals method
        assertFalse(p1.equals(new Object())); // third chk in equals method
        Person p2 = new Person("jj", "aa", "[email protected]", "1112223333");
        assertTrue(p1.equals(p2)); // check for deep comparison
        p1 = new Person("jj", "aa", "[email protected]", "1112223333");
        p2 = new Person("kk", "aa", "[email protected]", "1112223333");
        assertFalse(p1.equals(p2)); // check for deep comkparison
    }
}

© Stack Overflow or respective owner

Related posts about junit

Related posts about java