简体   繁体   中英

Auto static import in IntelliJ

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?

您可以将插入符号放在方法名称上,按“Alt+Enter”并选择“为...添加静态导入”

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在此处输入图像描述

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 :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM