0

I have no idea why people with me say this that don't use static import of Mockito methods while writing unit test cases. They say doing so may dramatically increase memory usage and caused build failures.

I want to know whether they are right or wrong. And please explain the reason behind it.

Please share links, thoughts or personal experiences. Thanks.

Amit Kumar
  • 2,685
  • 2
  • 37
  • 72
  • 1
    I've [seen some people complain](http://stackoverflow.com/questions/7322705/finding-import-static-statements-for-mockito-constructs) about the static imports from Mockito and hamcrest clashing (i.e. similar method names). But on the whole, static imports are a useful feature with Mockito I think. Can you cite some sources that warn against the static imports or are these verbal discussions with colleagues? – Duncan Jones Sep 24 '14 at 07:23
  • Yes these are verbal discussions and everyone has started following it, which seems illogical to me and that's why I am here. The link provided by you seems logical though and may be the cause of build failure with me. +1 – Amit Kumar Sep 24 '14 at 07:29
  • Your IDE will help you avoid build failures; there's no need to be shy about static imports. But of course if your colleagues have made a stylistic decision, you may just have to go along with it. Inconsistency isn't helpful in a code base. – Duncan Jones Sep 24 '14 at 07:30

1 Answers1

2

I use the static import for a long time in my unit test. Static import from org.junit.Assert and org.mockito.Mockito. By doing this the syntax is more concise and my test is more readable.

There isn't any performance issue. This is just a syntactic sugar:

syntax within a programming language that is designed to make things easier to read or to express. (wikipedia)

You could have problem if you are importing two methods or constants with the same name. Well in this case you will have to disambiguate.

But it is the same problem if you are using two different classes having the same name (You will have to use a qualified name).

import java.awt.List

...
java.util.List dataList = new ArrayList();
List viewList = new List();
gontard
  • 28,720
  • 11
  • 94
  • 117
  • Oh, I misunderstood. I didn't realise you were just defining syntactic sugar, I thought you were quoting about static imports from somewhere. – Duncan Jones Sep 25 '14 at 09:20