NetBeans – Creating Automatic Code Templates

I’m guessing everyone in the world already knows this, but its taken me ages to find and set-up. I’ve recently being following James Shores’s excellent Test Driven Development Screencasts, and have been finding it incredibly tedious to have to type out the Test syntax on each case.

I’ve searched before and not had any success, but today I revisited trying to insert a code template to insert the code for me automatically – and I found it. For future reference for myself,  and maybe for others out there heres the simple steps:

In NetBeans, select Tools->Options. Then select the Editor icon. Then select the Code Templates tab, and select Java for the language. Here, either look through the currently defined templates and re-use, or alternatively click ‘New’.

I gave my template an abbreviation of ‘test’ and then inserted the code for the test (e.g. @Test public void () { }).

Now in the IDE I can just type test [followed by pressing TAB] for the automatic code generation to insert the text for me.


One thought on “NetBeans – Creating Automatic Code Templates

  1. I just bumped over this, and figured if I found it, others will too and thought I would suggest a small improvement. If you enter the following for the code

    public void ${testname}() {

    you will be able to name your method whatever you want, hit enter, and the System.out.println will be ready, and the mouse cursor will be where the ${cursor} placeholder is.

Leave a Reply

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

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

Google+ photo

You are commenting using your Google+ 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 )


Connecting to %s