简体   繁体   中英

Eclipse QuickFix Feature

I have the following basic code in Eclipse:

import static org.junit.Assert.assertThat;   
import org.junit.Test;

public class GreeterTest {

    private Greeter greeter = new Greeter();

    @Test
    public void greeterSaysHello() {
        assertThat(greeter.sayHello(), containsString("Hello"));
    }  
}

Eclipse correctly recognizes that The method containsString(String) is undefined for the type GreeterTest . To resolve this, I want to import the following:

import static org.hamcrest.CoreMatchers.containsString;

However, the only Quick Fix ( Ctrl + 1 ) Eclipse suggests is Create method containsString(String) , rather than the above import. And Organize imports ( Ctl + Shift + O ) also doesn't do the trick.

Is there a different keyboard shortcut in Eclipse that can predict, based on my dependencies, that I may want to add the above import? For comparison, I believe in IntelliJ, this is done via Intention Actions ( Alt + Enter ).

This can be done by adding org.hamcrest.CoreMatchers to Java Editor Favorites

Action Plan

  • Open Eclipse Preferences
  • In the left tree menu go to Java > Editor > Content Assist > Favorites 在此处输入图片说明
  • Use the button New Type... to open the dialog New Type Favorite
  • Here use the button Browse... to open the dialog Type Selection
  • Write in the text field Choose type name : CoreMatchers
    This should show the matching classes. Select the one you require, in case there are multiple.
    在此处输入图片说明
  • Then use the button OK to close the window
  • Back in the dialog New Type Favorite
    Here the selected class should be shown
    在此处输入图片说明
  • Close the dialog with the button OK
  • In the dialog Preferences the class should now be listed under Favorites
  • Close the dialog Preference by using the button Apply and Close

Now go back to the error in your editor and use Ctrl + 1 This should show the wished match! 在此处输入图片说明

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