Originally posted by Noel Verdurmen:
is it possible to test private methods with JUnit? How?
Originally posted by Noel Verdurmen:
Or is testing private methods something that is not desirable?
Author of Test Driven (2007) and Effective Unit Testing (2013) [Blog] [HowToAskQuestionsOnJavaRanch]
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
Originally posted by Ernest Friedman-Hill:
It may be ugly to have the test case in the same file as the main class, but note that it doesn't have to be shipped and adds nothing to the size of the outer class's .class file.
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
Originally posted by Ilja Preuss:
[F]or inner classes to be able to access private members of the enclosing class, the compiler has to create synthetic access methods with less restrictive visibility modifiers
No�l
Originally posted by Ilja Preuss:
I agree with Lasse - the need to test a private method is a design smell, probably your class is too complex.
Can you please explain why you want to test a private method? What is the method doing and what class is it part of?
No�l
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
Originally posted by Ernest Friedman-Hill:
If you're writing a standalone application or system, this is all well and good. If, perchance, you're writing a library or library-like tool, then you need to think twice about making something available that you are not prepared to support.
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
Find me live coding on Twitch at https://JitterTed.Live, on YouTube at https://JitterTed.TV, or on Twitter at https://twitter.com/JitterTed.
Author of Test Driven (2007) and Effective Unit Testing (2013) [Blog] [HowToAskQuestionsOnJavaRanch]
Originally posted by Ted Young:
[QB]How about legacy code? ...
Has anyone else run into this same issue and had a different solution?
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
Originally posted by Lasse Koskela:
[QB]If you're feeling stuck trying to write tests for the legacy code, try taking a step back and see if you could add an abstraction layer in between the new codebase and the legacy codebase which you can write tests for.
Find me live coding on Twitch at https://JitterTed.Live, on YouTube at https://JitterTed.TV, or on Twitter at https://twitter.com/JitterTed.
Originally posted by Ted Young:
That's a good idea that works well if the legacy code is granular enough. The problem that I'm dealing with (and solving in unit tests by accessing private fields) is that the public interfaces are not granular enough, i.e., one method that does everything instead of a number of more fine-grained methods.
[/QB]
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
Originally posted by Lasse Koskela:
...you shouldn't need to test private methods....
Juan Rolando Prieur-Reza, M.S., LSSBB, SCEA, SCBCD, SCWCD, SCJP/1.6, IBM OOAD, SCSA
Originally posted by Lasse Koskela:
... Here's a nice article explaining some ways to approach testing private methods:
Testing Private Methods with JUnit and SuiteRunner ...
Juan Rolando Prieur-Reza, M.S., LSSBB, SCEA, SCBCD, SCWCD, SCJP/1.6, IBM OOAD, SCSA
Author of Test Driven (2007) and Effective Unit Testing (2013) [Blog] [HowToAskQuestionsOnJavaRanch]
Originally posted by john prieur:
Silly example:
Test the public method addSome( smallN) that updates a private counter.
You want to see if it behaves properly when the private counter
turns to a value near Integer.MAX_VALUE (or a large Float or something).
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
Originally posted by Vladas Razas:
Though maybe I could move all that caching object/lazy instantion map functionality to the separate class. Maybe even "re-use" it later
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
Don't get me started about those stupid light bulbs. |