Keyboard shortcut to switch between test and class in eclipse? - eclipse

Lets say you create a class Foo and a test FooTest ( standard JUnit test )
I want a keyboard shortcut to help quickly switch between the test and the class.
If that is not possible, something like TextMate's "Run focussed test" (Command + Shift + R) might be of great help.

You can also use moreunit. I just installed it and it works fine, allowing to switch between source class and test class and also to run test class being in source class editor.

There isn't a built in one, but there's a plug-in called "Switcher" which will allow you to do that, here

Related

Eclipse: What is a better way to find the implementation of a virtual function?

Eclipse: What is a better way to find the implementation of a virtual function?
I am now using "Search" to look at every places that have the virtual function name. Apparently it is a very ineffective way.
[Update 1]: Specifically I am reading the code of the liveMedia of live555. I import it as C++ code in Eclipse.
I assume you mean abstract functions.
Right-click on the function and select 'Declarations'.
select function or method and press F3 you will reach at method creation.....
You can see this on the Type hierarchy which can be opened by:
double click on method name
Ctrl + T
or by right clicking on the method name and navigating in the menu.
This opens a class tree showing only classes that implement the method, and if you click on a class it jumps to the implementation for that class.
You have to be in the .hpp file annoyingly to open the Type hierarchy, if you are on the .cpp you have to first jump to the .hpp with Ctrl + Click on the method name.
Related question: Eclipse shortcut to find all children class that override a method
Tested in Eclipse 2020-03 (4.15.0) with this test project.

Eclipse optimize imports to include static members and methods

Long user of eclipse and Java. One issue that i've found with Eclipse, is it seems that there is no easy way to import static members and methods.
Namely, the jUnit fail() method from org.junit.Assert
I create several classes a day, and manually add
import static org.junit.Assert.fail;
to the import statements. This is quite annoying. I absolute LOVE using Ctrl+Shift+O to organize my imports, but it still doesn't find static members and methods.
Also, the import does not show up in eclipse.
Funny thing is, is that i've seen it work previously, but i cant recall the variables.
So to my question:
Does anybody know what I need to do to ensure that this static import is always recognized and can be found using Ctrl+Shift+O?
Thanks #qqilihq.
Note:
The answer that was accepted does not work with the Organize Imports keyboard shortcut that I preferred in eclipse, but does work for the "hover over" suggestion.
You can use Ctrl + Shift + M, for example you want to import verify method from Mockito class then
Mockito.verify() // select verify and press Ctrl + Shift + M
This will import verify static method from Mockito class.
Did you have a look at Preferences > Java > Editor > Content Assist > Favorites? You can define candidates for static imports there. Result:
For less used classes you can lower the value of Preferences > Java > Code Style > Organize Imports > Number of static imports needed for .* but beware that you may get .* for classes that contain generically named methods such as getInstance. This in turn may lead to confusion and/or naming conflicts.
You can add the classes that you statically import from Preferences > Java > Editor > Content Assist > Favorites page in Eclipse. Then, Ctrl+Space shortcut lists all the static members of your favourite classes in the content assist menu.

JUnit: How to bundle #Test methods to a suite?

I have several classes each containing several methods annotated with #Test. The classes are not extending TestCase. Now I want to
write a main that executes all these methods (for command line use)
create a class that can be "Run as -> JUnit Test" in Eclipse executing all these methods
Since the classes are no TestCases I can't just add them to a suite. Also extending TestCase is not an option because the test methods are just annotated and their names don't start with 'test'.
How can I do this?
Try this:
#RunWith(Suite.class)
#Suite.SuiteClasses({
Test1.class,
Test2.class,
Test3.class,
Test4.class
})
public class YourTestSuite {
}
You can set up run configurations for a project. Right click on your project. Then selecte 'Run as' -> 'Run configurations'. Within that you can select run all tests
What Eugene posted works if you have a small number of classes. If you want all the tests in a certain package pattern, Classpath Suite lets you specify this with patterns instead of listing them all out.

Generating JUnit stubs for new methods in existing class in Eclipse

This question is tangentially related to How can I create JUnit stub methods in eclipse? but it doesn't answer my specific question.
Assuming you have an existing JUnit test case class, if you add a method to the target class (or choose to test a previously untested method), is there a way to tell Eclipse to generate the stub for the "new" method(s) in the existing TestCase without creating a new test case class?
Install the MoreUnit extension from the MarketPlace.
Open the package hierarchy panel.
Navigate down to the class that
you've modified.
Right click on the new method.
Select "Generate Test". The generated stub will appear in your ClassTest file.
My solution.
I simply go through the standard 'create JUnit test case'
Select file to test. -> New Junit test case
Go through the normal process in creating the test case, but only select those that you want new stubs for.
The file is created with the stubs, which I now copy into the existing test case file.
Delete the newly created test file class.
It's not the most efficient, but quicker than 'thick fingering' when you create them manually.
David
The usual working cycle with unit tests is to create the test case first
public void test_new_method() {
ClassUnderTest x = new ClassUnderTest();
x.NewMethod();
}
At that point Eclipse will give you a warning that the method "NewMethod" does not exist. Than you simply select a "Quick Fix" (Ctrl-1 or Ctrl-2 i'm not sure) to create the missing method in the class.

Creating an interface from a huge class using resharper

I have a very big class with lots of methods, is it possible to build an interface from this class using Resharper?
Yes.
My shortcut is Ctrl + Shift + R to bring up the refactoring options. Doing this on the class name allows you to" Extract Interface..."
Optionally, you can choose from the menu > ReSharper > Refactor > Extract Interface...
Uh, maybe I'm missing something here (I've never used resharper) but you can extract an interface from a class using the standard VS IDE refactoring tools (at least you can in 2008). Right click the class, select 'Refactor' and then 'Extract Interface'. This will bring up a dialog box where you can select which properties to include.