在Intellij中的自动静态导入

发布于 2025-02-02 18:07:00 字数 607 浏览 1 评论 0原文

在以下代码中,我使用class名称导入Intellij Idea 2022 Community Edition中的静态方法,

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

是否有任何方法或键盘快捷键可以删除类名称并使用类似的静态导入:

import static org.mockito.BDDMockito.given;

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

我搜索了lot lot并阅读Intellij文章,但无法找到任何解决方案。任何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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

椒妓 2025-02-09 18:07:00

您可以将Caret放在方法名称上,按“ alt+Enter”,然后选择“添加静态导入...”

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

再见回来 2025-02-09 18:07:00

您可以将IDE配置为使用*自动导入静态功能/var。
这是这样做的方法:

  1. 设置 - >编辑 - >代码样式 - > Java->导入(TAB)
  2. 在内部添加您的软件包名称

它将导致*导入,您可能不希望此效果。但是我不知道其他任何方式。

这种方法的好处是,这些功能将用于自动完成,这使您的生活更轻松:)

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文