• 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

reset varabiables as if application just started

 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hello

I made several tests(about 20) and the problem I keep having is that some private variables with no getters or setters inside my application are being altered during my tests. What I want is that each test has the default variables as if the application just started running(as if the second test is the first test). I want this because I dont want that the second test is dependent of the outcome of the first test...

I know about setup and teardown. But I cant use it because I dont have setters and getters to those variables and I dont want to change my beans just to make my tests independent of eachother
 
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Without knowing more it's difficult to help. If you need to re-instantiate the classes in question then do do, or design the application and/or class hierarchy in such a way that you can change the variables you need to.

You might be able to use reflection, but it'd be substantially cleaner to just create a testable app.
 
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Jan,
You could run each test in a new JVM. It would be slower, but guarantee a clean start. It would be better to refactor the code to be testable as Dave said.
 
jan dressen
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks for the replies...

despite the advice I would like to try it with forking. But how do I do this from a rujn configuration from within Eclipse? or anyone has an iedea how I can do this?

thanks
 
Jeanne Boyarsky
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Do you know which variables are being "unset"? If so, you could write code that uses reflection to reset them.

I don't think you can fork purely within Eclipse. You can write an Ant task and have Eclipse call it.
 
Jeanne Boyarsky
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Note: This is awkward to do because it is not something people do frequently. It would be better to fix the app to be testable.
 
jan dressen
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thank you for the tips. but isn't it bad practice to change code just for testing purposes? Is it ok to make variables visible(directly or through getters) just for testing reasons?
 
Jeanne Boyarsky
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

jan dressen wrote:thank you for the tips. but isn't it bad practice to change code just for testing purposes?


No. The test code is a client of your application just like the human users are. Making good interfaces for the test client is fine.

jan dressen wrote:Is it ok to make variables visible(directly or through getters) just for testing reasons?


There are multiple schools of thought on this. I think making variables visible (package private) for testing is ok.
reply
    Bookmark Topic Watch Topic
  • New Topic