Geertjan's Blog

  • October 21, 2014

Configuring & Running Specific Methods in Maven Projects in NetBeans IDE

Geertjan Wielenga
Product Manager

In yesterday's blog entry, you saw that a specific test method, i.e., the currently focused test method, can be run, instead of all the test methods, i.e., you have fine-grained control over specifically which test method is executed. However, is it possible to configure that test method, passing in JVM arguments for that specific test method, for example?

Right-click the application and choose Properties, or choose Properties from the File menu, and assuming you're working on a Maven-based application, you'll see the tab below in the Project Properties dialog:

Above, you see the definition of the action that is run when "Test File" is called on a file, which can be done via a menu item or via the keyboard, i.e., by default, Ctrl-6.

Here you can see that I have customized it so that a specific test method is invoked, instead of all the methods in the class:

In the above example, a test method named "testPrintMessage" is run when the "Test File" action, e.g., via Ctrl-6, is performed.

That's because, as my colleague Theofanis told me today, these are Maven commands one can run on the command line, and the above is the equivalents of the below:

mvn -Dtest=a.b.c.MyTest surefire:test would run all tests in MyTest test class

mvn -Dtest=a.b.c.MyTest#testMethodName surefire:test would only run testMethodName in MyTest test class

When you click the Add button above, you are helped in different ways in seeing the options available that can be added:

However, instead of overriding the "Test File" action, you can create custom actions for each method you want to have control over, after you click "Add Custom" below:

After you add a custom action, like above, you can invoke it by right-clicking the file on which you want to run it:

Now, in the Output window, I can see that this is part of the command that NetBeans runs for me when I click the above menu item:

-Dtest=com.mycompany.hello.MainTest#testPrintMessage -Dvertx.timeout=9999

Hope this answers Martijn Verburg's question on Twitter:


Handily, you can also run the custom action directly in the editor, after right-clicking it:

One nice improvement would be if you could map a keyboard shortcut to your custom actions, so you wouldn't have to right-click on a file or in the editor but just press some keys on the keyboard. But simply right-clicking in the editor and then clicking the menu item, as shown above, is not bad at all, pretty handy that you can extend the NetBeans Java Editor with your own custom Maven actions, isn't it? :-)

Once you've created one custom action, like the above, all the next ones are simply a matter of writing in the XML file that is created the first time you create a custom action:

The above "nbactions.xml" file looks as follows, after the steps I took above:

<?xml version="1.0" encoding="UTF-8"?>
<actionName>CUSTOM-"testPrintMessage" method</actionName>
<displayName>"testPrintMessage" method</displayName>

So, you want to give yourself the option of running each test method separately? Just copy the action elements above, customize them, and automatically when you right-click the file or in the editor, you'll see a new menu item that connects to the related action in the file above.

Join the discussion

Comments ( 4 )
  • guest Wednesday, October 22, 2014

    Very useful, thank you!

  • Vidhyadharan Thursday, October 23, 2014

    Indeed ..NB maven is getting mature... i love it

  • jmborer Monday, October 27, 2014

    I knew this trick, but what I would love is have a small GUI that does the same: a window listing all test where you can select the test(s) to run.

    I thought starting to develop this myself, but I don't know exactly where to start (many modules comming into play here).

  • jmborer Tuesday, October 28, 2014

    Another nice trick to rerun a test: you can right click on test in a test results window and choose "Run Again".

Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.