Results 1 to 5 of 5
Like Tree1Likes
  • 1 Post By jim829

Thread: Given a @Test case, why is toString giving error?

  1. #1
    ssk2659 is offline Member
    Join Date
    Jan 2015
    Posts
    48
    Rep Power
    0

    Default Given a @Test case, why is toString giving error?

    hi! Given a @Test case(JUnit), why is toString giving error? The Test case is

    Java Code:
     @Test public void test_equals1(){assertTrue(new Exam("Richard","Carver",1,"final",100).equals(new Exam("Richard","Carver",1,"final",100)));}
    When I run the test it just gives me errors on all of the cases which are referred to the toString method. Why is that? How can I fix that?

    Java Code:
    class Exam {
        	
        	private String firstName;
        	private String lastName;
        	private int ID;
        	private String examType;
        	private int score;
        
        	
        	public Exam(String firstName, String lastName, int ID, String examType, int score)
        	{
        		this.firstName = firstName;
        		this.lastName = lastName;
        		this.ID = ID;
        		this.examType = examType;
        		this.score = score;
        	}
        	
        	public String getFirstName()
        	{
        		return this.firstName;
        	}
        	
        	public String getLastName()
        	{
        		return this.lastName;
        	}
        	
        	public int getID()
        	{
        		return this.ID;
        	}
        	
        	public char getExamType()
        	{
        		char examTypeCasted = 0;
        		examTypeCasted = examType.charAt(0);
        		return Character.toUpperCase(examTypeCasted);
        	}
        	
        	public int getScore()
        	{
        		return this.score;
        	}
        	
        	public String toString()
        	{
        		return this.firstName + " " + this.lastName + " " + this.ID + " " + this.examType + " " + this.score;
        	}
        	
        	public boolean equals(Exam e)
        	{
        		if(this.equals(e))
        			return true;
        		else
        		return false;
        	}
        
        }

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: Given a @Test case, why is toString giving error?

    You have not indicated that your other issues (in other threads) have been resolved. Others may get tired of not getting feedback on their recommendations or help.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    ssk2659 is offline Member
    Join Date
    Jan 2015
    Posts
    48
    Rep Power
    0

    Default Re: Given a @Test case, why is toString giving error?

    Sorry, jim829, previous threads were very helpful. Thank you, folks! I'll change that in seconds.

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: Given a @Test case, why is toString giving error?

    Quote Originally Posted by ssk2659 View Post
    Sorry, jim829, previous threads were very helpful. Thank you, folks! I'll change that in seconds.
    Ok, Great! Now, your equals method looks like it recursively calls itself. This is not appropriate. A proper equals method uses Object as its type. Second, you need to compare specific state information of the class using == or equals as appropriate. Here is an example:

    Java Code:
    class Foobar {
       int val;
       String str =""; // default value
       public boolean equals(Object o) {
          if (this == o) {
                return true;
          }
          if (o instanceof Foobar) {
              Foobar f = (Foobar) o;
              if (val == f.val && str.equals(f.str)) {
                 return true;
              }
          }
       }
       return false;
    }
    Please read the JDK documentation on the equals method defined in the Object class.

    Regards,
    Jim
    Last edited by jim829; 02-16-2015 at 03:24 AM.
    ssk2659 likes this.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  5. #5
    ssk2659 is offline Member
    Join Date
    Jan 2015
    Posts
    48
    Rep Power
    0

    Default Re: Given a @Test case, why is toString giving error?

    Thank you for this info, Jim! I did equatting every primitive and String type to each other and it worked completely and passed every test.

Similar Threads

  1. toString() giving errors
    By alexandra12 in forum New To Java
    Replies: 2
    Last Post: 06-12-2012, 04:41 AM
  2. junit test case error
    By acmohan in forum New To Java
    Replies: 7
    Last Post: 07-12-2011, 01:13 PM
  3. JavaCompiler giving an error WITHOUT ide
    By divs1210 in forum Advanced Java
    Replies: 9
    Last Post: 04-09-2011, 01:04 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •