question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Refactor instance based utility classes to static utility classes

See original GitHub issue

The internal implementation of Mockito contains numerous helper classed that serve as a container for useful methods. By its nature these classes don’t have a state. Currently some them are instance based in other word you need to create the utility class to call a helper method. This not only pollutes the heap but also the code cause instance methods can’t be imported statically.

Here is an example:

Helper helper = new Helper();
if (helper.isInputValid(input)){
  [...] 
}

vs. static import of Helper.isInputValid

if (isInputValid(input)){
  [...] 
}

The aim of this ticket is to identify canidates that can be refactored to static utility classes. If you like to refactoring and mockito feel free to send a PR and reference this issue.

Refactoring canidates:

  • AccessibilityChanger
  • BeanPropertySetter
  • ConditionalStackTraceFilter
  • FieldCopier
  • FieldReader
  • GenericMaster should be integrate into GenericTypeResolver
  • JUnitFailureHacker can be removed when the deprecated VerboseMockitoJUnitRunner is removed
  • LenientCopyTool
  • MatcherBinder
  • MockitoCore should better be a singleton
  • MockCreationValidator
  • RemoveFirstLine
  • #591 ArgumentMatchingTool
  • #515 AllInvocationsFinder
  • #502 ArgumentsComparator
  • #540 ArrayUtils
  • #490 AtLeastXNumberOfInvocationsChecker
  • #490 AtLeastXNumberOfInvocationsInOrderChecker
  • #912 Constructors
  • #427 FieldSetter
  • #908 FriendlyExceptionMaker
  • #431 HandyReturnValues
  • #432 InvocationMarker
  • #462 InvocationsFinder
  • #908 JUnitDetecter
  • #490 MissingInvocationChecker
  • #490 MissingInvocationInOrderChecker
  • #514 MockUtil
  • #503 NonGreedyNumberOfInvocationsInOrderChecker
  • #907 NumberOfInvocationsInOrderChecker
  • #907 NumberOfInvocationsChecker
  • #547 ObjectMethodsGuru
  • #427 Reporter
  • #535 SuperTypesLastSorter
  • #501 TestMethodFinder
  • #515 VerifiableInvocationsFinder

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:26 (24 by maintainers)

github_iconTop GitHub Comments

1reaction
ChristianSchwarzcommented, Aug 8, 2016

I would like that all methods invoked in the current class are referenced with this. (and super. for that matter).

To me it feels like unnecessary/duplicate code cause this is implicit. Adding this. before every instance call would create a lot more text and would IMHO reduce readability. E.g.:

this.doSometing(this.withPrivateMethod()) vs. doSomething(withPrivateMethod())

Therefore if we see this.verify, we know it is in the current class and the same object, whereas verify references a static method in this class, or an imported static method.

An other option avoid ambiguity is to qualify static methods via its class name. This way you can also distinguish which verify(…) is called ( MockitoCore.verify(..) / Mocktio.verify(..)) , which is not an easy task when a static import is used.

1reaction
ChristianSchwarzcommented, Jun 12, 2016

@TimvdLippe know what you mean! The next PR’s include only one refactored class at a time.

Read more comments on GitHub >

github_iconTop Results From Across the Web

What is the best way to refactor Utility class in java (static ...
Maybe an example would be useful. · Sometimes you want to mock them, they might be troublesome to do so. · can you...
Read more >
3 Considerations for Your Next Utility Function Refactor - Innovid
The first option is a static function in a new class. The second option, assuming you use any dependency injection framework (e.g. Guice),...
Read more >
Refactoring - Utility classes behavior under a common interface
it can have a private constructor and expose only static methods. Yes, it can, but there is no technical need for.
Read more >
Refactoring - How to safely convert a static class with tons of ...
Many programmers are attracted towards the ease of programming and simplicity in writing static classes and methods to implement utility ...
Read more >
OOP Alternative to Utility Classes - Yegor Bugayenko
A utility class (aka helper class) is a “structure” that has only static methods and encapsulates no state.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found