Home > Enterprise >  Auto static import in IntelliJ
Auto static import in IntelliJ

Time:05-30

In below piece of code I have used Class name to import static methods in IntelliJ IDEA 2022 Community Edition

BDDMockito.given(employeeRepository.findByEmail(employee.getEmail()))
    .willReturn(Optional.empty());
BDDMockito.given(employeeRepository.save(employee)).willReturn(employee);

Is there any way or keyboard shortcut that can remove the class name and use static import like this:

import static org.mockito.BDDMockito.given;

given(employeeRepository.findByEmail(employee.getEmail()))
    .willReturn(Optional.empty());
given(employeeRepository.save(employee)).willReturn(employee);

I googled lot and read IntelliJ articles but could not find any solution. Any IntelliJ expert can help me here?

CodePudding user response:

You may put caret on method name, press "Alt Enter" and select "Add static import for ..."

CodePudding user response:

You can configure your IDE to automatically import static functions/vars from specific packages with *. This is the way to do it:

  1. Settings -> Editor -> Code Style -> Java -> Imports (tab)
  2. Add your package name inside enter image description here

It will result in a * import though and you may not want this effect. But I do not know of any other way.

The good thing about this method is that the functions will be available for auto completion and that makes your life easier :)

  • Related