• Post Reply Bookmark Topic Watch Topic
  • New Topic

Problems with JUnit Tests  RSS feed

 
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi folks,

I'm having a few problems with some JUnit tests.

The problem is that the values in each test are multiples of their expected values, so it looks like the values produced by the previous test isnt' being removed.

The code is attached below and what I think is happening is that the "testObject" is not being cleared as I hope it is and thus the values are being produced twice. If this is the case why isn't my tearDown() working? I have also tried moving the testObject = null line to the start of each testCase.


[ May 03, 2005: Message edited by: David Dickinson ]
 
author & internet detective
Marshal
Posts: 37518
554
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
David,
Setting an object to null in tearDown() doesn't actually accomplish anything. It just nulls out the reference, which would happen when you assign the new Garage object in the next setUp().

I suspect that Garage is using static data. Can you post the relevant methods and data members from that class?

For the future, we have a testing forum farther down.
 
David Dickinson
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jeanne Boyarsky:
David,
I suspect that Garage is using static data. Can you post the relevant methods and data members from that class?


Jeanne,

I'm afraid I can't post the methods but the ArrayLists which hold the objects are static because I have a method which returns the ArrayList.size()

Thank you for explaining that but im afraid I don't have any idea how to clear the reference to the garage? Could you advise me?

Thank-you
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37518
554
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
David,
That's ok. If you know there is static data, it isn't necessary to post the methods.

I suggest creating a static utility method to clear out the static data. You can have your tearDown() call it. That way you are starting with a clean slate each time. If you don't want to expost that helper/clear out method, you can make it package-private visibility.
 
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by David Dickinson:


Jeanne,

I'm afraid I can't post the methods but the ArrayLists which hold the objects are static because I have a method which returns the ArrayList.size()

Thank you for explaining that but im afraid I don't have any idea how to clear the reference to the garage? Could you advise me?

Thank-you

Why are the ArrayLists static? What do they represent? Perhaps there is a deeper design issue here that needs to be explored rather than just fixing your tests to work like you want.

Layne
 
David Dickinson
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The ArrayLists are used in a class which extends AbstractTableModel. One of the methods is getRowCount() and it calls a getArrayListSize method in another class which holds the array, which in turn calls the ArrayList.size() method, for some reason Eclipse insists that the getArrayListSize method must be static and then insists the ArrayList is static as well.

Any idea why this is? I've just accepted it and moved on but I don't know how to clear my arrays now, and the tests are holding me up.

Thank-you
 
David Dickinson
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does anyone have any comments on whether my approach is bad practice?

Additionally what is the best way to clear the arrays to ensure the tests are accurate? I only have a two days to complete this im afraid so any advice is appreciated.

Thank you
 
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by David Dickinson:
The ArrayLists are used in a class which extends AbstractTableModel. One of the methods is getRowCount() and it calls a getArrayListSize method in another class which holds the array, which in turn calls the ArrayList.size() method, for some reason Eclipse insists that the getArrayListSize method must be static and then insists the ArrayList is static as well.

Any idea why this is?


What error message is Eclipse showing when the getArrayListSize method is not static? How do you call it in getRowCount?

I've just accepted it and moved on but I don't know how to clear my arrays now, and the tests are holding me up.


The tests are highlighting a problem of your code that you still would have without the tests and that probably would lead to other, hard to find bugs in the long run. you should be glad that you have them!
 
David Dickinson
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
getRowCount is extremely simple...



Surely this is standard practice in Java? I've only got 12 hours to finish this so if anyone knows why its insisting I make my ArrayLists static and a way around it please let me know.

Thank-you!
 
David Dickinson
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I should add this code is called from an implementation of the AbstractTableModel class's getRowCount which may have something to do with the problem.

However I don't know why Eclipse is insisting it must be static when called from the getRowCount method. Other methods calling the getArrayListSize() method don't insist on it being static. This only happens when called from within the AbstractTableModel.

This is the error message in eclipse..
The method getArrayListSize() from the type Garage is not static

This is the code which is causing the problem...

Problem method in my implementation of AbstractTableModel


Method called from the AbstractTableModel implementation


Thank you very much
[ May 08, 2005: Message edited by: David Dickinson ]
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by David Dickinson:

This is the error message in eclipse..
The method getArrayListSize() from the type Garage is not static

This is the code which is causing the problem...

Problem method in my implementation of AbstractTableModel



I assume that Garage is the name of the class, not the name of a reference variable? For calling a non-static method, you need an object instance to call it on. Do you have a Garage instance somewhere in the table model?
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!