wood burning stoves 2.0*
The moose likes Testing and the fly likes JUnit on eclipse Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Engineering » Testing
Bookmark "JUnit on eclipse" Watch "JUnit on eclipse" New topic
Author

JUnit on eclipse

vijay kumarg
Ranch Hand

Joined: Dec 14, 2006
Posts: 105
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

Joined: Apr 14, 2004
Posts: 10336

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?


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
vijay kumarg
Ranch Hand

Joined: Dec 14, 2006
Posts: 105
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

Joined: Apr 14, 2004
Posts: 10336


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

Joined: Dec 14, 2006
Posts: 105
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

Joined: Apr 14, 2004
Posts: 10336


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

Joined: Jul 11, 2001
Posts: 14112
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 ]

The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

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

Joined: Dec 14, 2006
Posts: 105
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

Joined: Apr 14, 2004
Posts: 10336

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 ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JUnit on eclipse
 
Similar Threads
Junit test: cannot find test Method although that it is already defined in the test class
Custom Eclipse Template
creating a WAS client.
how to run junit test in eclipse
WMB ESQL and DB question