unit testing - How do I test local inner class methods in java? -
In many applications, I often have algorithms that use dedicated sub-algorithms (or just well-defined pieces of code).
So far, when I wrote the main algorithm, I made a personal law for each sub-algorithm like the example given below (Oldstyle):
Public class OldStyle {public at main alg () {int x = subAlg01 (); Int y = subAlg02 (); Int z = x * y; Return z; } Private Ent SubLegacy () {Return 3; } Private Ent SubAlg 02 () {Return 5; }}
This work is fine, but I did not like the propagation of methods (sub alg 101 and all a lg 02), even if private, only one method (main alg ).
Recently I gave information about the use of local internal classes and now my example is (Newstyle):
public class Newstyle {public ent major Alg () { Class nested {public at sub a lg 01 () {return 3; } Public at subAlg02 () {return 5; }} Nested n = new nested (); Int x = n.subAlg01 (); Int y = n.subAlg02 (); Int z = x * y; Return z; }}
I like it very much, but now I have the following problem: how can I test sub-LG 01 and sub algate 2?
Anyway: I
edit : I try to understand better: I am, let's, a sorting algorithm and I want to make sure To test that it runs as expected, this sorting algorithm is used only by method X of Class X. I can make it a personal way of class X but in class X, there is usually nothing to do with sorting, so why with the sorting method is to spoil the "X"? So I put it in the method m. After some time, I want to improve my sorting algorithm (I make it fast), but I want to ensure that its behavior is expected, so I want to test it again with basic tests.
This is what I have to do, maybe there is no solution, I hope someone can help me.
Edit after the answer option I have selected Rodney's answer because I have taken the solution: A standalone assistant class helps me (this is helpful!) To see clearly what the sub-methods are, and it also gives me the ability to test them.
Filipo, I understand your frustration with the problem and with some replies when I first started a Junket Had used many years ago, I also wanted to test private code, and I thought it was a bad idea with the folly of the gurus.
They were right (surprise!) But only after writing some tests I understood why why you may have to go through the same process, but You will eventually reach the same conclusion; -)
However, in your situation, I will create the nested
in the appropriate standalone class, possibly in a separate package to make it clear that it is a supportive class then I I will write the test directly for any other test.
Then I will write a test for NewStyle
and only NewStyle
. (Rather than starting in NewStyle
- rather than injecting NewStyle
in nested code , it is possible.
Code> Newstyle .
Then when I write a test for testing NewStyle
, code> nested and If I find particularly difficult, then I will create an interface outside of nested
and make another implementation, and test Ne With wStyle
, too.)