To achieve the desired behavior in my Maven project on Netbeans, I set up a profile defining an environment variable in my project's pom and modified the Test file action in my project's properties to activate my new profile. In this way I could check the enviroment variable in my tests. (This could have been done similarly with a system property.)
To keep from having to add a check to each and every GUI test, however, I found that I could add a JUnit filter, which would automatically ignore the UI tests in the case where I didn't want to run them and put the number of skipped tests in the test result line.
Here what my profile looks like:
<profile>
<id>test-gui</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<skipTests>false</skipTests>
<excludes>
<exclude>**/TestSuite.java</exclude>
</excludes>
<environmentVariables>
<GUI_TEST>true</GUI_TEST>
</environmentVariables>
</configuration>
</plugin>
</plugins>
</build>
</profile>
Here's a description of how to update the Netbeans Action:
(Right click on project in Projects window)->Properies
In Categories box, select 'Actions'
In Actions box, select 'Test file'
In Activate Profiles textfield, enter 'test-gui'
Click 'OK' button to save.
To add the JUnit filter (and the documentation was sparse, so I may not have done this in the most effective manner), I subclassed the default test runner, TestClassRunner and had it call my Filter, which checks the test name and the environment variable. To get JUnit to call my class, I annotated the test class.
public class GUITestClassRunner extends TestClassRunner {
public GUITestClassRunner(Class klazz) throws InitializationError {
super(klazz);
}
public GUITestClassRunner(Class klazz, Runner runner)
throws InitializationError, NoTestsRemainException {
super(klazz, runner);
}
@Override
public void run(RunNotifier notifier) {
if (getDescription().testCount() > 0) {
try {
filter(new GUIFilter(notifier));
} catch (NoTestsRemainException ex) {
Description description = getDescription();
notifier.fireTestStarted(description);
notifier.fireTestIgnored(description);
notifier.fireTestFinished(description);
return;
}
}
super.run(notifier);
}
class GUIFilter extends Filter {
private boolean isGUI = false;
private RunNotifier notifier;
public GUIFilter(RunNotifier notifier) {
this.notifier = notifier;
isGUI = UI.isGUITestEnvironment();
}
@Override
public boolean shouldRun(Description desc) {
if (!isGUI && UI.isGUITest(desc.getDisplayName())) {
Description description = getDescription();
notifier.fireTestStarted(description);
notifier.fireTestIgnored(description);
notifier.fireTestFinished(description);
return false;
}
return true;
}
@Override
public String describe() {
return "all tests except GUI tests if headless";
}
}
}
To call this runner, the test class needs to be annotated with:
@RunWith(GUITestClassRunner.class)
public class MyJUnitTestClass
{
....
@Test
public void testAlpha() {...}
@Test
public void testBeta_UI() {...}
}
So, now, from Netbeans, I just run my unit test class and the GUI tests automatically run. I can run from the command line with no environment variables set or GUI_TEST set to false, and my GUI tests are skipped, or I can run from the command line with GUI_TEST set to true, or use mvn -Ptest-gui
and all my GUI tests run.
My sincerest thanks to Brian and Pascal for pointing me in the right direction.
Ilane