JUnit testing made easier with Eclipse templates

When you’re writing JUnit tests, you’ll often find yourself repeating the same boilerplate code. Take the following code:

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

public class EngineTest {
    @Before
    public void setup() {
        ...
    }

    @Test
    public void test() throws Exception {
        ...
    }

    @After
    public void tearDown() {
        ...
    }
}

If you want to add a test, you have to somehow add lines 3, 11, 12 and 14 before you even get to the actual test. The same holds for setup and teardown code. We know that Eclipse can help with adding imports automatically when you save or autocomplete, but it still takes time to get everything in place.

Eclipse templates can help take away the drudgery by adding all this boilerplate code automatically. It can automatically add a test, setup and teardown methods together with calls to assertEquals, assertTrue and others. I’m assuming JUnit 4 in all the templates. If you’re new to templates, then see this tip to get an overview.

Import the templates automatically

Download the template below then import it into Eclipse.

  • JUnit_templates: This bundle includes:
    test4: Creates a test method together with the @Test annotation and its import. NB: The 4 at the end is to distinguish it from two other  Eclipse built-in templates called test and Test.
    setup4: Creates a setup method with the @Before annotation and its import.
    teardown4: Creates a teardown method (called tearDown) with an @After annotation and its import.
    asequals: A call to assertEquals with its static import. The expected an actual default to locally available objects, but can easily be changed.
    astrue & asfalse: A call to assertTrue/assertFalse and its static import. The argument defaults to the nearest boolean but can easily be changed.
    asnull & asnotnull: A call to assertNull/assertNotNull and its static import. The argument defaults to the nearest object, but can easily be changed.

If you want to create the templates manually, open the template XML in your favourite XML editor, then see this tip, taking the values from the relevant fields in the XML.

Related Tips

Here are some more templated-related tips to help speed up coding:

Support Eclipse On E

3 thoughts on “JUnit testing made easier with Eclipse templates

  1. Pingback: Plantillas de Eclipse « Java Mania

  2. How do I at an “@Ignore” to the test template so that when the junit test class is created. I would get:

    @Test
    @Ignore
    public void testName() {
    fail(“Not yet implemented”);
    }

    • If you’ve imported the ones on this post, just go to Window > Preferences > Java > Editor > Templates and edit the template called “test4”. Add another line at the top that says (note the syntax is slightly different to the syntax in the template but the result is the same):

      @${ignoreType:newType(org.junit.Ignore)}

      If you haven’t imported these templates from the post, then you’ll probably need to edit Eclipse’s default template called “test”.

      Hope this helps.