Run a single JUnit test method in Eclipse

Normally you would run all JUnit tests to ensure that changes haven’t broken any of the tests. But sometimes you want to focus on a single test method and only rerun that test, especially if running all tests is too slow or if you write a single (failing) test upfront.

For example, in the following code you might only want to run testMethod2():

public class SomeTest {
   @Test
   public void testMethod1() {...}

   @Test
   public void testMethod2() {...}
}

Eclipse provides a couple of ways to run individual test methods, one from within the editor itself and another from the JUnit view. It also has especially good keyboard support so you can run those tests without reaching for the mouse.

Continue reading

The easiest ways to navigate methods in a class using Eclipse keyboard shortcuts

Java classes can get big and hairy, making it difficult to find the method you’re looking for when browsing or editing a class. There is no specific order to where methods can be in a class and different developers have different preferences about where to put them.

You could use the mouse wheel and scroll ferociously until you eventually find the method or you could even use Page Down/Page Up on your keyboard. But these methods can be time-consuming and haphazard, especially when the class has lots of methods or they’re scattered in an arbitrary order.

Luckily, Eclipse has a number of fast and easy ways to help you navigate methods in a class, especially using the keyboard. I’ll discuss some of those keyboard shortcuts and also which ones to use when.

Continue reading

The fastest ways to open editors in Eclipse using the keyboard

Something you do a lot in Eclipse is open files such as classes, XML files and property files in editors. But using the mouse to hunt through the Package Explorer folder hierarchy takes a long time, especially if you forgot where the files are located. The problem gets worse the more projects and files you have in your workspace, so there must be a better way of opening editors.

Luckily, Eclipse has a number of ways to open editors easily using the keyboard. Couple these with oodles of keyboard shortcuts to navigate between editors once you’ve opened them, and you’ve got enough tools to stay away from the mouse and make coding go a lot faster. I’ll discuss some of the more efficient options and then give a summary of when to use which method.

Continue reading

Add comments and Javadocs in Eclipse with a single keystroke

When you want to work with comments in Eclipse, you could use the slow way of moving to the start of the line, pressing // and then repeating this for all the lines you have.

Or you could use the quick way of adding a comment with a single keystroke no matter where the cursor’s positioned in the statement.

The same goes for Javadocs – there are just too many things to type before you can start commenting the good stuff. That’s why Eclipse also has a shortcut that let’s you add Javadoc to a field, method or class.

Continue reading

Eclipse shortcuts for common SVN commands

When working with SVN, CVS or any other repo, there are some things you’ll just do over and over again. Things like committing files, comparing files and viewing a file’s history are all commonplace actions.

So you might be wondering where the keyboard shortcuts for these actions are, because,  as I mentioned before, you normally work faster with keyboard shortcuts than jumping between the mouse and keyboard all the time. And often the longest part about working with repos is guiding your mouse through treacherous context menus and submenus.

The good news is that there are keyboard shortcuts in Eclipse for the most common repository commands (and not-so-common as well). The bad news is that most of them aren’t configured by default, so you need to set some of them up beforehand. But the 2 minutes spent configuring these may save you hours in the long run.

I’ll be using SVN as the example repository (using Subclipse) but these should apply to your favourite repository integrated into Eclipse (I know that the in-built CVS has these commands as well).

Continue reading

Navigate and fix errors and warnings in a class with Eclipse keyboard shortcuts

I really don’t like it when Eclipse shows errors/warnings annotations in a class. It’s sometimes nice to jump from one to the next and clean out a class one line at a time, but most of the time they’re just distractions, so I want to be able to find and fix them fast.

So there must be a better way to jump between the errors/warnings  than to use the mouse or page down to the next error. These methods are not only slow but often frustrating because you tend to miss the annotation, especially if it’s a big class. And navigating to the Problems view using the keyboard is ok, but sometimes overkill for just clearing out errors/warnings in one class.

A good thing then that Eclipse offers keyboard shortcuts that take you to the next/previous annotation in the class. And it does so in a way that selects the annotation immediately, allowing you to use Quick Fix (Ctrl+1) to fix it fast. So here’s how to use these shortcuts to navigate between the error/warning annotations and fix some of the errors easily.

Continue reading

The fastest ways to navigate views in Eclipse using the keyboard

With so many views in Eclipse (around 50 for a default install), it can become difficult to navigate them all, especially when you don’t want to reach for the mouse all the time. As I’ve mentioned in a previous post on how to manage keyboard shortcuts, learning how to use the keyboard efficiently can save you oodles of time later on.

Fortunately, Eclipse has a number of ways to navigate between views using only the keyboard. I’ll discuss the Show View shortcuts, the Show Views dialog, Quick Access and then view cycling, with some advice on when to use which one. There’s also a bonus tip on how to activate the editor with the keyboard once you’re done with a view.

Continue reading

How to manage keyboard shortcuts in Eclipse and why you should

When I use Eclipse I try and use shortcuts all the time. They really make me work faster and give me time to focus on getting the code out rather than slowing down to invoke IDE commands with the mouse.

Because people work differently and on different things, it’s important to manage keyboard shortcuts to suit the way you work. So it’s good that Eclipse makes it really easy to change shortcuts and also to view shortcuts for the commands you use a lot.

I’ll discuss how to change keyboard shortcuts, how to avoid conflicts with existing ones, why it’s a good thing to manage shortcuts and then end off with some examples of common shortcuts that you should add to you arsenal. Continue reading

Run/debug a class in Eclipse as a JUnit test or Java app with a single keystroke

If you’ve used Eclipse for a while, you may have run into the commands Run As and Debug As. These are used to launch a class/project as a particular application, eg. a Java application, JUnit test or Eclipse application.

They’re normally accessed with the mouse via the right-click menu or the pulldown menu on the run/debug icons in the toolbar. However, using the mouse to launch these classes isn’t the fastest way of doing things, especially if you’re frequently running different classes, such as JUnit tests.

To help with this, Eclipse has keyboard shortcuts to invoke the Run As/Debug As commands. They really speed up launching apps (which you’ll do often in a day’s work). I’ll show you these shortcuts and also a couple of others to make life easier.

Continue reading

Switch and close editors faster with the keyboard using Eclipse’s Quick Switch Editor

I spend a huge amount of my programming time in editors, editing, switching between them and closing them. This means that managing editors has to be as efficient as possible, which means that, for one thing, I shouldn’t be swapping between the mouse and keyboard all the time to work with editors.

Luckily, there is a way to manage editors using the keyboard (and it doesn’t involve the laborious Ctrl+F6). It’s a feature called Quick Switch Editor and it makes working with editors a lot more enjoyable.

Quick Switch Editor works by popping up a list of all open editors (sorted by editor title) to the right of the editors tabs. The list is searchable so you don’t have to navigate long lists with the arrow key and searching supports wildcards. On top of that, you can also close editors from the list. Here’s a quick glimpse of what it looks like:

Continue reading