• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

JUnit on eclipse

 
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
reply
    Bookmark Topic Watch Topic
  • New Topic