• 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

JUnit question

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I'm using JUnit to test my code and have what I think is a basic question. My class under test has methods that set field values, but the fields are private and there are not accessors to the data. How do you test setter methods worked if there's no public access to the fields. I've been creating elaborate checks by deconstructing other public methods results. I figure there's a smarter way to do this?

Thanks
 
Marshal
Posts: 79179
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Don't know. We have a testing forum and your question would probably sit better there.
 
best scout
Posts: 1294
Scala IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi rick,

I think there's no easy way to test code like this with JUnit. I'm not an expert in unit testing but this is a problem you should perhaps solve by designing your code with testing in mind. It's generally considered a better design if code is easier to test.

In this case you should think about it why you don't have accessor methods and how objects of this class are constructed in "normal" code. It's hard to give good answers without knowing more details but perhaps someone with more experience in unit tests could give you advice how to produce well-designed and well-testable code.

Marco
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You don't test methods, you test behavior. You probably call those setters to modify the behavior of the class. Test for that changed behavior.
reply
    Bookmark Topic Watch Topic
  • New Topic