java - Difference between @VisibleForTesting and @Deprecated int unit test -


let's there happens existing long private method (one of i'm not allowed refactor smaller pieces @ stage in development process) want write couple of regression-protection unit test it, now.

i heard of @visiblefortesting annotation, not sure of benefits , gotchas. previously, had been marking things @deprecated , comments try , make clear like:

... code ...     // ====================================== testing use below  ====================================== @deprecated // testing only, not use! boolean testgiveaccesstosomethingprivate() {     // call private method , results } 

it seems whenever mark @visiblefortesting seems expose method realz, without indication user of api method meant testing... (whereas if mark method @deprecated, ides put strike-through warns other developers not accidentally use test method actual code

@deprecated

when method marked @deprecated, programmers use method know maybe method removed, different behavior ... in future version. , if happen, code broken , rework. so, should use function or should replace other function. (often programmer made api make similar function without deprecated use).

@visiblefortesting

you have learnt public private protected ... , use well. in real world, not our imagination. example, class have private variable, , want test variable, how can?

class simpleclass() {    private int a;     public simplemethod() {        if (a < 0) {           //        } else {           // thing        }    } } 

one way changing scope of variable package level, because test file same package file tested. programmer jumps , ask self: "why put modifier of package level? think private level better". (of course, don't know or don't mind reading test code because life busy). have way know using @visiblefortesting. read , google , know: "ahhhh, understood. want variable testable".

that's short story 2 annotations. similar is: don't change way code run. not change notify other people know something. famous annotation every java developer knew , similar @override

the difference is: @deprecated using code, , @visiblefortesting reading code. let make life's programmer easier in both case.

hope :)


Comments

Popular posts from this blog

apache - Remove .php and add trailing slash in url using htaccess not loading css -

inno setup - TLabel or TNewStaticText - change .Font.Style on Focus like Cursor changes with .Cursor -