Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Customized new JUnit templates in Eclipse?

I've spent a few hours searching for this, and can't find it, so I'm asking. Why here?

How can I customize Eclipse so that when I create a new JUnit test class or method, it includes different items? For other source, I'd just go into Window->Preferences, and change the templates.

But for JUnit, that doesn't seem to be possible.

I've recently installed MoreUnit, and that doesn't seem to help either.

Anyone have a pointer?

like image 258
CPerkins Avatar asked Aug 28 '09 02:08

CPerkins


People also ask

How do I import a template into Eclipse?

To import a code template: Click Import to open the "Import Templates" browser. Select the relevant XML file containing the template information. Click Open.

How do I run a template in Eclipse?

Click Ctrl+Space. The Content Assist box will appear, listing all available templates and completion options that begin with that combination of keys. Templates are marked in the content assist list with a blue square. Double-click the required template from the list.

How do I create a JUnit test in Eclipse?

To use JUnit you must create a separate . java file in your project that will test one of your existing classes. In the Package Explorer area on the left side of the Eclipse window, right-click the class you want to test and click New → JUnit Test Case. A dialog box will pop up to help you create your test case.


1 Answers

It does not seem to be directly available.

The idea has been raised to save a file (like a JUnit empty file) as a template, to reuse it at future (JUnit for instance) creation time.
This is bug 37440... and it is not very popular (since 2003!). And in WON'T FIX status...

The closest would be

Window > Preferences > Java > Editor - Templates and look under "test method (JUnit4)"

However, as mentioned in this thread:

but that will not affect the auto-generated JUnit test method stub templates.
In other words in the New JUnit Test Case dialog, one can select the methods of the class under test and test methods stubs are auto-generated.

To modify the template that creates these JUnit test method stubs... seems not possible at the moment.

Bug 71783 is somewhat related to this issue, regarding the comment templates of those methods.

Note: the generation of the JUnit class itself seems entirely hard-coded in the class

org.eclipse.jdt.junit.wizards.NewTestCaseWizardPageOne


Speaking of template, you also needed to to provide VM arguments to all JUnit tests you run, manually copying them for every new configuration.
Bug 37862 has been set to address that and allow to create a Installed JRE for use with a JUnit testing, configuring it with the correct set of VM arguments.

like image 152
VonC Avatar answered Sep 17 '22 15:09

VonC