my dog learned polymorphism*
The moose likes Testing and the fly likes JUnit testing and method scope? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Engineering » Testing
Bookmark "JUnit testing and method scope?" Watch "JUnit testing and method scope?" New topic
Author

JUnit testing and method scope?

S Saunders
Greenhorn

Joined: Mar 21, 2003
Posts: 22
Hi. I am currently starting to use JUnit to perform unit testing in my code. I have a question with regards with method scope. When I try to write a test for a private method, I run into an obvious problem my fixture cannot inoke the method.
So my question is about testing of private and protected methods. Are these types of methods ignored? How do you test them? I am looking for concrete answers like: I do this... and even philosophical reasons for the testing of these types of methods.
Thanks,
Steve
Richard Jensen
Ranch Hand

Joined: May 14, 2003
Posts: 67
JUnit is really designed to test the public API you are providing to the users of the class. The private methods in particular are implementation details that could change without affecting the public (advertised) behavior.
However, this is a FAQ.
See How do I test protected methods?
and
How do I test private methods?


Richard
N 37 33 W 122 18
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
My current opinion is:
If a method is so complex that it needs explicite testing, it should be moved to a (new?) class where it naturally will be public.
Do you have an example we could take a look at?


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
S Saunders
Greenhorn

Joined: Mar 21, 2003
Posts: 22
The method is not a complex method. As I stated before, I am beginnging to implement unit testing and ran across the testing of a private method in my code.
Ilja, what is your opinion of the scope of testing? Currently, we are trying to test the majority of methods by using JUnit. Is this a waste of time? What are your thoughts about how much/many of a classes methods should be tested?
Steve
Richard Jensen
Ranch Hand

Joined: May 14, 2003
Posts: 67
Originally posted by S Saunders:
Ilja, what is your opinion of the scope of testing? Currently, we are trying to test the majority of methods by using JUnit. Is this a waste of time? What are your thoughts about how much/many of a classes methods should be tested?

I'm not Ilja, but I'll take another shot at this. The XP answer (which is relevant because Kent Beck created JUnit) is
test everything that could possibly break

If you don't care about your class operating correctly or you don't think it could possibly break, don't test it.
Now, maybe your real concern is retrofitting tests onto existing classes. Then you have to answer the question about will the cost of doing so give you a proportional benefit.
If you are doing Test-Driven Development, you end up with JUnit tests for most everything.
Have you read Test Infected?
Are you having problems writing JUnit tests for your code or are you just feeling that it is a lot of work for little (or no) benefit?
[Do all of the developers share the same feelings?]
Are you doing (or plan to do) refactoring? If so, even the tests that you don't think are worthwhile now may save you later.
What are your goals for unit testing?
S Saunders
Greenhorn

Joined: Mar 21, 2003
Posts: 22
Originally posted by Richard Jensen:

Have you read Test Infected?


Yes, I have read that article and am trying to read the other articles on JUnit's site.


Are you having problems writing JUnit tests for your code or are you just feeling that it is a lot of work for little (or no) benefit?
[Do all of the developers share the same feelings?]
Are you doing (or plan to do) refactoring? If so, even the tests that you don't think are worthwhile now may save you later.
What are your goals for unit testing?


I am for unit testing. I see that it can have many potential benefits. At the beginning of this year my manager selected myself and another devloper to "try out" JUnit and unit testing our code. Therefore we are in a learning period. I am trying to understand the why, when, where and hows of testing to give this a good test. I am new to the concepts of XP and would love to have input as to good information about XP and unit testing.
Some developers are wary about the whole thing that it could simply be extra time to write the tests. I want to have a good experiance and have enough information for when we do evaluate this type of development. I can say that I have already found several short comings in my code when using tests.
Steve
Richard Jensen
Ranch Hand

Joined: May 14, 2003
Posts: 67
Originally posted by S Saunders:
I am trying to understand the why, when, where and hows of testing to give this a good test. I am new to the concepts of XP and would love to have input as to good information about XP and unit testing.

Well, this is probably not the right forum for ALL of the XP related info, but my understanding of the testing aspects of XP are:
  • Unit Tests (Programmer Tests) preferably Test First
  • Acceptance Tests (Customer Tests/Functional Tests)

  • Both sorts of tests should be automated and be run all the time. [Using something like CruiseControl]
    JUnit really addresses more the Unit Test level and something like FIT addresses the user/acceptance tests.
    Some developers are wary about the whole thing that it could simply be extra time to write the tests. I want to have a good experiance and have enough information for when we do evaluate this type of development. I can say that I have already found several short comings in my code when using tests.

    It is always hard to introduce new practices to an organization. Have you seen Pete McBreen's book Questioning XP? Chapter 12 is about test first development and he says:
    In practice, although XP-style testing may not be as good as it possible could be as far as the testing community is concerned, many organizations that adopt XP are discovering that the quality of the resulting applications is better. In part, this can probably be attributed to the fact that XP teams devote more effort to testing activities than many traditional teams.

    In a summary list towards the end of his book McBreen writes:
    The basic idea is that you do not have to adopt XP to improve your own software development process. It is possible to lift some of the ideas and apply them within any process:
  • XP-style Unit Tests are a very big win even if you are not using Test First Development
  • ...
  • Check in code only when all tests pass.
  • ...


  • So, I guess one question you have to answer is what are your goals for unit testing? To improve quality? Or something else?
    Are you wanting to change your development process to incorporate XP elements like test-first and continuous integration? Another way to ask that is where do you feel you current development method is NOT working for you?
    [ June 03, 2003: Message edited by: Richard Jensen ]
    [ June 03, 2003: Message edited by: Richard Jensen ]
    Ilja Preuss
    author
    Sheriff

    Joined: Jul 11, 2001
    Posts: 14112
    Originally posted by S Saunders:
    Some developers are wary about the whole thing that it could simply be extra time to write the tests.

    How much time do they typically spend debugging?
    Ilja Preuss
    author
    Sheriff

    Joined: Jul 11, 2001
    Posts: 14112
    Originally posted by S Saunders:
    The method is not a complex method. As I stated before, I am beginnging to implement unit testing and ran across the testing of a private method in my code.
    Ilja, what is your opinion of the scope of testing? Currently, we are trying to test the majority of methods by using JUnit. Is this a waste of time? What are your thoughts about how much/many of a classes methods should be tested?

    I think your confusion arises from the notion of "testing methods". You shouldn't do that - you should test for *functionality*.
    If your private method is needed to fulfill some functionality, it will therefore get tested indirectly - for simple methods that should probably suffice. When you are practicing writing the tests first, a private method can in fact only come into existance by being extracted from an already tested method.
    Regarding your question on how much tests to write, I like the saying that you should test until "uncertainty gets replaced by boredom". That's rather subjective, of course - but the best advice I can offer at the moment...
    BTW, I would only write tests for already existing code if I had to apply a change to it - for example when fixing a bug or extending its functionality.
    Does that help?
     
    It is sorta covered in the JavaRanch Style Guide.
     
    subject: JUnit testing and method scope?