Twitter github

Posts Tagged with “testing”

JUnit4 @Rule’s Rule

Maybe I’ve been under a rock as of late, but I’ve been writing some tests as of late and came across a nifty little feature in JUnit4 called Rules while needing to verify a certain exception was being thrown. For example, let’s say we needed to verify that something threw an IAE…

public class MyTest {
  @Rule
  public ExpectedException exception = ExpectedException.none();
 
  @Test
  public void willThrowIAE() {
    exception.expect(IllegalArgumentException.class);
    exception.expectMessage("omg bad arguments");
    // do crap that will throw an IAE
  }
}

This is just the tip of the iceberg with what you can do with rules. There’s also a rule (TemporaryFolder) for creating temporary folders that are guaranteed to be deleted after the test is run. On top of that, it’s pretty easy to create your own rules if you need to modify test behavior to suit your needs.

In the end, it’s always fun to discover something new and useful in a tool you’ve been using for awhile.

SWTBot and Eclipse Forms

When it comes to user interface testing in Eclipse, I’m a big fan of SWTBot. I had some long plane rides recently and a need to add some functionality to SWTBot so I decided to see if I could tackle an outstanding feature request in SWTBot to add Eclipse Forms support. On top of that, I also had a selfish reason to learn more about the internals of SWTBot. After forking Ketan’s copy of SWTBot on GitHub, I pushed my changes which adds an SWTFormsBot class as the primary handle to drive user interface tests.

So your test code would look something like this…

public class SWTBotImageHyperlinkTest extends AbstractSWTBotFormsTestCase {
 
	@Test
	public void findImageHyperlink() throws Exception {
                FormView view = new FormView(shell);
                SWTFormsBot = new SWTFormsBot();
		SWTBotImageHyperlink link = 
                     bot.imageHyperlink("Image link with no image");
		assertNotNull(link);
		assertEquals("Image link with no image", link.getText());
	}
 
}

We’re looking to eventually integrate this into SWTBot proper, just pay attention to this bug if you’re interested.

Enjoy.