Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JUnit on eclipse

 
vijay kumarg
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am new to the eclipse IDE.
How to identify whether the failure trace which we get upon running a test case is belongs to the current test case or any previous one's.
Since I am not getting any failure trace eventhough an expected is other than result.
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Eclipse's JUnit GUI associates an assertion failure with the test method that faile. It also allows you to double click on the failuse to be taken to the line the assertion that failed was on, so it should be easy enough to find out.


Since I am not getting any failure trace eventhough an expected is other than result

I'd check the logic of your test case. Is the assertion as you expect it to be?
 
vijay kumarg
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear Paul ,

Here are all the three classes:
1 Employee
2 EmployeeList
3 EmployeeListTest

I am getting AssertionFailedError in testGetEmployee() and testRemoveEmployee() eventhough expected and the result are same.

// // EmployeeList

// // EmployeeListTest

[CODE]import junit.framework.TestCase;
import java.util.Hashtable;
import java.util.Enumeration;

public class EmployeeListTest extends TestCase
{
private EmployeeList emplist;
private Employee empx;

protected void setUp()
{
emplist=new EmployeeList();
empx=new Employee("ff",6666);
emplist.addEmployee();
}
protected void tearDown()
{
//optional
}

/*
* Test Methods
*/
public void testCountEmployees()// Working!!
{
int result=emplist.countEmployees();
assertEquals(6,result);
}
public void testRemoveEmployee()
{
Employee expected=new Employee("cc",3333);
Employee result=(Employee)emplist.removeEmployee("cc");
assertNotNull(result);
assertEquals(expected,result);
}
public void testAddEmployee()//Working!!
{
String expected="gg";
assertEquals(expected,emplist.addEmployee());
}
public void testGetEmployee()//Not Working!!
{
Employee expected=new Employee("dd",4444);
Employee result=emplist.getEmployee("dd");
assertNotNull(result);
assertEquals(expected,result);
}
}
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I am getting AssertionFailedError in testGetEmployee() and testRemoveEmployee() eventhough expected and the result are same.


Looking just at testGetEmployee():


I would expect an AssertionError to be thrown at line three, since I don't see anywhere that you add an Employee object to your collection with the key "dd".

Also, looking at your Employee class you override equals() but you don't override hashCode(). Yo0u will need to override this too.
[ January 12, 2007: Message edited by: Paul Sturrock ]
 
vijay kumarg
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear Paul,

Please find the failure trace. As per the trace the expected and the result are same but why an error is thrown?
Could You please the post code for the hashCode() for the Employee object here in the reply.

 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

As per the trace the expected and the result are same but why an error is thrown

The results are not the same. An object is equal if its equals method returns true and it has the same hashCode value as the object you are comparing it to. Since you have not overridden hashCode() its unlikely they will have the same hashCode value.


Could You please the post code for the hashCode() for the Employee object here in the reply

How you implement hashCode() is up to you. If you look at how the String class does it:

It is basically derived from the three properied of a String - value, offset and count. You'll need something similar for your Employee object. Here is an article which covers some of the factors you might consider.
[ January 12, 2007: Message edited by: Paul Sturrock ]
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul, I think you are following the wrong trail, the hashcode method isn't the problem - it isn't used by the test.

Vijayk, the problem is that by implementing

public boolean equals(Employee x)

you haven't overridden

public boolean equals(Object)

but you have *overloaded* it. Your custom equals method simply isn't used at all.
[ January 12, 2007: Message edited by: Ilja Preuss ]
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah yes, of course! Skim read the code too quickly.

(That's twice today Ilja. Think I'll wake myself up wit ha cup of coffee before posting again )
 
vijay kumarg
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear Ilja/Paul,
Have you find the problem with the program?
If you have any solution I request you to please post the code for the fixture.

Thanks
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Read Ilja's post. He's correctly spotted that you are overloading not overriding the equals method. Replace:

with a method that takes an Object as a parameter and it should work.
[ January 12, 2007: Message edited by: Paul Sturrock ]
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic