Menu

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.