Brand new useful Netbeans Code Templates

I am using Netbeans for a few weeks now and I have to admit, that I really enjoy this IDE. But my happiness with the IDE is not what I want to blog about today :)

Programmers are (or should be) lazy. We do not want to write the same code again and again. Fortunately every sophisticated IDE has some kind of Code Template mechanism, so does Netbeans. Here are some of my created Templates which I think other people could find it as useful as I do:

Logger

We use SLF4j as Logging Facade in our current project. Most of the classes need a logger, so we can define a Template for it:

private final static ${loggerType type="org.slf4j.Logger" default="Logger" editable="false"} log = ${loggerTypeFactory type="org.slf4j.LoggerFactory" default="LoggerFactory" editable="false"}.getLogger(${classVar editable="false" currClassName default="getClass()"}.class);

I replaced the Netbeans standard logger template because it uses the java.util Logger, which I do not like :) . The above SLF4j Logger Template would generate this code:

1
private final static Logger log = LoggerFactory.getLogger(CurrentClass.class );

Tests

Nowadays most developer (fortunately) write tests. I personally prefer JUnit and I use it a lot. So of course I defined a template for a simple test. This template renames just the part after „test“ in the Methodname, because of the JUnit naming convention.

@${testannotation type="org.junit.Test" default="Test" editable="false"}
public void test${expr newVarName default="Method"}()throws Exception
{
    ${selection}${cursor}
}

this template will create this code with all imports. You can rename and then jump between the braces by just hitting enter:

1
2
3
4
5
    @Test
    public void testMethod() throws Exception
    {
 
    }

Often you want to write a test which checks for an Exception. I extended my template, so you can catch exceptions, rename the method and start typing your test immediately :

@${testannotation type="org.junit.Test" default="Test" editable="false"}(expected = ${exc default="Exception"}.class )
public void test${expr newVarName default="Method"}()throws Exception
{
    ${selection}${cursor}
}

Mockito

We enjoy using Mockito in our company. There are really much tests which need some mock objects, of course. If you are lazy (like me :) ), you want to use Mockitos magical @Mock Annotation for Mocking Objects, so you save yourself some typing, especially if you need more than one Mock Object in a TestClass. The problem is, that we need to tell Mockito, that Annotations are used. So I created a small template which creates a setUp() Method which initializes the Mocks:

    @${testannotation type="org.junit.Before" default="Before" editable="false"}
    public void setUp()
    {
        ${mockitoannotations type="org.mockito.MockitoAnnotations" default="MockitoAnnotations" editable="false"}.initMocks( this );
    }

And this is the output for the template (of course with imports):

1
2
3
4
5
    @Before
    public void setUp()
    {
        MockitoAnnotations.initMocks( this );
    }

I personally find templates very useful, because they can be a huge timesaver. It is also worth checking the standard templates provided by Netbeans, it has tons of it by default. If you have some favorite template, feel free to share :)

Share this Diese Icons verlinken auf Bookmark Dienste bei denen Nutzer neue Inhalte finden und mit anderen teilen können.
  • MisterWong
  • del.icio.us
  • Google Bookmarks
  • Facebook
  • TwitThis
  • DZone
  • Digg
  • Print

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert