File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Problems with JUnit Tests Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "Problems with JUnit Tests" Watch "Problems with JUnit Tests" New topic
Author

Problems with JUnit Tests

David Dickinson
Ranch Hand

Joined: Nov 11, 2004
Posts: 66
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 ]
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 26144
    
  66

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.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
David Dickinson
Ranch Hand

Joined: Nov 11, 2004
Posts: 66
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
internet detective
Marshal

Joined: May 26, 2003
Posts: 26144
    
  66

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.
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
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


Java API Documentation
The Java Tutorial
David Dickinson
Ranch Hand

Joined: Nov 11, 2004
Posts: 66
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

Joined: Nov 11, 2004
Posts: 66
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
Ilja Preuss
author
Sheriff

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


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
David Dickinson
Ranch Hand

Joined: Nov 11, 2004
Posts: 66
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

Joined: Nov 11, 2004
Posts: 66
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

Joined: Jul 11, 2001
Posts: 14112
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?
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: Problems with JUnit Tests
 
Similar Threads
java autoboxing / unboxing and Junit -- Solved
Using ANT to execute JUNIT test cases.
OOCalculator >> Sidebar >> Mike Matola
OO design question?
setUp and tearDown