File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Testing and the fly likes Static Variable Access from Junit class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Make it so: Java DB Connections & Transactions this week in the JDBC forum!
JavaRanch » Java Forums » Engineering » Testing
Bookmark "Static Variable Access from Junit class" Watch "Static Variable Access from Junit class" New topic

Static Variable Access from Junit class

Ravissant Saraswat

Joined: Mar 15, 2010
Posts: 20
Hi Everyone,
I'm trying to test a web application with multiple junit tests (with multiple inputs), for which I'm using a main class to call all these Junit test cases. Now what I basically need to do is, get some benchmarking parameters which I intend displaying on a jfree chart, that reads from a csv.
Now, all the benchmarking parameters are retrieved in a service I've written, all parameters set as public static variables in this service. I need all these parameters in the main class from which I'm calling the junit test cases. However , all variables come 0, even though all test cases execute successfully . The following is the code for the main class:

Please note that the endeavour works when I put the writeToCSV method in my service, but that isn't how I want it, I want to access the variable from the main class.
Please help!!!
Jeanne Boyarsky
author & internet detective

Joined: May 26, 2003
Posts: 32844

Do you know if JUnit is running in a separate JVM? I don't think it is, but that's something to check. Another is when the variables get zeroed out.

[OCA 8 book] [Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Other Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, TOGAF part 1 and part 2
Ravissant Saraswat

Joined: Mar 15, 2010
Posts: 20
Thanks for the reply Jeanne! Well, I don't know if JUnit is running on a seperate JVM, but after debugging, I suspect that the service class is unloading. I even tried to serialize the service object to deseralize in the main class but I got a "file not found" exception.
Could you/someone please tell me how I can persist the state/value of the object/variables so I can get those parameters in my main class?
Hebert Coelho
Ranch Hand

Joined: Jul 14, 2010
Posts: 754

Could you post the code that declares the variables?

[] [Full WebApplication JSF EJB JPA JAAS with source code to download] One Table Per SubClass [Web/JSF]
I agree. Here's the link:
subject: Static Variable Access from Junit class
It's not a secret anymore!