Useful Eclipse templates

These are some templates I use in eclipse for the auto-completion (Ctrl+Space):

Section comments:

General section comment (sec):


/* --- ${comment} --- */
${cursor}

Section comment for constants (secconstants):


/* --- Constants --- */
 ${cursor}

Section comment for constructors (secctors):


/* --- Constructors --- */
${cursor}

Section comment for class fields (secfields):


/* --- Class Fields --- */
 ${cursor}

Section comment for class getters & setters (secgettersetters):


/* --- Getters & Setters --- */
 ${cursor}

Section comment for helper methods (sechelpers):


/* --- Helper Methods --- */
 ${cursor}

Section comment for implemented methods (secimplemented):

/* --- Implemented methods of ${interface} --- */
${cursor}

Section comment for mocks (secmocks):


/* --- Mocks (isolating the tested class) --- */
${cursor}

Section comment for test setup (secsetup):


/* --- Set up for tests --- */
${cursor}

Section comment for tests (sectest):


/* --- Tests for ${testedFunctionality} --- */
${cursor}

Test related templates:

JUnit 4 setUp method (Setup):


/* --- Set up for tests --- */

@${testType:newType(org.junit.Before)}
public void setUp() throws Exception {
    ${cursor}
}

Junit4 + Mockito setup method (SetupMockito):


/* --- Set up for tests --- */

@${testType:newType(org.junit.Before)}
public void setUp() throws Exception {
    ${mockito:newType(org.mockito.MockitoAnnotations)}.initMocks(this);
    ${testedInstance:field} = new ${testedClass:argType(testedInstance, 0)}();
    ${testedInstance}.set${mockedClass};
    // ${todo}: Set the other mocks into the tested instance.${cursor}
}

Create a new Mockito argument captor for a given class (captor):


@${captorAnnotation:newType(Captor)}
private ${argCaptor:newType(ArgumentCaptor)}<${captured:link}> ${cursor}${captured}Captor;

Assert the value inside a captor (assertCaptor):


${:importStatic(Assert)}assertEquals(${expected}, ${captor:var(ArgumentCaptor)}.getValue().get${property});${cursor}

And here’s an export of the aforementioned templates to a single xml file (+ some specific ones for randomness which I might edit).

<?xml version="1.0" encoding="UTF-8" standalone="no"?><templates><template autoinsert="true" context="java" deleted="false" description="Assert the value inside a captor" enabled="true" name="assertCaptor">${:importStatic(Assert)}assertEquals(${expected}, ${captor:var(ArgumentCaptor)}.getValue().get${property});${cursor}</template><template autoinsert="true" context="java" deleted="false" description="Create a new argument captor for a given class" enabled="true" name="captor">@${captorAnnotation:newType(Captor)}

private ${argCaptor:newType(ArgumentCaptor)}&lt;${captured:link}&gt; ${cursor}${captured}Captor;

</template><template autoinsert="true" context="java" deleted="false" description="Put in a viper random field." enabled="true" name="randomField">/**

 * Random generator.

 */

private ${randomGen:newType(ViperRandom)} random = ViperRandom.instance();

${cursor}</template><template autoinsert="true" context="java" deleted="false" description="Random.nextBoolean()" enabled="true" name="rnb">${random:var(com.muzicall.commons.random.ViperRandom, java.util.Random)}.nextBoolean()${cursor}</template><template autoinsert="true" context="java" deleted="false" description="Random.nextEnum()" enabled="true" name="rne">${random:var(ViperRandom)}.nextEnum(${enumClass})${cursor}</template><template autoinsert="true" context="java" deleted="false" description="Random.nextLong()" enabled="true" name="rnl">${random:var(ViperRandom, Random)}.nextLong(${:link})${cursor}</template><template autoinsert="true" context="java" deleted="false" description="Random.nextString()" enabled="true" name="rns">${random:var(ViperRandom)}.nextString(${10:link})${cursor}</template><template autoinsert="true" context="java" deleted="false" description="Section comment" enabled="true" name="sec">/* --- ${comment} --- */

${cursor}</template><template autoinsert="true" context="java" deleted="false" description="Section comment for constants" enabled="true" name="secconstants">



/* --- Constants --- */

${cursor}</template><template autoinsert="true" context="java" deleted="false" description="Section comment for class constructors" enabled="true" name="secctors">



/* --- Constructors --- */

${cursor}</template><template autoinsert="true" context="java" deleted="false" description="Section comment for class fields" enabled="true" name="secfields">



/* --- Class Fields --- */

${cursor}</template><template autoinsert="true" context="java" deleted="false" description="Section comment for class getters &amp; setters" enabled="true" name="secgettersetters">



/* --- Getters &amp; Setters --- */

${cursor}</template><template autoinsert="true" context="java" deleted="false" description="Section comment for helper methods" enabled="true" name="sechelpers">



/* --- Helper Methods --- */

${cursor}</template><template autoinsert="true" context="java" deleted="false" description="Section comment for implemented methods" enabled="true" name="secimplemented">



/* --- Implemented methods of ${interface} --- */

${cursor}</template><template autoinsert="true" context="java" deleted="false" description="Section comment for mocks" enabled="true" name="secmocks">/* --- Mocks (isolating the tested class) --- */

${cursor}</template><template autoinsert="true" context="java" deleted="false" description="Section comment for overriding methods" enabled="true" name="secoverride">/* --- Override methods from ${superClass} --- */

${cursor}</template><template autoinsert="true" context="java" deleted="false" description="Section comment for setup" enabled="true" name="secsetup">/* --- Set up for tests --- */

${cursor}</template><template autoinsert="true" context="java" deleted="false" description="Section comment for tests" enabled="true" name="sectest">/* --- Tests for ${TESTED_FUNCTIONALITY} --- */

${cursor}</template><template autoinsert="true" context="java" deleted="false" description="set pointcut (AspectJ)" enabled="true" name="set">set(${modifier_pattern} ${ret_type_pattern} ${type_pattern}.${id_pattern})</template><template autoinsert="true" context="java" deleted="false" description="JUnit 4 setUp method" enabled="true" name="Setup">/* --- Set up for tests --- */



@${testType:newType(org.junit.Before)}

public void setUp() throws Exception {

 ${cursor}

}</template><template autoinsert="true" context="java" deleted="false" description="Junit4 + Mockito setup method" enabled="true" name="SetupMockito">/* --- Set up for tests --- */



@${testType:newType(org.junit.Before)}

public void setUp() throws Exception {

 ${mockito:newType(org.mockito.MockitoAnnotations)}.initMocks(this);

 ${testedInstance:field} = new ${testedClass:argType(testedInstance, 0)}();

 ${testedInstance}.set${mockedClass};

 // ${todo}: Set the other mocks into the tested instance.${cursor}

}</template></templates>
<pre>

Fun!

Advertisements
This entry was posted in Java and tagged , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s